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, 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, 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, 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, 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(
222 ISD::SHL, dl, RVT, DAG.getConstant(1, dl, RVT),
223 DAG.getConstant(RSize - 1, dl,
224 TLI.getShiftAmountTy(RVT, DAG.getDataLayout())));
225 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
227 // Shift right or sign-extend it if the two operands have different types.
228 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
231 DAG.getNode(ISD::SRL, dl, RVT, SignBit,
232 DAG.getConstant(SizeDiff, dl,
233 TLI.getShiftAmountTy(SignBit.getValueType(),
234 DAG.getDataLayout())));
235 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
236 } else if (SizeDiff < 0) {
237 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
239 DAG.getNode(ISD::SHL, dl, LVT, SignBit,
240 DAG.getConstant(-SizeDiff, dl,
241 TLI.getShiftAmountTy(SignBit.getValueType(),
242 DAG.getDataLayout())));
245 // Clear the sign bit of the first operand.
246 SDValue Mask = DAG.getNode(
247 ISD::SHL, dl, LVT, DAG.getConstant(1, dl, LVT),
248 DAG.getConstant(LSize - 1, dl,
249 TLI.getShiftAmountTy(LVT, DAG.getDataLayout())));
250 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, dl, LVT));
251 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
253 // Or the value with the sign bit.
254 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
257 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
258 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
259 SDValue Op = GetSoftenedFloat(N->getOperand(0));
260 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
266 NVT, Op, false, SDLoc(N)).first;
269 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
270 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
271 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
272 GetSoftenedFloat(N->getOperand(1)) };
273 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
279 NVT, Ops, false, SDLoc(N)).first;
282 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
283 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
284 SDValue Op = GetSoftenedFloat(N->getOperand(0));
285 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
291 NVT, Op, false, SDLoc(N)).first;
294 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
295 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
296 SDValue Op = GetSoftenedFloat(N->getOperand(0));
297 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
302 RTLIB::EXP2_PPCF128),
303 NVT, Op, false, SDLoc(N)).first;
306 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
307 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
308 SDValue Op = GetSoftenedFloat(N->getOperand(0));
309 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
314 RTLIB::FLOOR_PPCF128),
315 NVT, Op, false, SDLoc(N)).first;
318 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
319 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
320 SDValue Op = GetSoftenedFloat(N->getOperand(0));
321 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
327 NVT, Op, false, SDLoc(N)).first;
330 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
331 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
332 SDValue Op = GetSoftenedFloat(N->getOperand(0));
333 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
338 RTLIB::LOG2_PPCF128),
339 NVT, Op, false, SDLoc(N)).first;
342 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
343 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
344 SDValue Op = GetSoftenedFloat(N->getOperand(0));
345 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
350 RTLIB::LOG10_PPCF128),
351 NVT, Op, false, SDLoc(N)).first;
354 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
355 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
356 SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
357 GetSoftenedFloat(N->getOperand(1)),
358 GetSoftenedFloat(N->getOperand(2)) };
359 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
365 NVT, Ops, false, SDLoc(N)).first;
368 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
369 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
370 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
371 GetSoftenedFloat(N->getOperand(1)) };
372 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
378 NVT, Ops, false, SDLoc(N)).first;
381 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
382 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
383 SDValue Op = GetSoftenedFloat(N->getOperand(0));
384 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
385 RTLIB::NEARBYINT_F32,
386 RTLIB::NEARBYINT_F64,
387 RTLIB::NEARBYINT_F80,
388 RTLIB::NEARBYINT_F128,
389 RTLIB::NEARBYINT_PPCF128),
390 NVT, Op, false, SDLoc(N)).first;
393 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
394 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
396 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
397 SDValue Ops[2] = { DAG.getConstantFP(-0.0, dl, N->getValueType(0)),
398 GetSoftenedFloat(N->getOperand(0)) };
399 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
405 NVT, Ops, false, dl).first;
408 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
409 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
410 SDValue Op = N->getOperand(0);
412 // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
413 // entirely possible for both f16 and f32 to be legal, so use the fully
414 // hard-float FP_EXTEND rather than FP16_TO_FP.
415 if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
416 Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
417 if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
418 SoftenFloatResult(Op.getNode(), 0);
421 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat) {
422 Op = GetPromotedFloat(Op);
423 // If the promotion did the FP_EXTEND to the destination type for us,
424 // there's nothing left to do here.
425 if (Op.getValueType() == N->getValueType(0)) {
426 return BitConvertToInteger(Op);
430 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
431 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
432 Op = GetSoftenedFloat(Op);
433 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
434 return TLI.makeLibCall(DAG, LC, NVT, Op, false, SDLoc(N)).first;
437 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
439 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
440 EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
441 SDValue Op = N->getOperand(0);
442 SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, Op,
443 false, SDLoc(N)).first;
444 if (N->getValueType(0) == MVT::f32)
447 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
448 RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
449 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
450 return TLI.makeLibCall(DAG, LC, NVT, Res32, false, SDLoc(N)).first;
453 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
454 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
455 SDValue Op = N->getOperand(0);
456 if (N->getValueType(0) == MVT::f16) {
457 // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
458 // storage-only type get a chance to select things.
459 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
462 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
463 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
464 return TLI.makeLibCall(DAG, LC, NVT, Op, false, SDLoc(N)).first;
467 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
468 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
469 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
470 GetSoftenedFloat(N->getOperand(1)) };
471 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
477 NVT, Ops, false, SDLoc(N)).first;
480 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
481 assert(N->getOperand(1).getValueType() == MVT::i32 &&
482 "Unsupported power type!");
483 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
484 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
485 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
490 RTLIB::POWI_PPCF128),
491 NVT, Ops, false, SDLoc(N)).first;
494 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
495 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
496 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
497 GetSoftenedFloat(N->getOperand(1)) };
498 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
504 NVT, Ops, false, SDLoc(N)).first;
507 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
508 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
509 SDValue Op = GetSoftenedFloat(N->getOperand(0));
510 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
515 RTLIB::RINT_PPCF128),
516 NVT, Op, false, SDLoc(N)).first;
519 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
520 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
521 SDValue Op = GetSoftenedFloat(N->getOperand(0));
522 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
527 RTLIB::ROUND_PPCF128),
528 NVT, Op, false, SDLoc(N)).first;
531 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
532 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
533 SDValue Op = GetSoftenedFloat(N->getOperand(0));
534 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
540 NVT, Op, false, SDLoc(N)).first;
543 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
544 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
545 SDValue Op = GetSoftenedFloat(N->getOperand(0));
546 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
551 RTLIB::SQRT_PPCF128),
552 NVT, Op, false, SDLoc(N)).first;
555 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
556 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
557 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
558 GetSoftenedFloat(N->getOperand(1)) };
559 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
565 NVT, Ops, false, SDLoc(N)).first;
568 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
569 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
570 if (N->getValueType(0) == MVT::f16)
571 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
573 SDValue Op = GetSoftenedFloat(N->getOperand(0));
574 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
579 RTLIB::TRUNC_PPCF128),
580 NVT, Op, false, SDLoc(N)).first;
583 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
584 LoadSDNode *L = cast<LoadSDNode>(N);
585 EVT VT = N->getValueType(0);
586 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
590 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
591 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
592 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
593 L->getPointerInfo(), NVT, L->isVolatile(),
594 L->isNonTemporal(), false, L->getAlignment(),
596 // Legalized the chain result - switch anything that used the old chain to
598 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
602 // Do a non-extending load followed by FP_EXTEND.
603 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
604 L->getMemoryVT(), dl, L->getChain(),
605 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
606 L->getMemoryVT(), L->isVolatile(),
607 L->isNonTemporal(), false, L->getAlignment(),
609 // Legalized the chain result - switch anything that used the old chain to
611 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
612 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
615 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
616 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
617 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
618 return DAG.getSelect(SDLoc(N),
619 LHS.getValueType(), N->getOperand(0), LHS, RHS);
622 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
623 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
624 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
625 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
626 LHS.getValueType(), N->getOperand(0),
627 N->getOperand(1), LHS, RHS, N->getOperand(4));
630 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
631 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
632 N->getValueType(0)));
635 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
636 SDValue Chain = N->getOperand(0); // Get the chain.
637 SDValue Ptr = N->getOperand(1); // Get the pointer.
638 EVT VT = N->getValueType(0);
639 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
643 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
644 N->getConstantOperandVal(3));
646 // Legalized the chain result - switch anything that used the old chain to
648 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
652 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
653 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
654 EVT SVT = N->getOperand(0).getValueType();
655 EVT RVT = N->getValueType(0);
659 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
660 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
661 // match. Look for an appropriate libcall.
662 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
663 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
664 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
665 NVT = (MVT::SimpleValueType)t;
666 // The source needs to big enough to hold the operand.
668 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
670 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
672 // Sign/zero extend the argument if the libcall takes a larger type.
673 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
674 NVT, N->getOperand(0));
675 return TLI.makeLibCall(DAG, LC,
676 TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
677 Op, Signed, dl).first;
681 //===----------------------------------------------------------------------===//
682 // Operand Float to Integer Conversion..
683 //===----------------------------------------------------------------------===//
685 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
686 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
688 SDValue Res = SDValue();
690 switch (N->getOpcode()) {
693 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
694 N->dump(&DAG); dbgs() << "\n";
696 llvm_unreachable("Do not know how to soften this operator's operand!");
698 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
699 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
700 case ISD::FP_EXTEND: Res = SoftenFloatOp_FP_EXTEND(N); break;
701 case ISD::FP_TO_FP16: // Same as FP_ROUND for softening purposes
702 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
703 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
704 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
705 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
706 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
707 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
710 // If the result is null, the sub-method took care of registering results etc.
711 if (!Res.getNode()) return false;
713 // If the result is N, the sub-method updated N in place. Tell the legalizer
715 if (Res.getNode() == N)
718 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
719 "Invalid operand expansion");
721 ReplaceValueWith(SDValue(N, 0), Res);
725 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
726 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
727 GetSoftenedFloat(N->getOperand(0)));
730 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
731 // If we get here, the result must be legal but the source illegal.
732 EVT SVT = N->getOperand(0).getValueType();
733 EVT RVT = N->getValueType(0);
734 SDValue Op = GetSoftenedFloat(N->getOperand(0));
737 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
739 RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
740 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
742 return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
746 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
747 // We actually deal with the partially-softened FP_TO_FP16 node too, which
748 // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
749 assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
751 EVT SVT = N->getOperand(0).getValueType();
752 EVT RVT = N->getValueType(0);
753 EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
755 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
756 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
758 SDValue Op = GetSoftenedFloat(N->getOperand(0));
759 return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
762 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
763 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
764 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
766 EVT VT = NewLHS.getValueType();
767 NewLHS = GetSoftenedFloat(NewLHS);
768 NewRHS = GetSoftenedFloat(NewRHS);
769 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
771 // If softenSetCCOperands returned a scalar, we need to compare the result
772 // against zero to select between true and false values.
773 if (!NewRHS.getNode()) {
774 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
778 // Update N to have the operands specified.
779 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
780 DAG.getCondCode(CCCode), NewLHS, NewRHS,
785 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
786 EVT RVT = N->getValueType(0);
787 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
788 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
789 SDValue Op = GetSoftenedFloat(N->getOperand(0));
790 return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
793 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
794 EVT RVT = N->getValueType(0);
795 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
796 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
797 SDValue Op = GetSoftenedFloat(N->getOperand(0));
798 return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
801 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
802 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
803 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
805 EVT VT = NewLHS.getValueType();
806 NewLHS = GetSoftenedFloat(NewLHS);
807 NewRHS = GetSoftenedFloat(NewRHS);
808 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
810 // If softenSetCCOperands returned a scalar, we need to compare the result
811 // against zero to select between true and false values.
812 if (!NewRHS.getNode()) {
813 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
817 // Update N to have the operands specified.
818 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
819 N->getOperand(2), N->getOperand(3),
820 DAG.getCondCode(CCCode)),
824 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
825 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
826 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
828 EVT VT = NewLHS.getValueType();
829 NewLHS = GetSoftenedFloat(NewLHS);
830 NewRHS = GetSoftenedFloat(NewRHS);
831 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
833 // If softenSetCCOperands returned a scalar, use it.
834 if (!NewRHS.getNode()) {
835 assert(NewLHS.getValueType() == N->getValueType(0) &&
836 "Unexpected setcc expansion!");
840 // Otherwise, update N to have the operands specified.
841 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
842 DAG.getCondCode(CCCode)),
846 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
847 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
848 assert(OpNo == 1 && "Can only soften the stored value!");
849 StoreSDNode *ST = cast<StoreSDNode>(N);
850 SDValue Val = ST->getValue();
853 if (ST->isTruncatingStore())
854 // Do an FP_ROUND followed by a non-truncating store.
855 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
856 Val, DAG.getIntPtrConstant(0, dl)));
858 Val = GetSoftenedFloat(Val);
860 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
861 ST->getMemOperand());
865 //===----------------------------------------------------------------------===//
866 // Float Result Expansion
867 //===----------------------------------------------------------------------===//
869 /// ExpandFloatResult - This method is called when the specified result of the
870 /// specified node is found to need expansion. At this point, the node may also
871 /// have invalid operands or may have other results that need promotion, we just
872 /// know that (at least) one result needs expansion.
873 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
874 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
878 // See if the target wants to custom expand this node.
879 if (CustomLowerNode(N, N->getValueType(ResNo), true))
882 switch (N->getOpcode()) {
885 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
886 N->dump(&DAG); dbgs() << "\n";
888 llvm_unreachable("Do not know how to expand the result of this operator!");
890 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
891 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
892 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
894 case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
895 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
896 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
897 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
898 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
899 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
901 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
902 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
903 case ISD::FMINNUM: ExpandFloatRes_FMINNUM(N, Lo, Hi); break;
904 case ISD::FMAXNUM: ExpandFloatRes_FMAXNUM(N, Lo, Hi); break;
905 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
906 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
907 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
908 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
909 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
910 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
911 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
912 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
913 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
914 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
915 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
916 case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
917 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
918 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
919 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
920 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
921 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
922 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
923 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
924 case ISD::FROUND: ExpandFloatRes_FROUND(N, Lo, Hi); break;
925 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
926 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
927 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
928 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
929 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
930 case ISD::SINT_TO_FP:
931 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
932 case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
935 // If Lo/Hi is null, the sub-method took care of registering results etc.
937 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
940 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
942 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
943 assert(NVT.getSizeInBits() == integerPartWidth &&
944 "Do not know how to expand this float constant!");
945 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
947 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
948 APInt(integerPartWidth, C.getRawData()[1])),
950 Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
951 APInt(integerPartWidth, C.getRawData()[0])),
955 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
957 assert(N->getValueType(0) == MVT::ppcf128 &&
958 "Logic only correct for ppcf128!");
961 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
962 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
963 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
964 Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
965 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
969 void DAGTypeLegalizer::ExpandFloatRes_FMINNUM(SDNode *N, SDValue &Lo,
971 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
972 RTLIB::FMIN_F32, RTLIB::FMIN_F64,
973 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
974 RTLIB::FMIN_PPCF128),
976 GetPairElements(Call, Lo, Hi);
979 void DAGTypeLegalizer::ExpandFloatRes_FMAXNUM(SDNode *N, SDValue &Lo,
981 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
982 RTLIB::FMAX_F32, RTLIB::FMAX_F64,
983 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
984 RTLIB::FMAX_PPCF128),
986 GetPairElements(Call, Lo, Hi);
989 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
991 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
992 RTLIB::ADD_F32, RTLIB::ADD_F64,
993 RTLIB::ADD_F80, RTLIB::ADD_F128,
996 GetPairElements(Call, Lo, Hi);
999 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
1000 SDValue &Lo, SDValue &Hi) {
1001 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1002 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
1003 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
1004 RTLIB::CEIL_PPCF128),
1006 GetPairElements(Call, Lo, Hi);
1009 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
1010 SDValue &Lo, SDValue &Hi) {
1011 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1012 RTLIB::COPYSIGN_F32,
1013 RTLIB::COPYSIGN_F64,
1014 RTLIB::COPYSIGN_F80,
1015 RTLIB::COPYSIGN_F128,
1016 RTLIB::COPYSIGN_PPCF128),
1018 GetPairElements(Call, Lo, Hi);
1021 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
1022 SDValue &Lo, SDValue &Hi) {
1023 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1024 RTLIB::COS_F32, RTLIB::COS_F64,
1025 RTLIB::COS_F80, RTLIB::COS_F128,
1026 RTLIB::COS_PPCF128),
1028 GetPairElements(Call, Lo, Hi);
1031 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
1033 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1034 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1039 RTLIB::DIV_PPCF128),
1040 N->getValueType(0), Ops, false,
1042 GetPairElements(Call, Lo, Hi);
1045 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
1046 SDValue &Lo, SDValue &Hi) {
1047 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1048 RTLIB::EXP_F32, RTLIB::EXP_F64,
1049 RTLIB::EXP_F80, RTLIB::EXP_F128,
1050 RTLIB::EXP_PPCF128),
1052 GetPairElements(Call, Lo, Hi);
1055 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
1056 SDValue &Lo, SDValue &Hi) {
1057 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1058 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
1059 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
1060 RTLIB::EXP2_PPCF128),
1062 GetPairElements(Call, Lo, Hi);
1065 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
1066 SDValue &Lo, SDValue &Hi) {
1067 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1068 RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
1069 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
1070 RTLIB::FLOOR_PPCF128),
1072 GetPairElements(Call, Lo, Hi);
1075 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1076 SDValue &Lo, SDValue &Hi) {
1077 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1078 RTLIB::LOG_F32, RTLIB::LOG_F64,
1079 RTLIB::LOG_F80, RTLIB::LOG_F128,
1080 RTLIB::LOG_PPCF128),
1082 GetPairElements(Call, Lo, Hi);
1085 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1086 SDValue &Lo, SDValue &Hi) {
1087 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1088 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1089 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1090 RTLIB::LOG2_PPCF128),
1092 GetPairElements(Call, Lo, Hi);
1095 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1096 SDValue &Lo, SDValue &Hi) {
1097 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1098 RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1099 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1100 RTLIB::LOG10_PPCF128),
1102 GetPairElements(Call, Lo, Hi);
1105 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1107 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1108 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1113 RTLIB::FMA_PPCF128),
1114 N->getValueType(0), Ops, false,
1116 GetPairElements(Call, Lo, Hi);
1119 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1121 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1122 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1127 RTLIB::MUL_PPCF128),
1128 N->getValueType(0), Ops, false,
1130 GetPairElements(Call, Lo, Hi);
1133 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1134 SDValue &Lo, SDValue &Hi) {
1135 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1136 RTLIB::NEARBYINT_F32,
1137 RTLIB::NEARBYINT_F64,
1138 RTLIB::NEARBYINT_F80,
1139 RTLIB::NEARBYINT_F128,
1140 RTLIB::NEARBYINT_PPCF128),
1142 GetPairElements(Call, Lo, Hi);
1145 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1148 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1149 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1150 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1153 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1155 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1157 Hi = DAG.getNode(ISD::FP_EXTEND, dl, NVT, N->getOperand(0));
1158 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1159 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1162 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1163 SDValue &Lo, SDValue &Hi) {
1164 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1165 RTLIB::POW_F32, RTLIB::POW_F64,
1166 RTLIB::POW_F80, RTLIB::POW_F128,
1167 RTLIB::POW_PPCF128),
1169 GetPairElements(Call, Lo, Hi);
1172 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1173 SDValue &Lo, SDValue &Hi) {
1174 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1175 RTLIB::POWI_F32, RTLIB::POWI_F64,
1176 RTLIB::POWI_F80, RTLIB::POWI_F128,
1177 RTLIB::POWI_PPCF128),
1179 GetPairElements(Call, Lo, Hi);
1182 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1183 SDValue &Lo, SDValue &Hi) {
1184 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1185 RTLIB::REM_F32, RTLIB::REM_F64,
1186 RTLIB::REM_F80, RTLIB::REM_F128,
1187 RTLIB::REM_PPCF128),
1189 GetPairElements(Call, Lo, Hi);
1192 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1193 SDValue &Lo, SDValue &Hi) {
1194 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1195 RTLIB::RINT_F32, RTLIB::RINT_F64,
1196 RTLIB::RINT_F80, RTLIB::RINT_F128,
1197 RTLIB::RINT_PPCF128),
1199 GetPairElements(Call, Lo, Hi);
1202 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1203 SDValue &Lo, SDValue &Hi) {
1204 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1209 RTLIB::ROUND_PPCF128),
1211 GetPairElements(Call, Lo, Hi);
1214 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1215 SDValue &Lo, SDValue &Hi) {
1216 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1217 RTLIB::SIN_F32, RTLIB::SIN_F64,
1218 RTLIB::SIN_F80, RTLIB::SIN_F128,
1219 RTLIB::SIN_PPCF128),
1221 GetPairElements(Call, Lo, Hi);
1224 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1225 SDValue &Lo, SDValue &Hi) {
1226 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1227 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1228 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1229 RTLIB::SQRT_PPCF128),
1231 GetPairElements(Call, Lo, Hi);
1234 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1236 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1237 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1242 RTLIB::SUB_PPCF128),
1243 N->getValueType(0), Ops, false,
1245 GetPairElements(Call, Lo, Hi);
1248 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1249 SDValue &Lo, SDValue &Hi) {
1250 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1251 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1252 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1253 RTLIB::TRUNC_PPCF128),
1255 GetPairElements(Call, Lo, Hi);
1258 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1260 if (ISD::isNormalLoad(N)) {
1261 ExpandRes_NormalLoad(N, Lo, Hi);
1265 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1266 LoadSDNode *LD = cast<LoadSDNode>(N);
1267 SDValue Chain = LD->getChain();
1268 SDValue Ptr = LD->getBasePtr();
1271 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1272 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1273 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1275 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1276 LD->getMemoryVT(), LD->getMemOperand());
1278 // Remember the chain.
1279 Chain = Hi.getValue(1);
1281 // The low part is zero.
1282 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1283 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1285 // Modified the chain - switch anything that used the old chain to use the
1287 ReplaceValueWith(SDValue(LD, 1), Chain);
1290 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1292 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1293 EVT VT = N->getValueType(0);
1294 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1295 SDValue Src = N->getOperand(0);
1296 EVT SrcVT = Src.getValueType();
1297 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1300 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1301 // When promoting partial word types to i32 we must honor the signedness,
1303 if (SrcVT.bitsLE(MVT::i32)) {
1304 // The integer can be represented exactly in an f64.
1305 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1307 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1308 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1309 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1311 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1312 if (SrcVT.bitsLE(MVT::i64)) {
1313 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1315 LC = RTLIB::SINTTOFP_I64_PPCF128;
1316 } else if (SrcVT.bitsLE(MVT::i128)) {
1317 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1318 LC = RTLIB::SINTTOFP_I128_PPCF128;
1320 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1322 Hi = TLI.makeLibCall(DAG, LC, VT, Src, true, dl).first;
1323 GetPairElements(Hi, Lo, Hi);
1329 // Unsigned - fix up the SINT_TO_FP value just calculated.
1330 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1331 SrcVT = Src.getValueType();
1333 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1334 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1335 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1336 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1337 ArrayRef<uint64_t> Parts;
1339 switch (SrcVT.getSimpleVT().SimpleTy) {
1341 llvm_unreachable("Unsupported UINT_TO_FP!");
1353 // TODO: Are there fast-math-flags to propagate to this FADD?
1354 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1355 DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1358 Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, dl, SrcVT),
1359 Lo, Hi, ISD::SETLT);
1360 GetPairElements(Lo, Lo, Hi);
1364 //===----------------------------------------------------------------------===//
1365 // Float Operand Expansion
1366 //===----------------------------------------------------------------------===//
1368 /// ExpandFloatOperand - This method is called when the specified operand of the
1369 /// specified node is found to need expansion. At this point, all of the result
1370 /// types of the node are known to be legal, but other operands of the node may
1371 /// need promotion or expansion as well as the specified one.
1372 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1373 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1374 SDValue Res = SDValue();
1376 // See if the target wants to custom expand this node.
1377 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1380 switch (N->getOpcode()) {
1383 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1384 N->dump(&DAG); dbgs() << "\n";
1386 llvm_unreachable("Do not know how to expand this operator's operand!");
1388 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1389 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1390 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1392 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1393 case ISD::FCOPYSIGN: Res = ExpandFloatOp_FCOPYSIGN(N); break;
1394 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1395 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1396 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1397 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1398 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1399 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1403 // If the result is null, the sub-method took care of registering results etc.
1404 if (!Res.getNode()) return false;
1406 // If the result is N, the sub-method updated N in place. Tell the legalizer
1408 if (Res.getNode() == N)
1411 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1412 "Invalid operand expansion");
1414 ReplaceValueWith(SDValue(N, 0), Res);
1418 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1419 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1420 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1422 ISD::CondCode &CCCode,
1424 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1425 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1426 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1428 assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1430 // FIXME: This generated code sucks. We want to generate
1431 // FCMPU crN, hi1, hi2
1433 // FCMPU crN, lo1, lo2
1434 // The following can be improved, but not that much.
1435 SDValue Tmp1, Tmp2, Tmp3;
1436 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1437 LHSHi, RHSHi, ISD::SETOEQ);
1438 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1439 LHSLo, RHSLo, CCCode);
1440 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1441 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1442 LHSHi, RHSHi, ISD::SETUNE);
1443 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1444 LHSHi, RHSHi, CCCode);
1445 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1446 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1447 NewRHS = SDValue(); // LHS is the result, not a compare.
1450 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1451 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1452 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1453 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1455 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1456 // against zero to select between true and false values.
1457 if (!NewRHS.getNode()) {
1458 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1459 CCCode = ISD::SETNE;
1462 // Update N to have the operands specified.
1463 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1464 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1465 N->getOperand(4)), 0);
1468 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1469 assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1470 "Logic only correct for ppcf128!");
1472 GetExpandedFloat(N->getOperand(1), Lo, Hi);
1473 // The ppcf128 value is providing only the sign; take it from the
1474 // higher-order double (which must have the larger magnitude).
1475 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1476 N->getValueType(0), N->getOperand(0), Hi);
1479 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1480 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1481 "Logic only correct for ppcf128!");
1483 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1484 // Round it the rest of the way (e.g. to f32) if needed.
1485 return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1486 N->getValueType(0), Hi, N->getOperand(1));
1489 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1490 EVT RVT = N->getValueType(0);
1493 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1494 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1495 if (RVT == MVT::i32) {
1496 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1497 "Logic only correct for ppcf128!");
1498 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1499 N->getOperand(0), DAG.getValueType(MVT::f64));
1500 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1501 DAG.getIntPtrConstant(1, dl));
1502 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1505 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1506 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1507 return TLI.makeLibCall(DAG, LC, RVT, N->getOperand(0), false, dl).first;
1510 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1511 EVT RVT = N->getValueType(0);
1514 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1515 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1516 if (RVT == MVT::i32) {
1517 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1518 "Logic only correct for ppcf128!");
1519 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1520 APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1521 SDValue Tmp = DAG.getConstantFP(APF, dl, MVT::ppcf128);
1522 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1523 // FIXME: generated code sucks.
1524 // TODO: Are there fast-math-flags to propagate to this FSUB?
1525 return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1526 DAG.getNode(ISD::ADD, dl, MVT::i32,
1527 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1528 DAG.getNode(ISD::FSUB, dl,
1532 DAG.getConstant(0x80000000, dl,
1534 DAG.getNode(ISD::FP_TO_SINT, dl,
1535 MVT::i32, N->getOperand(0)),
1539 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1540 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1541 return TLI.makeLibCall(DAG, LC, N->getValueType(0), N->getOperand(0),
1545 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1546 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1547 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1548 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1550 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1551 // against zero to select between true and false values.
1552 if (!NewRHS.getNode()) {
1553 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1554 CCCode = ISD::SETNE;
1557 // Update N to have the operands specified.
1558 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1559 N->getOperand(2), N->getOperand(3),
1560 DAG.getCondCode(CCCode)), 0);
1563 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1564 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1565 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1566 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1568 // If ExpandSetCCOperands returned a scalar, use it.
1569 if (!NewRHS.getNode()) {
1570 assert(NewLHS.getValueType() == N->getValueType(0) &&
1571 "Unexpected setcc expansion!");
1575 // Otherwise, update N to have the operands specified.
1576 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1577 DAG.getCondCode(CCCode)), 0);
1580 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1581 if (ISD::isNormalStore(N))
1582 return ExpandOp_NormalStore(N, OpNo);
1584 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1585 assert(OpNo == 1 && "Can only expand the stored value so far");
1586 StoreSDNode *ST = cast<StoreSDNode>(N);
1588 SDValue Chain = ST->getChain();
1589 SDValue Ptr = ST->getBasePtr();
1591 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1592 ST->getValue().getValueType());
1593 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1594 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1598 GetExpandedOp(ST->getValue(), Lo, Hi);
1600 return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1601 ST->getMemoryVT(), ST->getMemOperand());
1604 //===----------------------------------------------------------------------===//
1605 // Float Operand Promotion
1606 //===----------------------------------------------------------------------===//
1609 static ISD::NodeType GetPromotionOpcode(EVT OpVT, EVT RetVT) {
1610 if (OpVT == MVT::f16) {
1611 return ISD::FP16_TO_FP;
1612 } else if (RetVT == MVT::f16) {
1613 return ISD::FP_TO_FP16;
1616 report_fatal_error("Attempt at an invalid promotion-related conversion");
1619 bool DAGTypeLegalizer::PromoteFloatOperand(SDNode *N, unsigned OpNo) {
1620 SDValue R = SDValue();
1622 // Nodes that use a promotion-requiring floating point operand, but doesn't
1623 // produce a promotion-requiring floating point result, need to be legalized
1624 // to use the promoted float operand. Nodes that produce at least one
1625 // promotion-requiring floating point result have their operands legalized as
1626 // a part of PromoteFloatResult.
1627 switch (N->getOpcode()) {
1629 llvm_unreachable("Do not know how to promote this operator's operand!");
1631 case ISD::BITCAST: R = PromoteFloatOp_BITCAST(N, OpNo); break;
1632 case ISD::FCOPYSIGN: R = PromoteFloatOp_FCOPYSIGN(N, OpNo); break;
1633 case ISD::FP_TO_SINT:
1634 case ISD::FP_TO_UINT: R = PromoteFloatOp_FP_TO_XINT(N, OpNo); break;
1635 case ISD::FP_EXTEND: R = PromoteFloatOp_FP_EXTEND(N, OpNo); break;
1636 case ISD::SELECT_CC: R = PromoteFloatOp_SELECT_CC(N, OpNo); break;
1637 case ISD::SETCC: R = PromoteFloatOp_SETCC(N, OpNo); break;
1638 case ISD::STORE: R = PromoteFloatOp_STORE(N, OpNo); break;
1642 ReplaceValueWith(SDValue(N, 0), R);
1646 SDValue DAGTypeLegalizer::PromoteFloatOp_BITCAST(SDNode *N, unsigned OpNo) {
1647 SDValue Op = N->getOperand(0);
1648 EVT OpVT = Op->getValueType(0);
1650 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
1651 assert (IVT == N->getValueType(0) && "Bitcast to type of different size");
1653 SDValue Promoted = GetPromotedFloat(N->getOperand(0));
1654 EVT PromotedVT = Promoted->getValueType(0);
1656 // Convert the promoted float value to the desired IVT.
1657 return DAG.getNode(GetPromotionOpcode(PromotedVT, OpVT), SDLoc(N), IVT,
1661 // Promote Operand 1 of FCOPYSIGN. Operand 0 ought to be handled by
1662 // PromoteFloatRes_FCOPYSIGN.
1663 SDValue DAGTypeLegalizer::PromoteFloatOp_FCOPYSIGN(SDNode *N, unsigned OpNo) {
1664 assert (OpNo == 1 && "Only Operand 1 must need promotion here");
1665 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1667 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1668 N->getOperand(0), Op1);
1671 // Convert the promoted float value to the desired integer type
1672 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_TO_XINT(SDNode *N, unsigned OpNo) {
1673 SDValue Op = GetPromotedFloat(N->getOperand(0));
1674 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), Op);
1677 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_EXTEND(SDNode *N, unsigned OpNo) {
1678 SDValue Op = GetPromotedFloat(N->getOperand(0));
1679 EVT VT = N->getValueType(0);
1681 // Desired VT is same as promoted type. Use promoted float directly.
1682 if (VT == Op->getValueType(0))
1685 // Else, extend the promoted float value to the desired VT.
1686 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Op);
1689 // Promote the float operands used for comparison. The true- and false-
1690 // operands have the same type as the result and are promoted, if needed, by
1691 // PromoteFloatRes_SELECT_CC
1692 SDValue DAGTypeLegalizer::PromoteFloatOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1693 SDValue LHS = GetPromotedFloat(N->getOperand(0));
1694 SDValue RHS = GetPromotedFloat(N->getOperand(1));
1696 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1697 LHS, RHS, N->getOperand(2), N->getOperand(3),
1701 // Construct a SETCC that compares the promoted values and sets the conditional
1703 SDValue DAGTypeLegalizer::PromoteFloatOp_SETCC(SDNode *N, unsigned OpNo) {
1704 EVT VT = N->getValueType(0);
1705 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1706 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1707 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1708 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1710 return DAG.getSetCC(SDLoc(N), NVT, Op0, Op1, CCCode);
1714 // Lower the promoted Float down to the integer value of same size and construct
1715 // a STORE of the integer value.
1716 SDValue DAGTypeLegalizer::PromoteFloatOp_STORE(SDNode *N, unsigned OpNo) {
1717 StoreSDNode *ST = cast<StoreSDNode>(N);
1718 SDValue Val = ST->getValue();
1721 SDValue Promoted = GetPromotedFloat(Val);
1722 EVT VT = ST->getOperand(1)->getValueType(0);
1723 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1726 NewVal = DAG.getNode(GetPromotionOpcode(Promoted.getValueType(), VT), DL,
1729 return DAG.getStore(ST->getChain(), DL, NewVal, ST->getBasePtr(),
1730 ST->getMemOperand());
1733 //===----------------------------------------------------------------------===//
1734 // Float Result Promotion
1735 //===----------------------------------------------------------------------===//
1737 void DAGTypeLegalizer::PromoteFloatResult(SDNode *N, unsigned ResNo) {
1738 SDValue R = SDValue();
1740 switch (N->getOpcode()) {
1741 // These opcodes cannot appear if promotion of FP16 is done in the backend
1743 case ISD::FP16_TO_FP:
1744 case ISD::FP_TO_FP16:
1746 llvm_unreachable("Do not know how to promote this operator's result!");
1748 case ISD::BITCAST: R = PromoteFloatRes_BITCAST(N); break;
1749 case ISD::ConstantFP: R = PromoteFloatRes_ConstantFP(N); break;
1750 case ISD::EXTRACT_VECTOR_ELT:
1751 R = PromoteFloatRes_EXTRACT_VECTOR_ELT(N); break;
1752 case ISD::FCOPYSIGN: R = PromoteFloatRes_FCOPYSIGN(N); break;
1754 // Unary FP Operations
1764 case ISD::FNEARBYINT:
1770 case ISD::FTRUNC: R = PromoteFloatRes_UnaryOp(N); break;
1772 // Binary FP Operations
1780 case ISD::FSUB: R = PromoteFloatRes_BinOp(N); break;
1782 case ISD::FMA: // FMA is same as FMAD
1783 case ISD::FMAD: R = PromoteFloatRes_FMAD(N); break;
1785 case ISD::FPOWI: R = PromoteFloatRes_FPOWI(N); break;
1787 case ISD::FP_ROUND: R = PromoteFloatRes_FP_ROUND(N); break;
1788 case ISD::LOAD: R = PromoteFloatRes_LOAD(N); break;
1789 case ISD::SELECT: R = PromoteFloatRes_SELECT(N); break;
1790 case ISD::SELECT_CC: R = PromoteFloatRes_SELECT_CC(N); break;
1792 case ISD::SINT_TO_FP:
1793 case ISD::UINT_TO_FP: R = PromoteFloatRes_XINT_TO_FP(N); break;
1794 case ISD::UNDEF: R = PromoteFloatRes_UNDEF(N); break;
1799 SetPromotedFloat(SDValue(N, ResNo), R);
1802 // Bitcast from i16 to f16: convert the i16 to a f32 value instead.
1803 // At this point, it is not possible to determine if the bitcast value is
1804 // eventually stored to memory or promoted to f32 or promoted to a floating
1805 // point at a higher precision. Some of these cases are handled by FP_EXTEND,
1806 // STORE promotion handlers.
1807 SDValue DAGTypeLegalizer::PromoteFloatRes_BITCAST(SDNode *N) {
1808 EVT VT = N->getValueType(0);
1809 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1810 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT,
1814 SDValue DAGTypeLegalizer::PromoteFloatRes_ConstantFP(SDNode *N) {
1815 ConstantFPSDNode *CFPNode = cast<ConstantFPSDNode>(N);
1816 EVT VT = N->getValueType(0);
1819 // Get the (bit-cast) APInt of the APFloat and build an integer constant
1820 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1821 SDValue C = DAG.getConstant(CFPNode->getValueAPF().bitcastToAPInt(), DL,
1824 // Convert the Constant to the desired FP type
1825 // FIXME We might be able to do the conversion during compilation and get rid
1826 // of it from the object code
1827 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1828 return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, C);
1831 // If the Index operand is a constant, try to redirect the extract operation to
1832 // the correct legalized vector. If not, bit-convert the input vector to
1833 // equivalent integer vector. Extract the element as an (bit-cast) integer
1834 // value and convert it to the promoted type.
1835 SDValue DAGTypeLegalizer::PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
1838 // If the index is constant, try to extract the value from the legalized
1840 if (isa<ConstantSDNode>(N->getOperand(1))) {
1841 SDValue Vec = N->getOperand(0);
1842 SDValue Idx = N->getOperand(1);
1843 EVT VecVT = Vec->getValueType(0);
1844 EVT EltVT = VecVT.getVectorElementType();
1846 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1848 switch (getTypeAction(VecVT)) {
1850 case TargetLowering::TypeScalarizeVector: {
1851 SDValue Res = GetScalarizedVector(N->getOperand(0));
1852 ReplaceValueWith(SDValue(N, 0), Res);
1855 case TargetLowering::TypeWidenVector: {
1856 Vec = GetWidenedVector(Vec);
1857 SDValue Res = DAG.getNode(N->getOpcode(), DL, EltVT, Vec, Idx);
1858 ReplaceValueWith(SDValue(N, 0), Res);
1861 case TargetLowering::TypeSplitVector: {
1863 GetSplitVector(Vec, Lo, Hi);
1865 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1867 if (IdxVal < LoElts)
1868 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Lo, Idx);
1870 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Hi,
1871 DAG.getConstant(IdxVal - LoElts, DL,
1872 Idx.getValueType()));
1873 ReplaceValueWith(SDValue(N, 0), Res);
1880 // Bit-convert the input vector to the equivalent integer vector
1881 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
1882 EVT IVT = NewOp.getValueType().getVectorElementType();
1884 // Extract the element as an (bit-cast) integer value
1885 SDValue NewVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IVT,
1886 NewOp, N->getOperand(1));
1888 // Convert the element to the desired FP type
1889 EVT VT = N->getValueType(0);
1890 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1891 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, NewVal);
1894 // FCOPYSIGN(X, Y) returns the value of X with the sign of Y. If the result
1895 // needs promotion, so does the argument X. Note that Y, if needed, will be
1896 // handled during operand promotion.
1897 SDValue DAGTypeLegalizer::PromoteFloatRes_FCOPYSIGN(SDNode *N) {
1898 EVT VT = N->getValueType(0);
1899 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1900 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1902 SDValue Op1 = N->getOperand(1);
1904 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1907 // Unary operation where the result and the operand have PromoteFloat type
1908 // action. Construct a new SDNode with the promoted float value of the old
1910 SDValue DAGTypeLegalizer::PromoteFloatRes_UnaryOp(SDNode *N) {
1911 EVT VT = N->getValueType(0);
1912 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1913 SDValue Op = GetPromotedFloat(N->getOperand(0));
1915 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op);
1918 // Binary operations where the result and both operands have PromoteFloat type
1919 // action. Construct a new SDNode with the promoted float values of the old
1921 SDValue DAGTypeLegalizer::PromoteFloatRes_BinOp(SDNode *N) {
1922 EVT VT = N->getValueType(0);
1923 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1924 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1925 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1926 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, N->getFlags());
1929 SDValue DAGTypeLegalizer::PromoteFloatRes_FMAD(SDNode *N) {
1930 EVT VT = N->getValueType(0);
1931 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1932 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1933 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1934 SDValue Op2 = GetPromotedFloat(N->getOperand(2));
1936 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, Op2);
1939 // Promote the Float (first) operand and retain the Integer (second) operand
1940 SDValue DAGTypeLegalizer::PromoteFloatRes_FPOWI(SDNode *N) {
1941 EVT VT = N->getValueType(0);
1942 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1943 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1944 SDValue Op1 = N->getOperand(1);
1946 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1949 // Explicit operation to reduce precision. Reduce the value to half precision
1950 // and promote it back to the legal type.
1951 SDValue DAGTypeLegalizer::PromoteFloatRes_FP_ROUND(SDNode *N) {
1954 SDValue Op = N->getOperand(0);
1955 EVT VT = N->getValueType(0);
1956 EVT OpVT = Op->getValueType(0);
1957 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1958 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1960 // Round promoted float to desired precision
1961 SDValue Round = DAG.getNode(GetPromotionOpcode(OpVT, VT), DL, IVT, Op);
1962 // Promote it back to the legal output type
1963 return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, Round);
1966 SDValue DAGTypeLegalizer::PromoteFloatRes_LOAD(SDNode *N) {
1967 LoadSDNode *L = cast<LoadSDNode>(N);
1968 EVT VT = N->getValueType(0);
1970 // Load the value as an integer value with the same number of bits
1971 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1972 SDValue newL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
1973 IVT, SDLoc(N), L->getChain(), L->getBasePtr(),
1974 L->getOffset(), L->getPointerInfo(), IVT, L->isVolatile(),
1975 L->isNonTemporal(), false, L->getAlignment(),
1977 // Legalize the chain result by replacing uses of the old value chain with the
1979 ReplaceValueWith(SDValue(N, 1), newL.getValue(1));
1981 // Convert the integer value to the desired FP type
1982 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1983 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, newL);
1986 // Construct a new SELECT node with the promoted true- and false- values.
1987 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT(SDNode *N) {
1988 SDValue TrueVal = GetPromotedFloat(N->getOperand(1));
1989 SDValue FalseVal = GetPromotedFloat(N->getOperand(2));
1991 return DAG.getNode(ISD::SELECT, SDLoc(N), TrueVal->getValueType(0),
1992 N->getOperand(0), TrueVal, FalseVal);
1995 // Construct a new SELECT_CC node with the promoted true- and false- values.
1996 // The operands used for comparison are promoted by PromoteFloatOp_SELECT_CC.
1997 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT_CC(SDNode *N) {
1998 SDValue TrueVal = GetPromotedFloat(N->getOperand(2));
1999 SDValue FalseVal = GetPromotedFloat(N->getOperand(3));
2001 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
2002 N->getOperand(0), N->getOperand(1), TrueVal, FalseVal,
2006 // Construct a SDNode that transforms the SINT or UINT operand to the promoted
2008 SDValue DAGTypeLegalizer::PromoteFloatRes_XINT_TO_FP(SDNode *N) {
2009 EVT VT = N->getValueType(0);
2010 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2011 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, N->getOperand(0));
2014 SDValue DAGTypeLegalizer::PromoteFloatRes_UNDEF(SDNode *N) {
2015 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
2016 N->getValueType(0)));