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(),
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, NVT);
154 SDValue Op = GetSoftenedFloat(N->getOperand(0));
155 return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
158 SDValue DAGTypeLegalizer::SoftenFloatRes_FMINNUM(SDNode *N) {
159 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
160 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
161 GetSoftenedFloat(N->getOperand(1)) };
162 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
167 RTLIB::FMIN_PPCF128),
168 NVT, Ops, 2, false, SDLoc(N)).first;
171 SDValue DAGTypeLegalizer::SoftenFloatRes_FMAXNUM(SDNode *N) {
172 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
173 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
174 GetSoftenedFloat(N->getOperand(1)) };
175 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
180 RTLIB::FMAX_PPCF128),
181 NVT, Ops, 2, false, SDLoc(N)).first;
184 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
185 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
186 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
187 GetSoftenedFloat(N->getOperand(1)) };
188 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
194 NVT, Ops, 2, false, SDLoc(N)).first;
197 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
198 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
199 SDValue Op = GetSoftenedFloat(N->getOperand(0));
200 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
205 RTLIB::CEIL_PPCF128),
206 NVT, &Op, 1, false, SDLoc(N)).first;
209 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
210 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
211 SDValue RHS = BitConvertToInteger(N->getOperand(1));
214 EVT LVT = LHS.getValueType();
215 EVT RVT = RHS.getValueType();
217 unsigned LSize = LVT.getSizeInBits();
218 unsigned RSize = RVT.getSizeInBits();
220 // First get the sign bit of second operand.
221 SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
222 DAG.getConstant(RSize - 1,
223 TLI.getShiftAmountTy(RVT)));
224 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
226 // Shift right or sign-extend it if the two operands have different types.
227 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
229 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
230 DAG.getConstant(SizeDiff,
231 TLI.getShiftAmountTy(SignBit.getValueType())));
232 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
233 } else if (SizeDiff < 0) {
234 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
235 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
236 DAG.getConstant(-SizeDiff,
237 TLI.getShiftAmountTy(SignBit.getValueType())));
240 // Clear the sign bit of the first operand.
241 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
242 DAG.getConstant(LSize - 1,
243 TLI.getShiftAmountTy(LVT)));
244 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
245 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
247 // Or the value with the sign bit.
248 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
251 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
252 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
253 SDValue Op = GetSoftenedFloat(N->getOperand(0));
254 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
260 NVT, &Op, 1, false, SDLoc(N)).first;
263 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
264 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
265 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
266 GetSoftenedFloat(N->getOperand(1)) };
267 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
273 NVT, Ops, 2, false, SDLoc(N)).first;
276 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
277 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
278 SDValue Op = GetSoftenedFloat(N->getOperand(0));
279 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
285 NVT, &Op, 1, false, SDLoc(N)).first;
288 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
289 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
290 SDValue Op = GetSoftenedFloat(N->getOperand(0));
291 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
296 RTLIB::EXP2_PPCF128),
297 NVT, &Op, 1, false, SDLoc(N)).first;
300 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
301 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
302 SDValue Op = GetSoftenedFloat(N->getOperand(0));
303 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
308 RTLIB::FLOOR_PPCF128),
309 NVT, &Op, 1, false, SDLoc(N)).first;
312 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
313 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
314 SDValue Op = GetSoftenedFloat(N->getOperand(0));
315 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
321 NVT, &Op, 1, false, SDLoc(N)).first;
324 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
325 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
326 SDValue Op = GetSoftenedFloat(N->getOperand(0));
327 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
332 RTLIB::LOG2_PPCF128),
333 NVT, &Op, 1, false, SDLoc(N)).first;
336 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
337 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
338 SDValue Op = GetSoftenedFloat(N->getOperand(0));
339 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
344 RTLIB::LOG10_PPCF128),
345 NVT, &Op, 1, false, SDLoc(N)).first;
348 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
349 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
350 SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
351 GetSoftenedFloat(N->getOperand(1)),
352 GetSoftenedFloat(N->getOperand(2)) };
353 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
359 NVT, Ops, 3, false, SDLoc(N)).first;
362 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
363 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
364 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
365 GetSoftenedFloat(N->getOperand(1)) };
366 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
372 NVT, Ops, 2, false, SDLoc(N)).first;
375 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
376 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
377 SDValue Op = GetSoftenedFloat(N->getOperand(0));
378 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
379 RTLIB::NEARBYINT_F32,
380 RTLIB::NEARBYINT_F64,
381 RTLIB::NEARBYINT_F80,
382 RTLIB::NEARBYINT_F128,
383 RTLIB::NEARBYINT_PPCF128),
384 NVT, &Op, 1, false, SDLoc(N)).first;
387 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
388 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
389 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
390 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
391 GetSoftenedFloat(N->getOperand(0)) };
392 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
398 NVT, Ops, 2, false, SDLoc(N)).first;
401 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
402 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
403 SDValue Op = N->getOperand(0);
405 // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
406 // entirely possible for both f16 and f32 to be legal, so use the fully
407 // hard-float FP_EXTEND rather than FP16_TO_FP.
408 if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
409 Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
410 if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
411 SoftenFloatResult(Op.getNode(), 0);
414 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
415 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
416 Op = GetSoftenedFloat(Op);
417 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
418 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
421 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
423 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
424 EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
425 SDValue Op = N->getOperand(0);
426 SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, &Op, 1,
427 false, SDLoc(N)).first;
428 if (N->getValueType(0) == MVT::f32)
431 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
432 RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
433 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
434 return TLI.makeLibCall(DAG, LC, NVT, &Res32, 1, false, SDLoc(N)).first;
437 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
438 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
439 SDValue Op = N->getOperand(0);
440 if (N->getValueType(0) == MVT::f16) {
441 // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
442 // storage-only type get a chance to select things.
443 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
446 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
447 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
448 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
451 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
452 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
453 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
454 GetSoftenedFloat(N->getOperand(1)) };
455 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
461 NVT, Ops, 2, false, SDLoc(N)).first;
464 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
465 assert(N->getOperand(1).getValueType() == MVT::i32 &&
466 "Unsupported power type!");
467 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
468 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
469 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
474 RTLIB::POWI_PPCF128),
475 NVT, Ops, 2, false, SDLoc(N)).first;
478 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
479 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
480 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
481 GetSoftenedFloat(N->getOperand(1)) };
482 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
488 NVT, Ops, 2, false, SDLoc(N)).first;
491 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
492 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
493 SDValue Op = GetSoftenedFloat(N->getOperand(0));
494 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
499 RTLIB::RINT_PPCF128),
500 NVT, &Op, 1, false, SDLoc(N)).first;
503 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
504 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
505 SDValue Op = GetSoftenedFloat(N->getOperand(0));
506 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
511 RTLIB::ROUND_PPCF128),
512 NVT, &Op, 1, false, SDLoc(N)).first;
515 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
516 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
517 SDValue Op = GetSoftenedFloat(N->getOperand(0));
518 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
524 NVT, &Op, 1, false, SDLoc(N)).first;
527 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
528 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
529 SDValue Op = GetSoftenedFloat(N->getOperand(0));
530 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
535 RTLIB::SQRT_PPCF128),
536 NVT, &Op, 1, false, SDLoc(N)).first;
539 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
540 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
541 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
542 GetSoftenedFloat(N->getOperand(1)) };
543 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
549 NVT, Ops, 2, false, SDLoc(N)).first;
552 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
553 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
554 if (N->getValueType(0) == MVT::f16)
555 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
557 SDValue Op = GetSoftenedFloat(N->getOperand(0));
558 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
563 RTLIB::TRUNC_PPCF128),
564 NVT, &Op, 1, false, SDLoc(N)).first;
567 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
568 LoadSDNode *L = cast<LoadSDNode>(N);
569 EVT VT = N->getValueType(0);
570 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
574 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
575 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
576 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
577 L->getPointerInfo(), NVT, L->isVolatile(),
578 L->isNonTemporal(), false, L->getAlignment(),
580 // Legalized the chain result - switch anything that used the old chain to
582 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
586 // Do a non-extending load followed by FP_EXTEND.
587 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
588 L->getMemoryVT(), dl, L->getChain(),
589 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
590 L->getMemoryVT(), L->isVolatile(),
591 L->isNonTemporal(), false, L->getAlignment(),
593 // Legalized the chain result - switch anything that used the old chain to
595 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
596 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
599 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
600 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
601 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
602 return DAG.getSelect(SDLoc(N),
603 LHS.getValueType(), N->getOperand(0), LHS, RHS);
606 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
607 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
608 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
609 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
610 LHS.getValueType(), N->getOperand(0),
611 N->getOperand(1), LHS, RHS, N->getOperand(4));
614 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
615 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
616 N->getValueType(0)));
619 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
620 SDValue Chain = N->getOperand(0); // Get the chain.
621 SDValue Ptr = N->getOperand(1); // Get the pointer.
622 EVT VT = N->getValueType(0);
623 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
627 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
628 N->getConstantOperandVal(3));
630 // Legalized the chain result - switch anything that used the old chain to
632 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
636 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
637 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
638 EVT SVT = N->getOperand(0).getValueType();
639 EVT RVT = N->getValueType(0);
643 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
644 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
645 // match. Look for an appropriate libcall.
646 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
647 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
648 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
649 NVT = (MVT::SimpleValueType)t;
650 // The source needs to big enough to hold the operand.
652 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
654 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
656 // Sign/zero extend the argument if the libcall takes a larger type.
657 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
658 NVT, N->getOperand(0));
659 return TLI.makeLibCall(DAG, LC,
660 TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
661 &Op, 1, Signed, dl).first;
665 //===----------------------------------------------------------------------===//
666 // Operand Float to Integer Conversion..
667 //===----------------------------------------------------------------------===//
669 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
670 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
672 SDValue Res = SDValue();
674 switch (N->getOpcode()) {
677 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
678 N->dump(&DAG); dbgs() << "\n";
680 llvm_unreachable("Do not know how to soften this operator's operand!");
682 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
683 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
684 case ISD::FP_EXTEND: Res = SoftenFloatOp_FP_EXTEND(N); break;
685 case ISD::FP_TO_FP16: // Same as FP_ROUND for softening purposes
686 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
687 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
688 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
689 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
690 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
691 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
694 // If the result is null, the sub-method took care of registering results etc.
695 if (!Res.getNode()) return false;
697 // If the result is N, the sub-method updated N in place. Tell the legalizer
699 if (Res.getNode() == N)
702 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
703 "Invalid operand expansion");
705 ReplaceValueWith(SDValue(N, 0), Res);
709 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
710 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
711 GetSoftenedFloat(N->getOperand(0)));
714 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
715 // If we get here, the result must be legal but the source illegal.
716 EVT SVT = N->getOperand(0).getValueType();
717 EVT RVT = N->getValueType(0);
718 SDValue Op = GetSoftenedFloat(N->getOperand(0));
721 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
723 RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
724 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
726 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
730 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
731 // We actually deal with the partially-softened FP_TO_FP16 node too, which
732 // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
733 assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
735 EVT SVT = N->getOperand(0).getValueType();
736 EVT RVT = N->getValueType(0);
737 EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
739 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
740 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
742 SDValue Op = GetSoftenedFloat(N->getOperand(0));
743 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
746 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
747 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
748 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
750 EVT VT = NewLHS.getValueType();
751 NewLHS = GetSoftenedFloat(NewLHS);
752 NewRHS = GetSoftenedFloat(NewRHS);
753 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
755 // If softenSetCCOperands returned a scalar, we need to compare the result
756 // against zero to select between true and false values.
757 if (!NewRHS.getNode()) {
758 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
762 // Update N to have the operands specified.
763 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
764 DAG.getCondCode(CCCode), NewLHS, NewRHS,
769 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
770 EVT RVT = N->getValueType(0);
771 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
772 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
773 SDValue Op = GetSoftenedFloat(N->getOperand(0));
774 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
777 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
778 EVT RVT = N->getValueType(0);
779 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
780 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
781 SDValue Op = GetSoftenedFloat(N->getOperand(0));
782 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
785 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
786 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
787 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
789 EVT VT = NewLHS.getValueType();
790 NewLHS = GetSoftenedFloat(NewLHS);
791 NewRHS = GetSoftenedFloat(NewRHS);
792 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
794 // If softenSetCCOperands returned a scalar, we need to compare the result
795 // against zero to select between true and false values.
796 if (!NewRHS.getNode()) {
797 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
801 // Update N to have the operands specified.
802 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
803 N->getOperand(2), N->getOperand(3),
804 DAG.getCondCode(CCCode)),
808 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
809 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
810 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
812 EVT VT = NewLHS.getValueType();
813 NewLHS = GetSoftenedFloat(NewLHS);
814 NewRHS = GetSoftenedFloat(NewRHS);
815 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
817 // If softenSetCCOperands returned a scalar, use it.
818 if (!NewRHS.getNode()) {
819 assert(NewLHS.getValueType() == N->getValueType(0) &&
820 "Unexpected setcc expansion!");
824 // Otherwise, update N to have the operands specified.
825 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
826 DAG.getCondCode(CCCode)),
830 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
831 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
832 assert(OpNo == 1 && "Can only soften the stored value!");
833 StoreSDNode *ST = cast<StoreSDNode>(N);
834 SDValue Val = ST->getValue();
837 if (ST->isTruncatingStore())
838 // Do an FP_ROUND followed by a non-truncating store.
839 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
840 Val, DAG.getIntPtrConstant(0)));
842 Val = GetSoftenedFloat(Val);
844 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
845 ST->getMemOperand());
849 //===----------------------------------------------------------------------===//
850 // Float Result Expansion
851 //===----------------------------------------------------------------------===//
853 /// ExpandFloatResult - This method is called when the specified result of the
854 /// specified node is found to need expansion. At this point, the node may also
855 /// have invalid operands or may have other results that need promotion, we just
856 /// know that (at least) one result needs expansion.
857 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
858 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
862 // See if the target wants to custom expand this node.
863 if (CustomLowerNode(N, N->getValueType(ResNo), true))
866 switch (N->getOpcode()) {
869 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
870 N->dump(&DAG); dbgs() << "\n";
872 llvm_unreachable("Do not know how to expand the result of this operator!");
874 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
875 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
876 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
878 case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
879 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
880 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
881 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
882 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
883 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
885 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
886 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
887 case ISD::FMINNUM: ExpandFloatRes_FMINNUM(N, Lo, Hi); break;
888 case ISD::FMAXNUM: ExpandFloatRes_FMAXNUM(N, Lo, Hi); break;
889 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
890 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
891 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
892 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
893 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
894 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
895 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
896 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
897 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
898 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
899 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
900 case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
901 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
902 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
903 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
904 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
905 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
906 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
907 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
908 case ISD::FROUND: ExpandFloatRes_FROUND(N, Lo, Hi); break;
909 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
910 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
911 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
912 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
913 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
914 case ISD::SINT_TO_FP:
915 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
916 case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
919 // If Lo/Hi is null, the sub-method took care of registering results etc.
921 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
924 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
926 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
927 assert(NVT.getSizeInBits() == integerPartWidth &&
928 "Do not know how to expand this float constant!");
929 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
930 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
931 APInt(integerPartWidth, C.getRawData()[1])),
933 Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
934 APInt(integerPartWidth, C.getRawData()[0])),
938 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
940 assert(N->getValueType(0) == MVT::ppcf128 &&
941 "Logic only correct for ppcf128!");
944 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
945 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
946 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
947 Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
948 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
952 void DAGTypeLegalizer::ExpandFloatRes_FMINNUM(SDNode *N, SDValue &Lo,
954 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
955 RTLIB::FMIN_F32, RTLIB::FMIN_F64,
956 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
957 RTLIB::FMIN_PPCF128),
959 GetPairElements(Call, Lo, Hi);
962 void DAGTypeLegalizer::ExpandFloatRes_FMAXNUM(SDNode *N, SDValue &Lo,
964 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
965 RTLIB::FMAX_F32, RTLIB::FMAX_F64,
966 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
967 RTLIB::FMAX_PPCF128),
969 GetPairElements(Call, Lo, Hi);
972 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
974 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
975 RTLIB::ADD_F32, RTLIB::ADD_F64,
976 RTLIB::ADD_F80, RTLIB::ADD_F128,
979 GetPairElements(Call, Lo, Hi);
982 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
983 SDValue &Lo, SDValue &Hi) {
984 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
985 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
986 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
987 RTLIB::CEIL_PPCF128),
989 GetPairElements(Call, Lo, Hi);
992 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
993 SDValue &Lo, SDValue &Hi) {
994 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
998 RTLIB::COPYSIGN_F128,
999 RTLIB::COPYSIGN_PPCF128),
1001 GetPairElements(Call, Lo, Hi);
1004 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
1005 SDValue &Lo, SDValue &Hi) {
1006 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1007 RTLIB::COS_F32, RTLIB::COS_F64,
1008 RTLIB::COS_F80, RTLIB::COS_F128,
1009 RTLIB::COS_PPCF128),
1011 GetPairElements(Call, Lo, Hi);
1014 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
1016 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1017 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1022 RTLIB::DIV_PPCF128),
1023 N->getValueType(0), Ops, 2, false,
1025 GetPairElements(Call, Lo, Hi);
1028 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
1029 SDValue &Lo, SDValue &Hi) {
1030 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1031 RTLIB::EXP_F32, RTLIB::EXP_F64,
1032 RTLIB::EXP_F80, RTLIB::EXP_F128,
1033 RTLIB::EXP_PPCF128),
1035 GetPairElements(Call, Lo, Hi);
1038 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
1039 SDValue &Lo, SDValue &Hi) {
1040 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1041 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
1042 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
1043 RTLIB::EXP2_PPCF128),
1045 GetPairElements(Call, Lo, Hi);
1048 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
1049 SDValue &Lo, SDValue &Hi) {
1050 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1051 RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
1052 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
1053 RTLIB::FLOOR_PPCF128),
1055 GetPairElements(Call, Lo, Hi);
1058 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1059 SDValue &Lo, SDValue &Hi) {
1060 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1061 RTLIB::LOG_F32, RTLIB::LOG_F64,
1062 RTLIB::LOG_F80, RTLIB::LOG_F128,
1063 RTLIB::LOG_PPCF128),
1065 GetPairElements(Call, Lo, Hi);
1068 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1069 SDValue &Lo, SDValue &Hi) {
1070 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1071 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1072 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1073 RTLIB::LOG2_PPCF128),
1075 GetPairElements(Call, Lo, Hi);
1078 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1079 SDValue &Lo, SDValue &Hi) {
1080 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1081 RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1082 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1083 RTLIB::LOG10_PPCF128),
1085 GetPairElements(Call, Lo, Hi);
1088 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1090 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1091 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1096 RTLIB::FMA_PPCF128),
1097 N->getValueType(0), Ops, 3, false,
1099 GetPairElements(Call, Lo, Hi);
1102 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1104 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1105 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1110 RTLIB::MUL_PPCF128),
1111 N->getValueType(0), Ops, 2, false,
1113 GetPairElements(Call, Lo, Hi);
1116 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1117 SDValue &Lo, SDValue &Hi) {
1118 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1119 RTLIB::NEARBYINT_F32,
1120 RTLIB::NEARBYINT_F64,
1121 RTLIB::NEARBYINT_F80,
1122 RTLIB::NEARBYINT_F128,
1123 RTLIB::NEARBYINT_PPCF128),
1125 GetPairElements(Call, Lo, Hi);
1128 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1131 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1132 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1133 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1136 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1138 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1139 Hi = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), NVT, N->getOperand(0));
1140 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1141 APInt(NVT.getSizeInBits(), 0)), NVT);
1144 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1145 SDValue &Lo, SDValue &Hi) {
1146 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1147 RTLIB::POW_F32, RTLIB::POW_F64,
1148 RTLIB::POW_F80, RTLIB::POW_F128,
1149 RTLIB::POW_PPCF128),
1151 GetPairElements(Call, Lo, Hi);
1154 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1155 SDValue &Lo, SDValue &Hi) {
1156 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1157 RTLIB::POWI_F32, RTLIB::POWI_F64,
1158 RTLIB::POWI_F80, RTLIB::POWI_F128,
1159 RTLIB::POWI_PPCF128),
1161 GetPairElements(Call, Lo, Hi);
1164 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1165 SDValue &Lo, SDValue &Hi) {
1166 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1167 RTLIB::REM_F32, RTLIB::REM_F64,
1168 RTLIB::REM_F80, RTLIB::REM_F128,
1169 RTLIB::REM_PPCF128),
1171 GetPairElements(Call, Lo, Hi);
1174 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1175 SDValue &Lo, SDValue &Hi) {
1176 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1177 RTLIB::RINT_F32, RTLIB::RINT_F64,
1178 RTLIB::RINT_F80, RTLIB::RINT_F128,
1179 RTLIB::RINT_PPCF128),
1181 GetPairElements(Call, Lo, Hi);
1184 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1185 SDValue &Lo, SDValue &Hi) {
1186 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1191 RTLIB::ROUND_PPCF128),
1193 GetPairElements(Call, Lo, Hi);
1196 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1197 SDValue &Lo, SDValue &Hi) {
1198 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1199 RTLIB::SIN_F32, RTLIB::SIN_F64,
1200 RTLIB::SIN_F80, RTLIB::SIN_F128,
1201 RTLIB::SIN_PPCF128),
1203 GetPairElements(Call, Lo, Hi);
1206 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1207 SDValue &Lo, SDValue &Hi) {
1208 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1209 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1210 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1211 RTLIB::SQRT_PPCF128),
1213 GetPairElements(Call, Lo, Hi);
1216 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1218 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1219 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1224 RTLIB::SUB_PPCF128),
1225 N->getValueType(0), Ops, 2, false,
1227 GetPairElements(Call, Lo, Hi);
1230 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1231 SDValue &Lo, SDValue &Hi) {
1232 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1233 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1234 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1235 RTLIB::TRUNC_PPCF128),
1237 GetPairElements(Call, Lo, Hi);
1240 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1242 if (ISD::isNormalLoad(N)) {
1243 ExpandRes_NormalLoad(N, Lo, Hi);
1247 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1248 LoadSDNode *LD = cast<LoadSDNode>(N);
1249 SDValue Chain = LD->getChain();
1250 SDValue Ptr = LD->getBasePtr();
1253 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1254 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1255 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1257 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1258 LD->getMemoryVT(), LD->getMemOperand());
1260 // Remember the chain.
1261 Chain = Hi.getValue(1);
1263 // The low part is zero.
1264 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1265 APInt(NVT.getSizeInBits(), 0)), NVT);
1267 // Modified the chain - switch anything that used the old chain to use the
1269 ReplaceValueWith(SDValue(LD, 1), Chain);
1272 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1274 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1275 EVT VT = N->getValueType(0);
1276 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1277 SDValue Src = N->getOperand(0);
1278 EVT SrcVT = Src.getValueType();
1279 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1282 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1283 // When promoting partial word types to i32 we must honor the signedness,
1285 if (SrcVT.bitsLE(MVT::i32)) {
1286 // The integer can be represented exactly in an f64.
1287 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1289 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1290 APInt(NVT.getSizeInBits(), 0)), NVT);
1291 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1293 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1294 if (SrcVT.bitsLE(MVT::i64)) {
1295 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1297 LC = RTLIB::SINTTOFP_I64_PPCF128;
1298 } else if (SrcVT.bitsLE(MVT::i128)) {
1299 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1300 LC = RTLIB::SINTTOFP_I128_PPCF128;
1302 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1304 Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl).first;
1305 GetPairElements(Hi, Lo, Hi);
1311 // Unsigned - fix up the SINT_TO_FP value just calculated.
1312 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1313 SrcVT = Src.getValueType();
1315 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1316 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1317 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1318 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1319 ArrayRef<uint64_t> Parts;
1321 switch (SrcVT.getSimpleVT().SimpleTy) {
1323 llvm_unreachable("Unsupported UINT_TO_FP!");
1335 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1336 DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1339 Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, SrcVT),
1340 Lo, Hi, ISD::SETLT);
1341 GetPairElements(Lo, Lo, Hi);
1345 //===----------------------------------------------------------------------===//
1346 // Float Operand Expansion
1347 //===----------------------------------------------------------------------===//
1349 /// ExpandFloatOperand - This method is called when the specified operand of the
1350 /// specified node is found to need expansion. At this point, all of the result
1351 /// types of the node are known to be legal, but other operands of the node may
1352 /// need promotion or expansion as well as the specified one.
1353 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1354 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1355 SDValue Res = SDValue();
1357 // See if the target wants to custom expand this node.
1358 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1361 switch (N->getOpcode()) {
1364 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1365 N->dump(&DAG); dbgs() << "\n";
1367 llvm_unreachable("Do not know how to expand this operator's operand!");
1369 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1370 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1371 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1373 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1374 case ISD::FCOPYSIGN: Res = ExpandFloatOp_FCOPYSIGN(N); break;
1375 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1376 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1377 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1378 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1379 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1380 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1384 // If the result is null, the sub-method took care of registering results etc.
1385 if (!Res.getNode()) return false;
1387 // If the result is N, the sub-method updated N in place. Tell the legalizer
1389 if (Res.getNode() == N)
1392 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1393 "Invalid operand expansion");
1395 ReplaceValueWith(SDValue(N, 0), Res);
1399 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1400 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1401 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1403 ISD::CondCode &CCCode,
1405 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1406 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1407 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1409 assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1411 // FIXME: This generated code sucks. We want to generate
1412 // FCMPU crN, hi1, hi2
1414 // FCMPU crN, lo1, lo2
1415 // The following can be improved, but not that much.
1416 SDValue Tmp1, Tmp2, Tmp3;
1417 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1418 LHSHi, RHSHi, ISD::SETOEQ);
1419 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1420 LHSLo, RHSLo, CCCode);
1421 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1422 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1423 LHSHi, RHSHi, ISD::SETUNE);
1424 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1425 LHSHi, RHSHi, CCCode);
1426 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1427 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1428 NewRHS = SDValue(); // LHS is the result, not a compare.
1431 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1432 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1433 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1434 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1436 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1437 // against zero to select between true and false values.
1438 if (!NewRHS.getNode()) {
1439 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1440 CCCode = ISD::SETNE;
1443 // Update N to have the operands specified.
1444 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1445 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1446 N->getOperand(4)), 0);
1449 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1450 assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1451 "Logic only correct for ppcf128!");
1453 GetExpandedFloat(N->getOperand(1), Lo, Hi);
1454 // The ppcf128 value is providing only the sign; take it from the
1455 // higher-order double (which must have the larger magnitude).
1456 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1457 N->getValueType(0), N->getOperand(0), Hi);
1460 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1461 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1462 "Logic only correct for ppcf128!");
1464 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1465 // Round it the rest of the way (e.g. to f32) if needed.
1466 return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1467 N->getValueType(0), Hi, N->getOperand(1));
1470 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1471 EVT RVT = N->getValueType(0);
1474 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1475 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1476 if (RVT == MVT::i32) {
1477 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1478 "Logic only correct for ppcf128!");
1479 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1480 N->getOperand(0), DAG.getValueType(MVT::f64));
1481 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1482 DAG.getIntPtrConstant(1));
1483 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1486 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1487 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1488 return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl).first;
1491 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1492 EVT RVT = N->getValueType(0);
1495 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1496 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1497 if (RVT == MVT::i32) {
1498 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1499 "Logic only correct for ppcf128!");
1500 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1501 APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1502 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1503 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1504 // FIXME: generated code sucks.
1505 return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1506 DAG.getNode(ISD::ADD, dl, MVT::i32,
1507 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1508 DAG.getNode(ISD::FSUB, dl,
1512 DAG.getConstant(0x80000000, MVT::i32)),
1513 DAG.getNode(ISD::FP_TO_SINT, dl,
1514 MVT::i32, N->getOperand(0)),
1518 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1519 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1520 return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1524 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1525 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1526 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1527 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1529 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1530 // against zero to select between true and false values.
1531 if (!NewRHS.getNode()) {
1532 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1533 CCCode = ISD::SETNE;
1536 // Update N to have the operands specified.
1537 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1538 N->getOperand(2), N->getOperand(3),
1539 DAG.getCondCode(CCCode)), 0);
1542 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1543 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1544 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1545 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1547 // If ExpandSetCCOperands returned a scalar, use it.
1548 if (!NewRHS.getNode()) {
1549 assert(NewLHS.getValueType() == N->getValueType(0) &&
1550 "Unexpected setcc expansion!");
1554 // Otherwise, update N to have the operands specified.
1555 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1556 DAG.getCondCode(CCCode)), 0);
1559 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1560 if (ISD::isNormalStore(N))
1561 return ExpandOp_NormalStore(N, OpNo);
1563 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1564 assert(OpNo == 1 && "Can only expand the stored value so far");
1565 StoreSDNode *ST = cast<StoreSDNode>(N);
1567 SDValue Chain = ST->getChain();
1568 SDValue Ptr = ST->getBasePtr();
1570 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1571 ST->getValue().getValueType());
1572 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1573 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1577 GetExpandedOp(ST->getValue(), Lo, Hi);
1579 return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1580 ST->getMemoryVT(), ST->getMemOperand());
1583 //===----------------------------------------------------------------------===//
1584 // Float Operand Promotion
1585 //===----------------------------------------------------------------------===//
1588 static ISD::NodeType GetPromotionOpcode(EVT OpVT, EVT RetVT) {
1589 if (OpVT == MVT::f16) {
1590 return ISD::FP16_TO_FP;
1591 } else if (RetVT == MVT::f16) {
1592 return ISD::FP_TO_FP16;
1595 report_fatal_error("Attempt at an invalid promotion-related conversion");
1598 bool DAGTypeLegalizer::PromoteFloatOperand(SDNode *N, unsigned OpNo) {
1599 SDValue R = SDValue();
1601 // Nodes that use a promotion-requiring floating point operand, but doesn't
1602 // produce a promotion-requiring floating point result, need to be legalized
1603 // to use the promoted float operand. Nodes that produce at least one
1604 // promotion-requiring floating point result have their operands legalized as
1605 // a part of PromoteFloatResult.
1606 switch (N->getOpcode()) {
1608 llvm_unreachable("Do not know how to promote this operator's operand!");
1610 case ISD::BITCAST: R = PromoteFloatOp_BITCAST(N, OpNo); break;
1611 case ISD::FCOPYSIGN: R = PromoteFloatOp_FCOPYSIGN(N, OpNo); break;
1612 case ISD::FP_TO_SINT:
1613 case ISD::FP_TO_UINT: R = PromoteFloatOp_FP_TO_XINT(N, OpNo); break;
1614 case ISD::FP_EXTEND: R = PromoteFloatOp_FP_EXTEND(N, OpNo); break;
1615 case ISD::SELECT_CC: R = PromoteFloatOp_SELECT_CC(N, OpNo); break;
1616 case ISD::SETCC: R = PromoteFloatOp_SETCC(N, OpNo); break;
1617 case ISD::STORE: R = PromoteFloatOp_STORE(N, OpNo); break;
1621 ReplaceValueWith(SDValue(N, 0), R);
1625 SDValue DAGTypeLegalizer::PromoteFloatOp_BITCAST(SDNode *N, unsigned OpNo) {
1626 SDValue Op = N->getOperand(0);
1627 EVT OpVT = Op->getValueType(0);
1629 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
1630 assert (IVT == N->getValueType(0) && "Bitcast to type of different size");
1632 SDValue Promoted = GetPromotedFloat(N->getOperand(0));
1633 EVT PromotedVT = Promoted->getValueType(0);
1635 // Convert the promoted float value to the desired IVT.
1636 return DAG.getNode(GetPromotionOpcode(PromotedVT, OpVT), SDLoc(N), IVT,
1640 // Promote Operand 1 of FCOPYSIGN. Operand 0 ought to be handled by
1641 // PromoteFloatRes_FCOPYSIGN.
1642 SDValue DAGTypeLegalizer::PromoteFloatOp_FCOPYSIGN(SDNode *N, unsigned OpNo) {
1643 assert (OpNo == 1 && "Only Operand 1 must need promotion here");
1644 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1646 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1647 N->getOperand(0), Op1);
1650 // Convert the promoted float value to the desired integer type
1651 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_TO_XINT(SDNode *N, unsigned OpNo) {
1652 SDValue Op = GetPromotedFloat(N->getOperand(0));
1653 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), Op);
1656 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_EXTEND(SDNode *N, unsigned OpNo) {
1657 SDValue Op = GetPromotedFloat(N->getOperand(0));
1658 EVT VT = N->getValueType(0);
1660 // Desired VT is same as promoted type. Use promoted float directly.
1661 if (VT == Op->getValueType(0))
1664 // Else, extend the promoted float value to the desired VT.
1665 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Op);
1668 // Promote the float operands used for comparison. The true- and false-
1669 // operands have the same type as the result and are promoted, if needed, by
1670 // PromoteFloatRes_SELECT_CC
1671 SDValue DAGTypeLegalizer::PromoteFloatOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1672 SDValue LHS = GetPromotedFloat(N->getOperand(0));
1673 SDValue RHS = GetPromotedFloat(N->getOperand(1));
1675 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1676 LHS, RHS, N->getOperand(2), N->getOperand(3),
1680 // Construct a SETCC that compares the promoted values and sets the conditional
1682 SDValue DAGTypeLegalizer::PromoteFloatOp_SETCC(SDNode *N, unsigned OpNo) {
1683 EVT VT = N->getValueType(0);
1684 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1685 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1686 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1687 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1689 return DAG.getSetCC(SDLoc(N), NVT, Op0, Op1, CCCode);
1693 // Lower the promoted Float down to the integer value of same size and construct
1694 // a STORE of the integer value.
1695 SDValue DAGTypeLegalizer::PromoteFloatOp_STORE(SDNode *N, unsigned OpNo) {
1696 StoreSDNode *ST = cast<StoreSDNode>(N);
1697 SDValue Val = ST->getValue();
1700 SDValue Promoted = GetPromotedFloat(Val);
1701 EVT VT = ST->getOperand(1)->getValueType(0);
1702 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1705 NewVal = DAG.getNode(GetPromotionOpcode(Promoted.getValueType(), VT), DL,
1708 return DAG.getStore(ST->getChain(), DL, NewVal, ST->getBasePtr(),
1709 ST->getMemOperand());
1712 //===----------------------------------------------------------------------===//
1713 // Float Result Promotion
1714 //===----------------------------------------------------------------------===//
1716 void DAGTypeLegalizer::PromoteFloatResult(SDNode *N, unsigned ResNo) {
1717 SDValue R = SDValue();
1719 switch (N->getOpcode()) {
1720 // These opcodes cannot appear if promotion of FP16 is done in the backend
1722 case ISD::FP16_TO_FP:
1723 case ISD::FP_TO_FP16:
1725 llvm_unreachable("Do not know how to promote this operator's result!");
1727 case ISD::BITCAST: R = PromoteFloatRes_BITCAST(N); break;
1728 case ISD::ConstantFP: R = PromoteFloatRes_ConstantFP(N); break;
1729 case ISD::EXTRACT_VECTOR_ELT:
1730 R = PromoteFloatRes_EXTRACT_VECTOR_ELT(N); break;
1731 case ISD::FCOPYSIGN: R = PromoteFloatRes_FCOPYSIGN(N); break;
1733 // Unary FP Operations
1743 case ISD::FNEARBYINT:
1749 case ISD::FTRUNC: R = PromoteFloatRes_UnaryOp(N); break;
1751 // Binary FP Operations
1759 case ISD::FSUB: R = PromoteFloatRes_BinOp(N); break;
1761 case ISD::FMA: // FMA is same as FMAD
1762 case ISD::FMAD: R = PromoteFloatRes_FMAD(N); break;
1764 case ISD::FPOWI: R = PromoteFloatRes_FPOWI(N); break;
1766 case ISD::FP_ROUND: R = PromoteFloatRes_FP_ROUND(N); break;
1767 case ISD::LOAD: R = PromoteFloatRes_LOAD(N); break;
1768 case ISD::SELECT: R = PromoteFloatRes_SELECT(N); break;
1769 case ISD::SELECT_CC: R = PromoteFloatRes_SELECT_CC(N); break;
1771 case ISD::SINT_TO_FP:
1772 case ISD::UINT_TO_FP: R = PromoteFloatRes_XINT_TO_FP(N); break;
1773 case ISD::UNDEF: R = PromoteFloatRes_UNDEF(N); break;
1778 SetPromotedFloat(SDValue(N, ResNo), R);
1781 // Bitcast from i16 to f16: convert the i16 to a f32 value instead.
1782 // At this point, it is not possible to determine if the bitcast value is
1783 // eventually stored to memory or promoted to f32 or promoted to a floating
1784 // point at a higher precision. Some of these cases are handled by FP_EXTEND,
1785 // STORE promotion handlers.
1786 SDValue DAGTypeLegalizer::PromoteFloatRes_BITCAST(SDNode *N) {
1787 EVT VT = N->getValueType(0);
1788 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1789 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT,
1793 SDValue DAGTypeLegalizer::PromoteFloatRes_ConstantFP(SDNode *N) {
1794 ConstantFPSDNode *CFPNode = cast<ConstantFPSDNode>(N);
1795 EVT VT = N->getValueType(0);
1797 // Get the (bit-cast) APInt of the APFloat and build an integer constant
1798 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1799 SDValue C = DAG.getConstant(CFPNode->getValueAPF().bitcastToAPInt(),
1802 // Convert the Constant to the desired FP type
1803 // FIXME We might be able to do the conversion during compilation and get rid
1804 // of it from the object code
1805 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1806 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, C);
1809 // If the Index operand is a constant, try to redirect the extract operation to
1810 // the correct legalized vector. If not, bit-convert the input vector to
1811 // equivalent integer vector. Extract the element as an (bit-cast) integer
1812 // value and convert it to the promoted type.
1813 SDValue DAGTypeLegalizer::PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
1816 // If the index is constant, try to extract the value from the legalized
1818 if (isa<ConstantSDNode>(N->getOperand(1))) {
1819 SDValue Vec = N->getOperand(0);
1820 SDValue Idx = N->getOperand(1);
1821 EVT VecVT = Vec->getValueType(0);
1822 EVT EltVT = VecVT.getVectorElementType();
1824 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1826 switch (getTypeAction(VecVT)) {
1828 case TargetLowering::TypeScalarizeVector: {
1829 SDValue Res = GetScalarizedVector(N->getOperand(0));
1830 ReplaceValueWith(SDValue(N, 0), Res);
1833 case TargetLowering::TypeWidenVector: {
1834 Vec = GetWidenedVector(Vec);
1835 SDValue Res = DAG.getNode(N->getOpcode(), DL, EltVT, Vec, Idx);
1836 ReplaceValueWith(SDValue(N, 0), Res);
1839 case TargetLowering::TypeSplitVector: {
1841 GetSplitVector(Vec, Lo, Hi);
1843 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1845 if (IdxVal < LoElts)
1846 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Lo, Idx);
1848 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Hi,
1849 DAG.getConstant(IdxVal - LoElts,
1850 Idx.getValueType()));
1851 ReplaceValueWith(SDValue(N, 0), Res);
1858 // Bit-convert the input vector to the equivalent integer vector
1859 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
1860 EVT IVT = NewOp.getValueType().getVectorElementType();
1862 // Extract the element as an (bit-cast) integer value
1863 SDValue NewVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IVT,
1864 NewOp, N->getOperand(1));
1866 // Convert the element to the desired FP type
1867 EVT VT = N->getValueType(0);
1868 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1869 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, NewVal);
1872 // FCOPYSIGN(X, Y) returns the value of X with the sign of Y. If the result
1873 // needs promotion, so does the argument X. Note that Y, if needed, will be
1874 // handled during operand promotion.
1875 SDValue DAGTypeLegalizer::PromoteFloatRes_FCOPYSIGN(SDNode *N) {
1876 EVT VT = N->getValueType(0);
1877 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1878 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1880 SDValue Op1 = N->getOperand(1);
1882 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1885 // Unary operation where the result and the operand have PromoteFloat type
1886 // action. Construct a new SDNode with the promoted float value of the old
1888 SDValue DAGTypeLegalizer::PromoteFloatRes_UnaryOp(SDNode *N) {
1889 EVT VT = N->getValueType(0);
1890 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1891 SDValue Op = GetPromotedFloat(N->getOperand(0));
1893 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op);
1896 // Binary operations where the result and both operands have PromoteFloat type
1897 // action. Construct a new SDNode with the promoted float values of the old
1899 SDValue DAGTypeLegalizer::PromoteFloatRes_BinOp(SDNode *N) {
1900 EVT VT = N->getValueType(0);
1901 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1902 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1903 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1905 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1908 SDValue DAGTypeLegalizer::PromoteFloatRes_FMAD(SDNode *N) {
1909 EVT VT = N->getValueType(0);
1910 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1911 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1912 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1913 SDValue Op2 = GetPromotedFloat(N->getOperand(2));
1915 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, Op2);
1918 // Promote the Float (first) operand and retain the Integer (second) operand
1919 SDValue DAGTypeLegalizer::PromoteFloatRes_FPOWI(SDNode *N) {
1920 EVT VT = N->getValueType(0);
1921 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1922 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1923 SDValue Op1 = N->getOperand(1);
1925 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1928 // Explicit operation to reduce precision. Reduce the value to half precision
1929 // and promote it back to the legal type.
1930 SDValue DAGTypeLegalizer::PromoteFloatRes_FP_ROUND(SDNode *N) {
1933 SDValue Op = N->getOperand(0);
1934 EVT VT = N->getValueType(0);
1935 EVT OpVT = Op->getValueType(0);
1936 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1937 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1939 // Round promoted float to desired precision
1940 SDValue Round = DAG.getNode(GetPromotionOpcode(OpVT, VT), DL, IVT, Op);
1941 // Promote it back to the legal output type
1942 return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, Round);
1945 SDValue DAGTypeLegalizer::PromoteFloatRes_LOAD(SDNode *N) {
1946 LoadSDNode *L = cast<LoadSDNode>(N);
1947 EVT VT = N->getValueType(0);
1949 // Load the value as an integer value with the same number of bits
1950 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1951 SDValue newL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
1952 IVT, SDLoc(N), L->getChain(), L->getBasePtr(),
1953 L->getOffset(), L->getPointerInfo(), IVT, L->isVolatile(),
1954 L->isNonTemporal(), false, L->getAlignment(),
1956 // Legalize the chain result by replacing uses of the old value chain with the
1958 ReplaceValueWith(SDValue(N, 1), newL.getValue(1));
1960 // Convert the integer value to the desired FP type
1961 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1962 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, newL);
1965 // Construct a new SELECT node with the promoted true- and false- values.
1966 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT(SDNode *N) {
1967 SDValue TrueVal = GetPromotedFloat(N->getOperand(1));
1968 SDValue FalseVal = GetPromotedFloat(N->getOperand(2));
1970 return DAG.getNode(ISD::SELECT, SDLoc(N), TrueVal->getValueType(0),
1971 N->getOperand(0), TrueVal, FalseVal);
1974 // Construct a new SELECT_CC node with the promoted true- and false- values.
1975 // The operands used for comparison are promoted by PromoteFloatOp_SELECT_CC.
1976 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT_CC(SDNode *N) {
1977 SDValue TrueVal = GetPromotedFloat(N->getOperand(2));
1978 SDValue FalseVal = GetPromotedFloat(N->getOperand(3));
1980 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1981 N->getOperand(0), N->getOperand(1), TrueVal, FalseVal,
1985 // Construct a SDNode that transforms the SINT or UINT operand to the promoted
1987 SDValue DAGTypeLegalizer::PromoteFloatRes_XINT_TO_FP(SDNode *N) {
1988 EVT VT = N->getValueType(0);
1989 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1990 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, N->getOperand(0));
1993 SDValue DAGTypeLegalizer::PromoteFloatRes_UNDEF(SDNode *N) {
1994 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
1995 N->getValueType(0)));