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 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
422 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
423 Op = GetSoftenedFloat(Op);
424 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
425 return TLI.makeLibCall(DAG, LC, NVT, Op, false, SDLoc(N)).first;
428 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
430 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
431 EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
432 SDValue Op = N->getOperand(0);
433 SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, Op,
434 false, SDLoc(N)).first;
435 if (N->getValueType(0) == MVT::f32)
438 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
439 RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
440 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
441 return TLI.makeLibCall(DAG, LC, NVT, Res32, false, SDLoc(N)).first;
444 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
445 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
446 SDValue Op = N->getOperand(0);
447 if (N->getValueType(0) == MVT::f16) {
448 // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
449 // storage-only type get a chance to select things.
450 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
453 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
454 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
455 return TLI.makeLibCall(DAG, LC, NVT, Op, false, SDLoc(N)).first;
458 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
459 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
460 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
461 GetSoftenedFloat(N->getOperand(1)) };
462 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
468 NVT, Ops, false, SDLoc(N)).first;
471 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
472 assert(N->getOperand(1).getValueType() == MVT::i32 &&
473 "Unsupported power type!");
474 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
475 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
476 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
481 RTLIB::POWI_PPCF128),
482 NVT, Ops, false, SDLoc(N)).first;
485 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
486 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
487 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
488 GetSoftenedFloat(N->getOperand(1)) };
489 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
495 NVT, Ops, false, SDLoc(N)).first;
498 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
499 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
500 SDValue Op = GetSoftenedFloat(N->getOperand(0));
501 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
506 RTLIB::RINT_PPCF128),
507 NVT, Op, false, SDLoc(N)).first;
510 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
511 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
512 SDValue Op = GetSoftenedFloat(N->getOperand(0));
513 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
518 RTLIB::ROUND_PPCF128),
519 NVT, Op, false, SDLoc(N)).first;
522 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
523 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
524 SDValue Op = GetSoftenedFloat(N->getOperand(0));
525 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
531 NVT, Op, false, SDLoc(N)).first;
534 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
535 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
536 SDValue Op = GetSoftenedFloat(N->getOperand(0));
537 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
542 RTLIB::SQRT_PPCF128),
543 NVT, Op, false, SDLoc(N)).first;
546 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
547 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
548 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
549 GetSoftenedFloat(N->getOperand(1)) };
550 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
556 NVT, Ops, false, SDLoc(N)).first;
559 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
560 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
561 if (N->getValueType(0) == MVT::f16)
562 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
564 SDValue Op = GetSoftenedFloat(N->getOperand(0));
565 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
570 RTLIB::TRUNC_PPCF128),
571 NVT, Op, false, SDLoc(N)).first;
574 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
575 LoadSDNode *L = cast<LoadSDNode>(N);
576 EVT VT = N->getValueType(0);
577 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
581 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
582 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
583 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
584 L->getPointerInfo(), NVT, L->isVolatile(),
585 L->isNonTemporal(), false, L->getAlignment(),
587 // Legalized the chain result - switch anything that used the old chain to
589 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
593 // Do a non-extending load followed by FP_EXTEND.
594 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
595 L->getMemoryVT(), dl, L->getChain(),
596 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
597 L->getMemoryVT(), L->isVolatile(),
598 L->isNonTemporal(), false, L->getAlignment(),
600 // Legalized the chain result - switch anything that used the old chain to
602 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
603 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
606 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
607 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
608 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
609 return DAG.getSelect(SDLoc(N),
610 LHS.getValueType(), N->getOperand(0), LHS, RHS);
613 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
614 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
615 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
616 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
617 LHS.getValueType(), N->getOperand(0),
618 N->getOperand(1), LHS, RHS, N->getOperand(4));
621 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
622 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
623 N->getValueType(0)));
626 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
627 SDValue Chain = N->getOperand(0); // Get the chain.
628 SDValue Ptr = N->getOperand(1); // Get the pointer.
629 EVT VT = N->getValueType(0);
630 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
634 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
635 N->getConstantOperandVal(3));
637 // Legalized the chain result - switch anything that used the old chain to
639 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
643 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
644 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
645 EVT SVT = N->getOperand(0).getValueType();
646 EVT RVT = N->getValueType(0);
650 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
651 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
652 // match. Look for an appropriate libcall.
653 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
654 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
655 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
656 NVT = (MVT::SimpleValueType)t;
657 // The source needs to big enough to hold the operand.
659 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
661 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
663 // Sign/zero extend the argument if the libcall takes a larger type.
664 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
665 NVT, N->getOperand(0));
666 return TLI.makeLibCall(DAG, LC,
667 TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
668 Op, Signed, dl).first;
672 //===----------------------------------------------------------------------===//
673 // Operand Float to Integer Conversion..
674 //===----------------------------------------------------------------------===//
676 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
677 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
679 SDValue Res = SDValue();
681 switch (N->getOpcode()) {
684 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
685 N->dump(&DAG); dbgs() << "\n";
687 llvm_unreachable("Do not know how to soften this operator's operand!");
689 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
690 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
691 case ISD::FP_EXTEND: Res = SoftenFloatOp_FP_EXTEND(N); break;
692 case ISD::FP_TO_FP16: // Same as FP_ROUND for softening purposes
693 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
694 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
695 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
696 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
697 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
698 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
701 // If the result is null, the sub-method took care of registering results etc.
702 if (!Res.getNode()) return false;
704 // If the result is N, the sub-method updated N in place. Tell the legalizer
706 if (Res.getNode() == N)
709 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
710 "Invalid operand expansion");
712 ReplaceValueWith(SDValue(N, 0), Res);
716 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
717 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
718 GetSoftenedFloat(N->getOperand(0)));
721 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
722 // If we get here, the result must be legal but the source illegal.
723 EVT SVT = N->getOperand(0).getValueType();
724 EVT RVT = N->getValueType(0);
725 SDValue Op = GetSoftenedFloat(N->getOperand(0));
728 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
730 RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
731 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
733 return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
737 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
738 // We actually deal with the partially-softened FP_TO_FP16 node too, which
739 // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
740 assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
742 EVT SVT = N->getOperand(0).getValueType();
743 EVT RVT = N->getValueType(0);
744 EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
746 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
747 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
749 SDValue Op = GetSoftenedFloat(N->getOperand(0));
750 return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
753 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
754 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
755 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
757 EVT VT = NewLHS.getValueType();
758 NewLHS = GetSoftenedFloat(NewLHS);
759 NewRHS = GetSoftenedFloat(NewRHS);
760 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
762 // If softenSetCCOperands returned a scalar, we need to compare the result
763 // against zero to select between true and false values.
764 if (!NewRHS.getNode()) {
765 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
769 // Update N to have the operands specified.
770 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
771 DAG.getCondCode(CCCode), NewLHS, NewRHS,
776 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
777 EVT RVT = N->getValueType(0);
778 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
779 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
780 SDValue Op = GetSoftenedFloat(N->getOperand(0));
781 return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
784 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
785 EVT RVT = N->getValueType(0);
786 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
787 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
788 SDValue Op = GetSoftenedFloat(N->getOperand(0));
789 return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
792 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
793 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
794 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
796 EVT VT = NewLHS.getValueType();
797 NewLHS = GetSoftenedFloat(NewLHS);
798 NewRHS = GetSoftenedFloat(NewRHS);
799 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
801 // If softenSetCCOperands returned a scalar, we need to compare the result
802 // against zero to select between true and false values.
803 if (!NewRHS.getNode()) {
804 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
808 // Update N to have the operands specified.
809 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
810 N->getOperand(2), N->getOperand(3),
811 DAG.getCondCode(CCCode)),
815 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
816 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
817 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
819 EVT VT = NewLHS.getValueType();
820 NewLHS = GetSoftenedFloat(NewLHS);
821 NewRHS = GetSoftenedFloat(NewRHS);
822 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
824 // If softenSetCCOperands returned a scalar, use it.
825 if (!NewRHS.getNode()) {
826 assert(NewLHS.getValueType() == N->getValueType(0) &&
827 "Unexpected setcc expansion!");
831 // Otherwise, update N to have the operands specified.
832 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
833 DAG.getCondCode(CCCode)),
837 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
838 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
839 assert(OpNo == 1 && "Can only soften the stored value!");
840 StoreSDNode *ST = cast<StoreSDNode>(N);
841 SDValue Val = ST->getValue();
844 if (ST->isTruncatingStore())
845 // Do an FP_ROUND followed by a non-truncating store.
846 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
847 Val, DAG.getIntPtrConstant(0, dl)));
849 Val = GetSoftenedFloat(Val);
851 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
852 ST->getMemOperand());
856 //===----------------------------------------------------------------------===//
857 // Float Result Expansion
858 //===----------------------------------------------------------------------===//
860 /// ExpandFloatResult - This method is called when the specified result of the
861 /// specified node is found to need expansion. At this point, the node may also
862 /// have invalid operands or may have other results that need promotion, we just
863 /// know that (at least) one result needs expansion.
864 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
865 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
869 // See if the target wants to custom expand this node.
870 if (CustomLowerNode(N, N->getValueType(ResNo), true))
873 switch (N->getOpcode()) {
876 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
877 N->dump(&DAG); dbgs() << "\n";
879 llvm_unreachable("Do not know how to expand the result of this operator!");
881 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
882 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
883 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
885 case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
886 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
887 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
888 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
889 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
890 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
892 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
893 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
894 case ISD::FMINNUM: ExpandFloatRes_FMINNUM(N, Lo, Hi); break;
895 case ISD::FMAXNUM: ExpandFloatRes_FMAXNUM(N, Lo, Hi); break;
896 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
897 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
898 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
899 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
900 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
901 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
902 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
903 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
904 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
905 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
906 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
907 case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
908 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
909 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
910 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
911 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
912 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
913 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
914 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
915 case ISD::FROUND: ExpandFloatRes_FROUND(N, Lo, Hi); break;
916 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
917 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
918 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
919 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
920 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
921 case ISD::SINT_TO_FP:
922 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
923 case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
926 // If Lo/Hi is null, the sub-method took care of registering results etc.
928 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
931 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
933 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
934 assert(NVT.getSizeInBits() == integerPartWidth &&
935 "Do not know how to expand this float constant!");
936 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
938 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
939 APInt(integerPartWidth, C.getRawData()[1])),
941 Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
942 APInt(integerPartWidth, C.getRawData()[0])),
946 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
948 assert(N->getValueType(0) == MVT::ppcf128 &&
949 "Logic only correct for ppcf128!");
952 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
953 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
954 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
955 Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
956 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
960 void DAGTypeLegalizer::ExpandFloatRes_FMINNUM(SDNode *N, SDValue &Lo,
962 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
963 RTLIB::FMIN_F32, RTLIB::FMIN_F64,
964 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
965 RTLIB::FMIN_PPCF128),
967 GetPairElements(Call, Lo, Hi);
970 void DAGTypeLegalizer::ExpandFloatRes_FMAXNUM(SDNode *N, SDValue &Lo,
972 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
973 RTLIB::FMAX_F32, RTLIB::FMAX_F64,
974 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
975 RTLIB::FMAX_PPCF128),
977 GetPairElements(Call, Lo, Hi);
980 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
982 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
983 RTLIB::ADD_F32, RTLIB::ADD_F64,
984 RTLIB::ADD_F80, RTLIB::ADD_F128,
987 GetPairElements(Call, Lo, Hi);
990 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
991 SDValue &Lo, SDValue &Hi) {
992 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
993 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
994 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
995 RTLIB::CEIL_PPCF128),
997 GetPairElements(Call, Lo, Hi);
1000 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
1001 SDValue &Lo, SDValue &Hi) {
1002 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1003 RTLIB::COPYSIGN_F32,
1004 RTLIB::COPYSIGN_F64,
1005 RTLIB::COPYSIGN_F80,
1006 RTLIB::COPYSIGN_F128,
1007 RTLIB::COPYSIGN_PPCF128),
1009 GetPairElements(Call, Lo, Hi);
1012 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
1013 SDValue &Lo, SDValue &Hi) {
1014 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1015 RTLIB::COS_F32, RTLIB::COS_F64,
1016 RTLIB::COS_F80, RTLIB::COS_F128,
1017 RTLIB::COS_PPCF128),
1019 GetPairElements(Call, Lo, Hi);
1022 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
1024 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1025 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1030 RTLIB::DIV_PPCF128),
1031 N->getValueType(0), Ops, false,
1033 GetPairElements(Call, Lo, Hi);
1036 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
1037 SDValue &Lo, SDValue &Hi) {
1038 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1039 RTLIB::EXP_F32, RTLIB::EXP_F64,
1040 RTLIB::EXP_F80, RTLIB::EXP_F128,
1041 RTLIB::EXP_PPCF128),
1043 GetPairElements(Call, Lo, Hi);
1046 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
1047 SDValue &Lo, SDValue &Hi) {
1048 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1049 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
1050 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
1051 RTLIB::EXP2_PPCF128),
1053 GetPairElements(Call, Lo, Hi);
1056 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
1057 SDValue &Lo, SDValue &Hi) {
1058 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1059 RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
1060 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
1061 RTLIB::FLOOR_PPCF128),
1063 GetPairElements(Call, Lo, Hi);
1066 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1067 SDValue &Lo, SDValue &Hi) {
1068 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1069 RTLIB::LOG_F32, RTLIB::LOG_F64,
1070 RTLIB::LOG_F80, RTLIB::LOG_F128,
1071 RTLIB::LOG_PPCF128),
1073 GetPairElements(Call, Lo, Hi);
1076 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1077 SDValue &Lo, SDValue &Hi) {
1078 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1079 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1080 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1081 RTLIB::LOG2_PPCF128),
1083 GetPairElements(Call, Lo, Hi);
1086 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1087 SDValue &Lo, SDValue &Hi) {
1088 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1089 RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1090 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1091 RTLIB::LOG10_PPCF128),
1093 GetPairElements(Call, Lo, Hi);
1096 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1098 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1099 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1104 RTLIB::FMA_PPCF128),
1105 N->getValueType(0), Ops, false,
1107 GetPairElements(Call, Lo, Hi);
1110 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1112 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1113 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1118 RTLIB::MUL_PPCF128),
1119 N->getValueType(0), Ops, false,
1121 GetPairElements(Call, Lo, Hi);
1124 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1125 SDValue &Lo, SDValue &Hi) {
1126 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1127 RTLIB::NEARBYINT_F32,
1128 RTLIB::NEARBYINT_F64,
1129 RTLIB::NEARBYINT_F80,
1130 RTLIB::NEARBYINT_F128,
1131 RTLIB::NEARBYINT_PPCF128),
1133 GetPairElements(Call, Lo, Hi);
1136 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1139 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1140 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1141 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1144 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1146 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1148 Hi = DAG.getNode(ISD::FP_EXTEND, dl, NVT, N->getOperand(0));
1149 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1150 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1153 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1154 SDValue &Lo, SDValue &Hi) {
1155 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1156 RTLIB::POW_F32, RTLIB::POW_F64,
1157 RTLIB::POW_F80, RTLIB::POW_F128,
1158 RTLIB::POW_PPCF128),
1160 GetPairElements(Call, Lo, Hi);
1163 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1164 SDValue &Lo, SDValue &Hi) {
1165 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1166 RTLIB::POWI_F32, RTLIB::POWI_F64,
1167 RTLIB::POWI_F80, RTLIB::POWI_F128,
1168 RTLIB::POWI_PPCF128),
1170 GetPairElements(Call, Lo, Hi);
1173 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1174 SDValue &Lo, SDValue &Hi) {
1175 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1176 RTLIB::REM_F32, RTLIB::REM_F64,
1177 RTLIB::REM_F80, RTLIB::REM_F128,
1178 RTLIB::REM_PPCF128),
1180 GetPairElements(Call, Lo, Hi);
1183 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1184 SDValue &Lo, SDValue &Hi) {
1185 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1186 RTLIB::RINT_F32, RTLIB::RINT_F64,
1187 RTLIB::RINT_F80, RTLIB::RINT_F128,
1188 RTLIB::RINT_PPCF128),
1190 GetPairElements(Call, Lo, Hi);
1193 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1194 SDValue &Lo, SDValue &Hi) {
1195 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1200 RTLIB::ROUND_PPCF128),
1202 GetPairElements(Call, Lo, Hi);
1205 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1206 SDValue &Lo, SDValue &Hi) {
1207 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1208 RTLIB::SIN_F32, RTLIB::SIN_F64,
1209 RTLIB::SIN_F80, RTLIB::SIN_F128,
1210 RTLIB::SIN_PPCF128),
1212 GetPairElements(Call, Lo, Hi);
1215 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1216 SDValue &Lo, SDValue &Hi) {
1217 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1218 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1219 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1220 RTLIB::SQRT_PPCF128),
1222 GetPairElements(Call, Lo, Hi);
1225 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1227 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1228 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1233 RTLIB::SUB_PPCF128),
1234 N->getValueType(0), Ops, false,
1236 GetPairElements(Call, Lo, Hi);
1239 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1240 SDValue &Lo, SDValue &Hi) {
1241 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1242 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1243 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1244 RTLIB::TRUNC_PPCF128),
1246 GetPairElements(Call, Lo, Hi);
1249 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1251 if (ISD::isNormalLoad(N)) {
1252 ExpandRes_NormalLoad(N, Lo, Hi);
1256 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1257 LoadSDNode *LD = cast<LoadSDNode>(N);
1258 SDValue Chain = LD->getChain();
1259 SDValue Ptr = LD->getBasePtr();
1262 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1263 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1264 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1266 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1267 LD->getMemoryVT(), LD->getMemOperand());
1269 // Remember the chain.
1270 Chain = Hi.getValue(1);
1272 // The low part is zero.
1273 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1274 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1276 // Modified the chain - switch anything that used the old chain to use the
1278 ReplaceValueWith(SDValue(LD, 1), Chain);
1281 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1283 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1284 EVT VT = N->getValueType(0);
1285 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1286 SDValue Src = N->getOperand(0);
1287 EVT SrcVT = Src.getValueType();
1288 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1291 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1292 // When promoting partial word types to i32 we must honor the signedness,
1294 if (SrcVT.bitsLE(MVT::i32)) {
1295 // The integer can be represented exactly in an f64.
1296 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1298 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1299 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1300 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1302 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1303 if (SrcVT.bitsLE(MVT::i64)) {
1304 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1306 LC = RTLIB::SINTTOFP_I64_PPCF128;
1307 } else if (SrcVT.bitsLE(MVT::i128)) {
1308 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1309 LC = RTLIB::SINTTOFP_I128_PPCF128;
1311 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1313 Hi = TLI.makeLibCall(DAG, LC, VT, Src, true, dl).first;
1314 GetPairElements(Hi, Lo, Hi);
1320 // Unsigned - fix up the SINT_TO_FP value just calculated.
1321 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1322 SrcVT = Src.getValueType();
1324 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1325 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1326 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1327 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1328 ArrayRef<uint64_t> Parts;
1330 switch (SrcVT.getSimpleVT().SimpleTy) {
1332 llvm_unreachable("Unsupported UINT_TO_FP!");
1344 // TODO: Are there fast-math-flags to propagate to this FADD?
1345 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1346 DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1349 Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, dl, SrcVT),
1350 Lo, Hi, ISD::SETLT);
1351 GetPairElements(Lo, Lo, Hi);
1355 //===----------------------------------------------------------------------===//
1356 // Float Operand Expansion
1357 //===----------------------------------------------------------------------===//
1359 /// ExpandFloatOperand - This method is called when the specified operand of the
1360 /// specified node is found to need expansion. At this point, all of the result
1361 /// types of the node are known to be legal, but other operands of the node may
1362 /// need promotion or expansion as well as the specified one.
1363 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1364 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1365 SDValue Res = SDValue();
1367 // See if the target wants to custom expand this node.
1368 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1371 switch (N->getOpcode()) {
1374 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1375 N->dump(&DAG); dbgs() << "\n";
1377 llvm_unreachable("Do not know how to expand this operator's operand!");
1379 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1380 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1381 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1383 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1384 case ISD::FCOPYSIGN: Res = ExpandFloatOp_FCOPYSIGN(N); break;
1385 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1386 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1387 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1388 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1389 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1390 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1394 // If the result is null, the sub-method took care of registering results etc.
1395 if (!Res.getNode()) return false;
1397 // If the result is N, the sub-method updated N in place. Tell the legalizer
1399 if (Res.getNode() == N)
1402 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1403 "Invalid operand expansion");
1405 ReplaceValueWith(SDValue(N, 0), Res);
1409 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1410 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1411 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1413 ISD::CondCode &CCCode,
1415 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1416 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1417 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1419 assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1421 // FIXME: This generated code sucks. We want to generate
1422 // FCMPU crN, hi1, hi2
1424 // FCMPU crN, lo1, lo2
1425 // The following can be improved, but not that much.
1426 SDValue Tmp1, Tmp2, Tmp3;
1427 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1428 LHSHi, RHSHi, ISD::SETOEQ);
1429 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1430 LHSLo, RHSLo, CCCode);
1431 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1432 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1433 LHSHi, RHSHi, ISD::SETUNE);
1434 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1435 LHSHi, RHSHi, CCCode);
1436 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1437 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1438 NewRHS = SDValue(); // LHS is the result, not a compare.
1441 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1442 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1443 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1444 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1446 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1447 // against zero to select between true and false values.
1448 if (!NewRHS.getNode()) {
1449 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1450 CCCode = ISD::SETNE;
1453 // Update N to have the operands specified.
1454 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1455 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1456 N->getOperand(4)), 0);
1459 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1460 assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1461 "Logic only correct for ppcf128!");
1463 GetExpandedFloat(N->getOperand(1), Lo, Hi);
1464 // The ppcf128 value is providing only the sign; take it from the
1465 // higher-order double (which must have the larger magnitude).
1466 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1467 N->getValueType(0), N->getOperand(0), Hi);
1470 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1471 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1472 "Logic only correct for ppcf128!");
1474 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1475 // Round it the rest of the way (e.g. to f32) if needed.
1476 return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1477 N->getValueType(0), Hi, N->getOperand(1));
1480 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1481 EVT RVT = N->getValueType(0);
1484 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1485 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1486 if (RVT == MVT::i32) {
1487 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1488 "Logic only correct for ppcf128!");
1489 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1490 N->getOperand(0), DAG.getValueType(MVT::f64));
1491 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1492 DAG.getIntPtrConstant(1, dl));
1493 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1496 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1497 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1498 return TLI.makeLibCall(DAG, LC, RVT, N->getOperand(0), false, dl).first;
1501 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1502 EVT RVT = N->getValueType(0);
1505 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1506 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1507 if (RVT == MVT::i32) {
1508 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1509 "Logic only correct for ppcf128!");
1510 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1511 APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1512 SDValue Tmp = DAG.getConstantFP(APF, dl, MVT::ppcf128);
1513 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1514 // FIXME: generated code sucks.
1515 // TODO: Are there fast-math-flags to propagate to this FSUB?
1516 return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1517 DAG.getNode(ISD::ADD, dl, MVT::i32,
1518 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1519 DAG.getNode(ISD::FSUB, dl,
1523 DAG.getConstant(0x80000000, dl,
1525 DAG.getNode(ISD::FP_TO_SINT, dl,
1526 MVT::i32, N->getOperand(0)),
1530 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1531 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1532 return TLI.makeLibCall(DAG, LC, N->getValueType(0), N->getOperand(0),
1536 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1537 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1538 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1539 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1541 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1542 // against zero to select between true and false values.
1543 if (!NewRHS.getNode()) {
1544 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1545 CCCode = ISD::SETNE;
1548 // Update N to have the operands specified.
1549 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1550 N->getOperand(2), N->getOperand(3),
1551 DAG.getCondCode(CCCode)), 0);
1554 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1555 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1556 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1557 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1559 // If ExpandSetCCOperands returned a scalar, use it.
1560 if (!NewRHS.getNode()) {
1561 assert(NewLHS.getValueType() == N->getValueType(0) &&
1562 "Unexpected setcc expansion!");
1566 // Otherwise, update N to have the operands specified.
1567 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1568 DAG.getCondCode(CCCode)), 0);
1571 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1572 if (ISD::isNormalStore(N))
1573 return ExpandOp_NormalStore(N, OpNo);
1575 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1576 assert(OpNo == 1 && "Can only expand the stored value so far");
1577 StoreSDNode *ST = cast<StoreSDNode>(N);
1579 SDValue Chain = ST->getChain();
1580 SDValue Ptr = ST->getBasePtr();
1582 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1583 ST->getValue().getValueType());
1584 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1585 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1589 GetExpandedOp(ST->getValue(), Lo, Hi);
1591 return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1592 ST->getMemoryVT(), ST->getMemOperand());
1595 //===----------------------------------------------------------------------===//
1596 // Float Operand Promotion
1597 //===----------------------------------------------------------------------===//
1600 static ISD::NodeType GetPromotionOpcode(EVT OpVT, EVT RetVT) {
1601 if (OpVT == MVT::f16) {
1602 return ISD::FP16_TO_FP;
1603 } else if (RetVT == MVT::f16) {
1604 return ISD::FP_TO_FP16;
1607 report_fatal_error("Attempt at an invalid promotion-related conversion");
1610 bool DAGTypeLegalizer::PromoteFloatOperand(SDNode *N, unsigned OpNo) {
1611 SDValue R = SDValue();
1613 // Nodes that use a promotion-requiring floating point operand, but doesn't
1614 // produce a promotion-requiring floating point result, need to be legalized
1615 // to use the promoted float operand. Nodes that produce at least one
1616 // promotion-requiring floating point result have their operands legalized as
1617 // a part of PromoteFloatResult.
1618 switch (N->getOpcode()) {
1620 llvm_unreachable("Do not know how to promote this operator's operand!");
1622 case ISD::BITCAST: R = PromoteFloatOp_BITCAST(N, OpNo); break;
1623 case ISD::FCOPYSIGN: R = PromoteFloatOp_FCOPYSIGN(N, OpNo); break;
1624 case ISD::FP_TO_SINT:
1625 case ISD::FP_TO_UINT: R = PromoteFloatOp_FP_TO_XINT(N, OpNo); break;
1626 case ISD::FP_EXTEND: R = PromoteFloatOp_FP_EXTEND(N, OpNo); break;
1627 case ISD::SELECT_CC: R = PromoteFloatOp_SELECT_CC(N, OpNo); break;
1628 case ISD::SETCC: R = PromoteFloatOp_SETCC(N, OpNo); break;
1629 case ISD::STORE: R = PromoteFloatOp_STORE(N, OpNo); break;
1633 ReplaceValueWith(SDValue(N, 0), R);
1637 SDValue DAGTypeLegalizer::PromoteFloatOp_BITCAST(SDNode *N, unsigned OpNo) {
1638 SDValue Op = N->getOperand(0);
1639 EVT OpVT = Op->getValueType(0);
1641 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
1642 assert (IVT == N->getValueType(0) && "Bitcast to type of different size");
1644 SDValue Promoted = GetPromotedFloat(N->getOperand(0));
1645 EVT PromotedVT = Promoted->getValueType(0);
1647 // Convert the promoted float value to the desired IVT.
1648 return DAG.getNode(GetPromotionOpcode(PromotedVT, OpVT), SDLoc(N), IVT,
1652 // Promote Operand 1 of FCOPYSIGN. Operand 0 ought to be handled by
1653 // PromoteFloatRes_FCOPYSIGN.
1654 SDValue DAGTypeLegalizer::PromoteFloatOp_FCOPYSIGN(SDNode *N, unsigned OpNo) {
1655 assert (OpNo == 1 && "Only Operand 1 must need promotion here");
1656 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1658 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1659 N->getOperand(0), Op1);
1662 // Convert the promoted float value to the desired integer type
1663 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_TO_XINT(SDNode *N, unsigned OpNo) {
1664 SDValue Op = GetPromotedFloat(N->getOperand(0));
1665 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), Op);
1668 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_EXTEND(SDNode *N, unsigned OpNo) {
1669 SDValue Op = GetPromotedFloat(N->getOperand(0));
1670 EVT VT = N->getValueType(0);
1672 // Desired VT is same as promoted type. Use promoted float directly.
1673 if (VT == Op->getValueType(0))
1676 // Else, extend the promoted float value to the desired VT.
1677 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Op);
1680 // Promote the float operands used for comparison. The true- and false-
1681 // operands have the same type as the result and are promoted, if needed, by
1682 // PromoteFloatRes_SELECT_CC
1683 SDValue DAGTypeLegalizer::PromoteFloatOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1684 SDValue LHS = GetPromotedFloat(N->getOperand(0));
1685 SDValue RHS = GetPromotedFloat(N->getOperand(1));
1687 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1688 LHS, RHS, N->getOperand(2), N->getOperand(3),
1692 // Construct a SETCC that compares the promoted values and sets the conditional
1694 SDValue DAGTypeLegalizer::PromoteFloatOp_SETCC(SDNode *N, unsigned OpNo) {
1695 EVT VT = N->getValueType(0);
1696 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1697 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1698 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1699 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1701 return DAG.getSetCC(SDLoc(N), NVT, Op0, Op1, CCCode);
1705 // Lower the promoted Float down to the integer value of same size and construct
1706 // a STORE of the integer value.
1707 SDValue DAGTypeLegalizer::PromoteFloatOp_STORE(SDNode *N, unsigned OpNo) {
1708 StoreSDNode *ST = cast<StoreSDNode>(N);
1709 SDValue Val = ST->getValue();
1712 SDValue Promoted = GetPromotedFloat(Val);
1713 EVT VT = ST->getOperand(1)->getValueType(0);
1714 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1717 NewVal = DAG.getNode(GetPromotionOpcode(Promoted.getValueType(), VT), DL,
1720 return DAG.getStore(ST->getChain(), DL, NewVal, ST->getBasePtr(),
1721 ST->getMemOperand());
1724 //===----------------------------------------------------------------------===//
1725 // Float Result Promotion
1726 //===----------------------------------------------------------------------===//
1728 void DAGTypeLegalizer::PromoteFloatResult(SDNode *N, unsigned ResNo) {
1729 SDValue R = SDValue();
1731 switch (N->getOpcode()) {
1732 // These opcodes cannot appear if promotion of FP16 is done in the backend
1734 case ISD::FP16_TO_FP:
1735 case ISD::FP_TO_FP16:
1737 llvm_unreachable("Do not know how to promote this operator's result!");
1739 case ISD::BITCAST: R = PromoteFloatRes_BITCAST(N); break;
1740 case ISD::ConstantFP: R = PromoteFloatRes_ConstantFP(N); break;
1741 case ISD::EXTRACT_VECTOR_ELT:
1742 R = PromoteFloatRes_EXTRACT_VECTOR_ELT(N); break;
1743 case ISD::FCOPYSIGN: R = PromoteFloatRes_FCOPYSIGN(N); break;
1745 // Unary FP Operations
1755 case ISD::FNEARBYINT:
1761 case ISD::FTRUNC: R = PromoteFloatRes_UnaryOp(N); break;
1763 // Binary FP Operations
1771 case ISD::FSUB: R = PromoteFloatRes_BinOp(N); break;
1773 case ISD::FMA: // FMA is same as FMAD
1774 case ISD::FMAD: R = PromoteFloatRes_FMAD(N); break;
1776 case ISD::FPOWI: R = PromoteFloatRes_FPOWI(N); break;
1778 case ISD::FP_ROUND: R = PromoteFloatRes_FP_ROUND(N); break;
1779 case ISD::LOAD: R = PromoteFloatRes_LOAD(N); break;
1780 case ISD::SELECT: R = PromoteFloatRes_SELECT(N); break;
1781 case ISD::SELECT_CC: R = PromoteFloatRes_SELECT_CC(N); break;
1783 case ISD::SINT_TO_FP:
1784 case ISD::UINT_TO_FP: R = PromoteFloatRes_XINT_TO_FP(N); break;
1785 case ISD::UNDEF: R = PromoteFloatRes_UNDEF(N); break;
1790 SetPromotedFloat(SDValue(N, ResNo), R);
1793 // Bitcast from i16 to f16: convert the i16 to a f32 value instead.
1794 // At this point, it is not possible to determine if the bitcast value is
1795 // eventually stored to memory or promoted to f32 or promoted to a floating
1796 // point at a higher precision. Some of these cases are handled by FP_EXTEND,
1797 // STORE promotion handlers.
1798 SDValue DAGTypeLegalizer::PromoteFloatRes_BITCAST(SDNode *N) {
1799 EVT VT = N->getValueType(0);
1800 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1801 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT,
1805 SDValue DAGTypeLegalizer::PromoteFloatRes_ConstantFP(SDNode *N) {
1806 ConstantFPSDNode *CFPNode = cast<ConstantFPSDNode>(N);
1807 EVT VT = N->getValueType(0);
1810 // Get the (bit-cast) APInt of the APFloat and build an integer constant
1811 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1812 SDValue C = DAG.getConstant(CFPNode->getValueAPF().bitcastToAPInt(), DL,
1815 // Convert the Constant to the desired FP type
1816 // FIXME We might be able to do the conversion during compilation and get rid
1817 // of it from the object code
1818 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1819 return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, C);
1822 // If the Index operand is a constant, try to redirect the extract operation to
1823 // the correct legalized vector. If not, bit-convert the input vector to
1824 // equivalent integer vector. Extract the element as an (bit-cast) integer
1825 // value and convert it to the promoted type.
1826 SDValue DAGTypeLegalizer::PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
1829 // If the index is constant, try to extract the value from the legalized
1831 if (isa<ConstantSDNode>(N->getOperand(1))) {
1832 SDValue Vec = N->getOperand(0);
1833 SDValue Idx = N->getOperand(1);
1834 EVT VecVT = Vec->getValueType(0);
1835 EVT EltVT = VecVT.getVectorElementType();
1837 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1839 switch (getTypeAction(VecVT)) {
1841 case TargetLowering::TypeScalarizeVector: {
1842 SDValue Res = GetScalarizedVector(N->getOperand(0));
1843 ReplaceValueWith(SDValue(N, 0), Res);
1846 case TargetLowering::TypeWidenVector: {
1847 Vec = GetWidenedVector(Vec);
1848 SDValue Res = DAG.getNode(N->getOpcode(), DL, EltVT, Vec, Idx);
1849 ReplaceValueWith(SDValue(N, 0), Res);
1852 case TargetLowering::TypeSplitVector: {
1854 GetSplitVector(Vec, Lo, Hi);
1856 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1858 if (IdxVal < LoElts)
1859 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Lo, Idx);
1861 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Hi,
1862 DAG.getConstant(IdxVal - LoElts, DL,
1863 Idx.getValueType()));
1864 ReplaceValueWith(SDValue(N, 0), Res);
1871 // Bit-convert the input vector to the equivalent integer vector
1872 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
1873 EVT IVT = NewOp.getValueType().getVectorElementType();
1875 // Extract the element as an (bit-cast) integer value
1876 SDValue NewVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IVT,
1877 NewOp, N->getOperand(1));
1879 // Convert the element to the desired FP type
1880 EVT VT = N->getValueType(0);
1881 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1882 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, NewVal);
1885 // FCOPYSIGN(X, Y) returns the value of X with the sign of Y. If the result
1886 // needs promotion, so does the argument X. Note that Y, if needed, will be
1887 // handled during operand promotion.
1888 SDValue DAGTypeLegalizer::PromoteFloatRes_FCOPYSIGN(SDNode *N) {
1889 EVT VT = N->getValueType(0);
1890 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1891 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1893 SDValue Op1 = N->getOperand(1);
1895 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1898 // Unary operation where the result and the operand have PromoteFloat type
1899 // action. Construct a new SDNode with the promoted float value of the old
1901 SDValue DAGTypeLegalizer::PromoteFloatRes_UnaryOp(SDNode *N) {
1902 EVT VT = N->getValueType(0);
1903 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1904 SDValue Op = GetPromotedFloat(N->getOperand(0));
1906 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op);
1909 // Binary operations where the result and both operands have PromoteFloat type
1910 // action. Construct a new SDNode with the promoted float values of the old
1912 SDValue DAGTypeLegalizer::PromoteFloatRes_BinOp(SDNode *N) {
1913 EVT VT = N->getValueType(0);
1914 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1915 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1916 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1917 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, N->getFlags());
1920 SDValue DAGTypeLegalizer::PromoteFloatRes_FMAD(SDNode *N) {
1921 EVT VT = N->getValueType(0);
1922 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1923 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1924 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1925 SDValue Op2 = GetPromotedFloat(N->getOperand(2));
1927 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, Op2);
1930 // Promote the Float (first) operand and retain the Integer (second) operand
1931 SDValue DAGTypeLegalizer::PromoteFloatRes_FPOWI(SDNode *N) {
1932 EVT VT = N->getValueType(0);
1933 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1934 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1935 SDValue Op1 = N->getOperand(1);
1937 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1940 // Explicit operation to reduce precision. Reduce the value to half precision
1941 // and promote it back to the legal type.
1942 SDValue DAGTypeLegalizer::PromoteFloatRes_FP_ROUND(SDNode *N) {
1945 SDValue Op = N->getOperand(0);
1946 EVT VT = N->getValueType(0);
1947 EVT OpVT = Op->getValueType(0);
1948 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1949 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1951 // Round promoted float to desired precision
1952 SDValue Round = DAG.getNode(GetPromotionOpcode(OpVT, VT), DL, IVT, Op);
1953 // Promote it back to the legal output type
1954 return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, Round);
1957 SDValue DAGTypeLegalizer::PromoteFloatRes_LOAD(SDNode *N) {
1958 LoadSDNode *L = cast<LoadSDNode>(N);
1959 EVT VT = N->getValueType(0);
1961 // Load the value as an integer value with the same number of bits
1962 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1963 SDValue newL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
1964 IVT, SDLoc(N), L->getChain(), L->getBasePtr(),
1965 L->getOffset(), L->getPointerInfo(), IVT, L->isVolatile(),
1966 L->isNonTemporal(), false, L->getAlignment(),
1968 // Legalize the chain result by replacing uses of the old value chain with the
1970 ReplaceValueWith(SDValue(N, 1), newL.getValue(1));
1972 // Convert the integer value to the desired FP type
1973 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1974 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, newL);
1977 // Construct a new SELECT node with the promoted true- and false- values.
1978 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT(SDNode *N) {
1979 SDValue TrueVal = GetPromotedFloat(N->getOperand(1));
1980 SDValue FalseVal = GetPromotedFloat(N->getOperand(2));
1982 return DAG.getNode(ISD::SELECT, SDLoc(N), TrueVal->getValueType(0),
1983 N->getOperand(0), TrueVal, FalseVal);
1986 // Construct a new SELECT_CC node with the promoted true- and false- values.
1987 // The operands used for comparison are promoted by PromoteFloatOp_SELECT_CC.
1988 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT_CC(SDNode *N) {
1989 SDValue TrueVal = GetPromotedFloat(N->getOperand(2));
1990 SDValue FalseVal = GetPromotedFloat(N->getOperand(3));
1992 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1993 N->getOperand(0), N->getOperand(1), TrueVal, FalseVal,
1997 // Construct a SDNode that transforms the SINT or UINT operand to the promoted
1999 SDValue DAGTypeLegalizer::PromoteFloatRes_XINT_TO_FP(SDNode *N) {
2000 EVT VT = N->getValueType(0);
2001 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2002 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, N->getOperand(0));
2005 SDValue DAGTypeLegalizer::PromoteFloatRes_UNDEF(SDNode *N) {
2006 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
2007 N->getValueType(0)));