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::FADD: R = SoftenFloatRes_FADD(N); break;
72 case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
73 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
74 case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
75 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
76 case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
77 case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
78 case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
79 case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
80 case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
81 case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
82 case ISD::FMA: R = SoftenFloatRes_FMA(N); break;
83 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
84 case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
85 case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
86 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
87 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
88 case ISD::FP16_TO_FP: R = SoftenFloatRes_FP16_TO_FP(N); break;
89 case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
90 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
91 case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
92 case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
93 case ISD::FROUND: R = SoftenFloatRes_FROUND(N); break;
94 case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
95 case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
96 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
97 case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
98 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
99 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
100 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
101 case ISD::SINT_TO_FP:
102 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
103 case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
104 case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
107 // If R is null, the sub-method took care of registering the result.
109 SetSoftenedFloat(SDValue(N, ResNo), R);
112 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
113 return BitConvertToInteger(N->getOperand(0));
116 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
118 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
119 return BitConvertToInteger(Op);
122 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
123 // Convert the inputs to integers, and build a new pair out of them.
124 return DAG.getNode(ISD::BUILD_PAIR, SDLoc(N),
125 TLI.getTypeToTransformTo(*DAG.getContext(),
127 BitConvertToInteger(N->getOperand(0)),
128 BitConvertToInteger(N->getOperand(1)));
131 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
132 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
133 TLI.getTypeToTransformTo(*DAG.getContext(),
134 N->getValueType(0)));
137 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
138 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
139 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
140 NewOp.getValueType().getVectorElementType(),
141 NewOp, N->getOperand(1));
144 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
145 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
146 unsigned Size = NVT.getSizeInBits();
148 // Mask = ~(1 << (Size-1))
149 APInt API = APInt::getAllOnesValue(Size);
150 API.clearBit(Size-1);
151 SDValue Mask = DAG.getConstant(API, NVT);
152 SDValue Op = GetSoftenedFloat(N->getOperand(0));
153 return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
156 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
157 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
158 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
159 GetSoftenedFloat(N->getOperand(1)) };
160 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
166 NVT, Ops, 2, false, SDLoc(N)).first;
169 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
170 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
171 SDValue Op = GetSoftenedFloat(N->getOperand(0));
172 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
177 RTLIB::CEIL_PPCF128),
178 NVT, &Op, 1, false, SDLoc(N)).first;
181 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
182 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
183 SDValue RHS = BitConvertToInteger(N->getOperand(1));
186 EVT LVT = LHS.getValueType();
187 EVT RVT = RHS.getValueType();
189 unsigned LSize = LVT.getSizeInBits();
190 unsigned RSize = RVT.getSizeInBits();
192 // First get the sign bit of second operand.
193 SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
194 DAG.getConstant(RSize - 1,
195 TLI.getShiftAmountTy(RVT)));
196 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
198 // Shift right or sign-extend it if the two operands have different types.
199 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
201 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
202 DAG.getConstant(SizeDiff,
203 TLI.getShiftAmountTy(SignBit.getValueType())));
204 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
205 } else if (SizeDiff < 0) {
206 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
207 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
208 DAG.getConstant(-SizeDiff,
209 TLI.getShiftAmountTy(SignBit.getValueType())));
212 // Clear the sign bit of the first operand.
213 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
214 DAG.getConstant(LSize - 1,
215 TLI.getShiftAmountTy(LVT)));
216 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
217 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
219 // Or the value with the sign bit.
220 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
223 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
224 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
225 SDValue Op = GetSoftenedFloat(N->getOperand(0));
226 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
232 NVT, &Op, 1, false, SDLoc(N)).first;
235 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
236 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
237 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
238 GetSoftenedFloat(N->getOperand(1)) };
239 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
245 NVT, Ops, 2, false, SDLoc(N)).first;
248 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
249 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
250 SDValue Op = GetSoftenedFloat(N->getOperand(0));
251 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
257 NVT, &Op, 1, false, SDLoc(N)).first;
260 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
261 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
262 SDValue Op = GetSoftenedFloat(N->getOperand(0));
263 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
268 RTLIB::EXP2_PPCF128),
269 NVT, &Op, 1, false, SDLoc(N)).first;
272 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
273 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
274 SDValue Op = GetSoftenedFloat(N->getOperand(0));
275 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
280 RTLIB::FLOOR_PPCF128),
281 NVT, &Op, 1, false, SDLoc(N)).first;
284 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
285 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
286 SDValue Op = GetSoftenedFloat(N->getOperand(0));
287 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
293 NVT, &Op, 1, false, SDLoc(N)).first;
296 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
297 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
298 SDValue Op = GetSoftenedFloat(N->getOperand(0));
299 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
304 RTLIB::LOG2_PPCF128),
305 NVT, &Op, 1, false, SDLoc(N)).first;
308 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
309 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
310 SDValue Op = GetSoftenedFloat(N->getOperand(0));
311 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
316 RTLIB::LOG10_PPCF128),
317 NVT, &Op, 1, false, SDLoc(N)).first;
320 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
321 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
322 SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
323 GetSoftenedFloat(N->getOperand(1)),
324 GetSoftenedFloat(N->getOperand(2)) };
325 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
331 NVT, Ops, 3, false, SDLoc(N)).first;
334 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
335 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
336 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
337 GetSoftenedFloat(N->getOperand(1)) };
338 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
344 NVT, Ops, 2, false, SDLoc(N)).first;
347 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
348 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
349 SDValue Op = GetSoftenedFloat(N->getOperand(0));
350 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
351 RTLIB::NEARBYINT_F32,
352 RTLIB::NEARBYINT_F64,
353 RTLIB::NEARBYINT_F80,
354 RTLIB::NEARBYINT_F128,
355 RTLIB::NEARBYINT_PPCF128),
356 NVT, &Op, 1, false, SDLoc(N)).first;
359 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
360 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
361 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
362 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
363 GetSoftenedFloat(N->getOperand(0)) };
364 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
370 NVT, Ops, 2, false, SDLoc(N)).first;
373 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
374 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
375 SDValue Op = N->getOperand(0);
377 // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
378 // entirely possible for both f16 and f32 to be legal, so use the fully
379 // hard-float FP_EXTEND rather than FP16_TO_FP.
380 if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32)
381 Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
383 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
384 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
385 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
388 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
390 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
391 EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
392 SDValue Op = N->getOperand(0);
393 SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, &Op, 1,
394 false, SDLoc(N)).first;
395 if (N->getValueType(0) == MVT::f32)
398 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
399 RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
400 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
401 return TLI.makeLibCall(DAG, LC, NVT, &Res32, 1, false, SDLoc(N)).first;
404 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
405 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
406 SDValue Op = N->getOperand(0);
407 if (N->getValueType(0) == MVT::f16) {
408 // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
409 // storage-only type get a chance to select things.
410 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
413 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
414 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
415 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
418 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
419 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
420 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
421 GetSoftenedFloat(N->getOperand(1)) };
422 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
428 NVT, Ops, 2, false, SDLoc(N)).first;
431 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
432 assert(N->getOperand(1).getValueType() == MVT::i32 &&
433 "Unsupported power type!");
434 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
435 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
436 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
441 RTLIB::POWI_PPCF128),
442 NVT, Ops, 2, false, SDLoc(N)).first;
445 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
446 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
447 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
448 GetSoftenedFloat(N->getOperand(1)) };
449 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
455 NVT, Ops, 2, false, SDLoc(N)).first;
458 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
459 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
460 SDValue Op = GetSoftenedFloat(N->getOperand(0));
461 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
466 RTLIB::RINT_PPCF128),
467 NVT, &Op, 1, false, SDLoc(N)).first;
470 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
471 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
472 SDValue Op = GetSoftenedFloat(N->getOperand(0));
473 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
478 RTLIB::ROUND_PPCF128),
479 NVT, &Op, 1, false, SDLoc(N)).first;
482 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
483 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
484 SDValue Op = GetSoftenedFloat(N->getOperand(0));
485 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
491 NVT, &Op, 1, false, SDLoc(N)).first;
494 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
495 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
496 SDValue Op = GetSoftenedFloat(N->getOperand(0));
497 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
502 RTLIB::SQRT_PPCF128),
503 NVT, &Op, 1, false, SDLoc(N)).first;
506 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
507 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
508 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
509 GetSoftenedFloat(N->getOperand(1)) };
510 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
516 NVT, Ops, 2, false, SDLoc(N)).first;
519 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
520 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
521 if (N->getValueType(0) == MVT::f16)
522 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
524 SDValue Op = GetSoftenedFloat(N->getOperand(0));
525 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
530 RTLIB::TRUNC_PPCF128),
531 NVT, &Op, 1, false, SDLoc(N)).first;
534 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
535 LoadSDNode *L = cast<LoadSDNode>(N);
536 EVT VT = N->getValueType(0);
537 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
541 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
542 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
543 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
544 L->getPointerInfo(), NVT, L->isVolatile(),
545 L->isNonTemporal(), false, L->getAlignment(),
547 // Legalized the chain result - switch anything that used the old chain to
549 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
553 // Do a non-extending load followed by FP_EXTEND.
554 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
555 L->getMemoryVT(), dl, L->getChain(),
556 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
557 L->getMemoryVT(), L->isVolatile(),
558 L->isNonTemporal(), false, L->getAlignment(),
560 // Legalized the chain result - switch anything that used the old chain to
562 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
563 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
566 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
567 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
568 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
569 return DAG.getSelect(SDLoc(N),
570 LHS.getValueType(), N->getOperand(0), LHS, RHS);
573 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
574 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
575 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
576 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
577 LHS.getValueType(), N->getOperand(0),
578 N->getOperand(1), LHS, RHS, N->getOperand(4));
581 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
582 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
583 N->getValueType(0)));
586 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
587 SDValue Chain = N->getOperand(0); // Get the chain.
588 SDValue Ptr = N->getOperand(1); // Get the pointer.
589 EVT VT = N->getValueType(0);
590 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
594 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
595 N->getConstantOperandVal(3));
597 // Legalized the chain result - switch anything that used the old chain to
599 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
603 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
604 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
605 EVT SVT = N->getOperand(0).getValueType();
606 EVT RVT = N->getValueType(0);
610 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
611 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
612 // match. Look for an appropriate libcall.
613 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
614 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
615 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
616 NVT = (MVT::SimpleValueType)t;
617 // The source needs to big enough to hold the operand.
619 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
621 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
623 // Sign/zero extend the argument if the libcall takes a larger type.
624 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
625 NVT, N->getOperand(0));
626 return TLI.makeLibCall(DAG, LC,
627 TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
628 &Op, 1, false, dl).first;
632 //===----------------------------------------------------------------------===//
633 // Operand Float to Integer Conversion..
634 //===----------------------------------------------------------------------===//
636 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
637 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
639 SDValue Res = SDValue();
641 switch (N->getOpcode()) {
644 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
645 N->dump(&DAG); dbgs() << "\n";
647 llvm_unreachable("Do not know how to soften this operator's operand!");
649 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
650 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
651 case ISD::FP_EXTEND: Res = SoftenFloatOp_FP_EXTEND(N); break;
652 case ISD::FP_TO_FP16: // Same as FP_ROUND for softening purposes
653 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
654 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
655 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
656 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
657 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
658 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
661 // If the result is null, the sub-method took care of registering results etc.
662 if (!Res.getNode()) return false;
664 // If the result is N, the sub-method updated N in place. Tell the legalizer
666 if (Res.getNode() == N)
669 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
670 "Invalid operand expansion");
672 ReplaceValueWith(SDValue(N, 0), Res);
676 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
677 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
678 GetSoftenedFloat(N->getOperand(0)));
681 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
682 // If we get here, the result must be legal but the source illegal.
683 EVT SVT = N->getOperand(0).getValueType();
684 EVT RVT = N->getValueType(0);
685 SDValue Op = GetSoftenedFloat(N->getOperand(0));
688 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
690 RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
691 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
693 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
697 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
698 // We actually deal with the partially-softened FP_TO_FP16 node too, which
699 // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
700 assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
702 EVT SVT = N->getOperand(0).getValueType();
703 EVT RVT = N->getValueType(0);
704 EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
706 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
707 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
709 SDValue Op = GetSoftenedFloat(N->getOperand(0));
710 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
713 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
714 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
715 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
717 EVT VT = NewLHS.getValueType();
718 NewLHS = GetSoftenedFloat(NewLHS);
719 NewRHS = GetSoftenedFloat(NewRHS);
720 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
722 // If softenSetCCOperands returned a scalar, we need to compare the result
723 // against zero to select between true and false values.
724 if (!NewRHS.getNode()) {
725 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
729 // Update N to have the operands specified.
730 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
731 DAG.getCondCode(CCCode), NewLHS, NewRHS,
736 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
737 EVT RVT = N->getValueType(0);
738 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
739 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
740 SDValue Op = GetSoftenedFloat(N->getOperand(0));
741 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
744 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
745 EVT RVT = N->getValueType(0);
746 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
747 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
748 SDValue Op = GetSoftenedFloat(N->getOperand(0));
749 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
752 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
753 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
754 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
756 EVT VT = NewLHS.getValueType();
757 NewLHS = GetSoftenedFloat(NewLHS);
758 NewRHS = GetSoftenedFloat(NewRHS);
759 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
761 // If softenSetCCOperands returned a scalar, we need to compare the result
762 // against zero to select between true and false values.
763 if (!NewRHS.getNode()) {
764 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
768 // Update N to have the operands specified.
769 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
770 N->getOperand(2), N->getOperand(3),
771 DAG.getCondCode(CCCode)),
775 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
776 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
777 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
779 EVT VT = NewLHS.getValueType();
780 NewLHS = GetSoftenedFloat(NewLHS);
781 NewRHS = GetSoftenedFloat(NewRHS);
782 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
784 // If softenSetCCOperands returned a scalar, use it.
785 if (!NewRHS.getNode()) {
786 assert(NewLHS.getValueType() == N->getValueType(0) &&
787 "Unexpected setcc expansion!");
791 // Otherwise, update N to have the operands specified.
792 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
793 DAG.getCondCode(CCCode)),
797 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
798 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
799 assert(OpNo == 1 && "Can only soften the stored value!");
800 StoreSDNode *ST = cast<StoreSDNode>(N);
801 SDValue Val = ST->getValue();
804 if (ST->isTruncatingStore())
805 // Do an FP_ROUND followed by a non-truncating store.
806 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
807 Val, DAG.getIntPtrConstant(0)));
809 Val = GetSoftenedFloat(Val);
811 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
812 ST->getMemOperand());
816 //===----------------------------------------------------------------------===//
817 // Float Result Expansion
818 //===----------------------------------------------------------------------===//
820 /// ExpandFloatResult - This method is called when the specified result of the
821 /// specified node is found to need expansion. At this point, the node may also
822 /// have invalid operands or may have other results that need promotion, we just
823 /// know that (at least) one result needs expansion.
824 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
825 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
829 // See if the target wants to custom expand this node.
830 if (CustomLowerNode(N, N->getValueType(ResNo), true))
833 switch (N->getOpcode()) {
836 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
837 N->dump(&DAG); dbgs() << "\n";
839 llvm_unreachable("Do not know how to expand the result of this operator!");
841 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
842 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
843 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
845 case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
846 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
847 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
848 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
849 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
850 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
852 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
853 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
854 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
855 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
856 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
857 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
858 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
859 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
860 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
861 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
862 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
863 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
864 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
865 case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
866 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
867 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
868 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
869 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
870 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
871 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
872 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
873 case ISD::FROUND: ExpandFloatRes_FROUND(N, Lo, Hi); break;
874 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
875 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
876 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
877 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
878 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
879 case ISD::SINT_TO_FP:
880 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
881 case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
884 // If Lo/Hi is null, the sub-method took care of registering results etc.
886 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
889 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
891 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
892 assert(NVT.getSizeInBits() == integerPartWidth &&
893 "Do not know how to expand this float constant!");
894 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
895 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
896 APInt(integerPartWidth, C.getRawData()[1])),
898 Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
899 APInt(integerPartWidth, C.getRawData()[0])),
903 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
905 assert(N->getValueType(0) == MVT::ppcf128 &&
906 "Logic only correct for ppcf128!");
909 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
910 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
911 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
912 Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
913 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
917 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
919 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
920 RTLIB::ADD_F32, RTLIB::ADD_F64,
921 RTLIB::ADD_F80, RTLIB::ADD_F128,
924 GetPairElements(Call, Lo, Hi);
927 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
928 SDValue &Lo, SDValue &Hi) {
929 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
930 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
931 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
932 RTLIB::CEIL_PPCF128),
934 GetPairElements(Call, Lo, Hi);
937 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
938 SDValue &Lo, SDValue &Hi) {
939 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
943 RTLIB::COPYSIGN_F128,
944 RTLIB::COPYSIGN_PPCF128),
946 GetPairElements(Call, Lo, Hi);
949 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
950 SDValue &Lo, SDValue &Hi) {
951 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
952 RTLIB::COS_F32, RTLIB::COS_F64,
953 RTLIB::COS_F80, RTLIB::COS_F128,
956 GetPairElements(Call, Lo, Hi);
959 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
961 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
962 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
968 N->getValueType(0), Ops, 2, false,
970 GetPairElements(Call, Lo, Hi);
973 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
974 SDValue &Lo, SDValue &Hi) {
975 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
976 RTLIB::EXP_F32, RTLIB::EXP_F64,
977 RTLIB::EXP_F80, RTLIB::EXP_F128,
980 GetPairElements(Call, Lo, Hi);
983 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
984 SDValue &Lo, SDValue &Hi) {
985 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
986 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
987 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
988 RTLIB::EXP2_PPCF128),
990 GetPairElements(Call, Lo, Hi);
993 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
994 SDValue &Lo, SDValue &Hi) {
995 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
996 RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
997 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
998 RTLIB::FLOOR_PPCF128),
1000 GetPairElements(Call, Lo, Hi);
1003 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1004 SDValue &Lo, SDValue &Hi) {
1005 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1006 RTLIB::LOG_F32, RTLIB::LOG_F64,
1007 RTLIB::LOG_F80, RTLIB::LOG_F128,
1008 RTLIB::LOG_PPCF128),
1010 GetPairElements(Call, Lo, Hi);
1013 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1014 SDValue &Lo, SDValue &Hi) {
1015 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1016 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1017 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1018 RTLIB::LOG2_PPCF128),
1020 GetPairElements(Call, Lo, Hi);
1023 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1024 SDValue &Lo, SDValue &Hi) {
1025 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1026 RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1027 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1028 RTLIB::LOG10_PPCF128),
1030 GetPairElements(Call, Lo, Hi);
1033 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1035 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1036 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1041 RTLIB::FMA_PPCF128),
1042 N->getValueType(0), Ops, 3, false,
1044 GetPairElements(Call, Lo, Hi);
1047 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1049 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1050 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1055 RTLIB::MUL_PPCF128),
1056 N->getValueType(0), Ops, 2, false,
1058 GetPairElements(Call, Lo, Hi);
1061 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1062 SDValue &Lo, SDValue &Hi) {
1063 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1064 RTLIB::NEARBYINT_F32,
1065 RTLIB::NEARBYINT_F64,
1066 RTLIB::NEARBYINT_F80,
1067 RTLIB::NEARBYINT_F128,
1068 RTLIB::NEARBYINT_PPCF128),
1070 GetPairElements(Call, Lo, Hi);
1073 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1076 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1077 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1078 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1081 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1083 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1084 Hi = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), NVT, N->getOperand(0));
1085 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1086 APInt(NVT.getSizeInBits(), 0)), NVT);
1089 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1090 SDValue &Lo, SDValue &Hi) {
1091 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1092 RTLIB::POW_F32, RTLIB::POW_F64,
1093 RTLIB::POW_F80, RTLIB::POW_F128,
1094 RTLIB::POW_PPCF128),
1096 GetPairElements(Call, Lo, Hi);
1099 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1100 SDValue &Lo, SDValue &Hi) {
1101 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1102 RTLIB::POWI_F32, RTLIB::POWI_F64,
1103 RTLIB::POWI_F80, RTLIB::POWI_F128,
1104 RTLIB::POWI_PPCF128),
1106 GetPairElements(Call, Lo, Hi);
1109 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1110 SDValue &Lo, SDValue &Hi) {
1111 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1112 RTLIB::REM_F32, RTLIB::REM_F64,
1113 RTLIB::REM_F80, RTLIB::REM_F128,
1114 RTLIB::REM_PPCF128),
1116 GetPairElements(Call, Lo, Hi);
1119 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1120 SDValue &Lo, SDValue &Hi) {
1121 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1122 RTLIB::RINT_F32, RTLIB::RINT_F64,
1123 RTLIB::RINT_F80, RTLIB::RINT_F128,
1124 RTLIB::RINT_PPCF128),
1126 GetPairElements(Call, Lo, Hi);
1129 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1130 SDValue &Lo, SDValue &Hi) {
1131 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1136 RTLIB::ROUND_PPCF128),
1138 GetPairElements(Call, Lo, Hi);
1141 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1142 SDValue &Lo, SDValue &Hi) {
1143 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1144 RTLIB::SIN_F32, RTLIB::SIN_F64,
1145 RTLIB::SIN_F80, RTLIB::SIN_F128,
1146 RTLIB::SIN_PPCF128),
1148 GetPairElements(Call, Lo, Hi);
1151 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1152 SDValue &Lo, SDValue &Hi) {
1153 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1154 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1155 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1156 RTLIB::SQRT_PPCF128),
1158 GetPairElements(Call, Lo, Hi);
1161 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1163 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1164 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1169 RTLIB::SUB_PPCF128),
1170 N->getValueType(0), Ops, 2, false,
1172 GetPairElements(Call, Lo, Hi);
1175 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1176 SDValue &Lo, SDValue &Hi) {
1177 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1178 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1179 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1180 RTLIB::TRUNC_PPCF128),
1182 GetPairElements(Call, Lo, Hi);
1185 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1187 if (ISD::isNormalLoad(N)) {
1188 ExpandRes_NormalLoad(N, Lo, Hi);
1192 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1193 LoadSDNode *LD = cast<LoadSDNode>(N);
1194 SDValue Chain = LD->getChain();
1195 SDValue Ptr = LD->getBasePtr();
1198 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1199 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1200 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1202 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1203 LD->getMemoryVT(), LD->getMemOperand());
1205 // Remember the chain.
1206 Chain = Hi.getValue(1);
1208 // The low part is zero.
1209 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1210 APInt(NVT.getSizeInBits(), 0)), NVT);
1212 // Modified the chain - switch anything that used the old chain to use the
1214 ReplaceValueWith(SDValue(LD, 1), Chain);
1217 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1219 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1220 EVT VT = N->getValueType(0);
1221 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1222 SDValue Src = N->getOperand(0);
1223 EVT SrcVT = Src.getValueType();
1224 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1227 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1228 // When promoting partial word types to i32 we must honor the signedness,
1230 if (SrcVT.bitsLE(MVT::i32)) {
1231 // The integer can be represented exactly in an f64.
1232 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1234 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1235 APInt(NVT.getSizeInBits(), 0)), NVT);
1236 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1238 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1239 if (SrcVT.bitsLE(MVT::i64)) {
1240 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1242 LC = RTLIB::SINTTOFP_I64_PPCF128;
1243 } else if (SrcVT.bitsLE(MVT::i128)) {
1244 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1245 LC = RTLIB::SINTTOFP_I128_PPCF128;
1247 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1249 Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl).first;
1250 GetPairElements(Hi, Lo, Hi);
1256 // Unsigned - fix up the SINT_TO_FP value just calculated.
1257 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1258 SrcVT = Src.getValueType();
1260 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1261 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1262 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1263 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1264 ArrayRef<uint64_t> Parts;
1266 switch (SrcVT.getSimpleVT().SimpleTy) {
1268 llvm_unreachable("Unsupported UINT_TO_FP!");
1280 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1281 DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1284 Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, SrcVT),
1285 Lo, Hi, ISD::SETLT);
1286 GetPairElements(Lo, Lo, Hi);
1290 //===----------------------------------------------------------------------===//
1291 // Float Operand Expansion
1292 //===----------------------------------------------------------------------===//
1294 /// ExpandFloatOperand - This method is called when the specified operand of the
1295 /// specified node is found to need expansion. At this point, all of the result
1296 /// types of the node are known to be legal, but other operands of the node may
1297 /// need promotion or expansion as well as the specified one.
1298 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1299 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1300 SDValue Res = SDValue();
1302 // See if the target wants to custom expand this node.
1303 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1306 switch (N->getOpcode()) {
1309 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1310 N->dump(&DAG); dbgs() << "\n";
1312 llvm_unreachable("Do not know how to expand this operator's operand!");
1314 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1315 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1316 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1318 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1319 case ISD::FCOPYSIGN: Res = ExpandFloatOp_FCOPYSIGN(N); break;
1320 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1321 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1322 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1323 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1324 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1325 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1329 // If the result is null, the sub-method took care of registering results etc.
1330 if (!Res.getNode()) return false;
1332 // If the result is N, the sub-method updated N in place. Tell the legalizer
1334 if (Res.getNode() == N)
1337 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1338 "Invalid operand expansion");
1340 ReplaceValueWith(SDValue(N, 0), Res);
1344 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1345 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1346 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1348 ISD::CondCode &CCCode,
1350 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1351 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1352 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1354 assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1356 // FIXME: This generated code sucks. We want to generate
1357 // FCMPU crN, hi1, hi2
1359 // FCMPU crN, lo1, lo2
1360 // The following can be improved, but not that much.
1361 SDValue Tmp1, Tmp2, Tmp3;
1362 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1363 LHSHi, RHSHi, ISD::SETOEQ);
1364 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1365 LHSLo, RHSLo, CCCode);
1366 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1367 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1368 LHSHi, RHSHi, ISD::SETUNE);
1369 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1370 LHSHi, RHSHi, CCCode);
1371 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1372 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1373 NewRHS = SDValue(); // LHS is the result, not a compare.
1376 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1377 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1378 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1379 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1381 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1382 // against zero to select between true and false values.
1383 if (!NewRHS.getNode()) {
1384 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1385 CCCode = ISD::SETNE;
1388 // Update N to have the operands specified.
1389 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1390 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1391 N->getOperand(4)), 0);
1394 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1395 assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1396 "Logic only correct for ppcf128!");
1398 GetExpandedFloat(N->getOperand(1), Lo, Hi);
1399 // The ppcf128 value is providing only the sign; take it from the
1400 // higher-order double (which must have the larger magnitude).
1401 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1402 N->getValueType(0), N->getOperand(0), Hi);
1405 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1406 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1407 "Logic only correct for ppcf128!");
1409 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1410 // Round it the rest of the way (e.g. to f32) if needed.
1411 return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1412 N->getValueType(0), Hi, N->getOperand(1));
1415 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1416 EVT RVT = N->getValueType(0);
1419 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1420 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1421 if (RVT == MVT::i32) {
1422 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1423 "Logic only correct for ppcf128!");
1424 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1425 N->getOperand(0), DAG.getValueType(MVT::f64));
1426 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1427 DAG.getIntPtrConstant(1));
1428 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1431 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1432 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1433 return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl).first;
1436 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1437 EVT RVT = N->getValueType(0);
1440 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1441 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1442 if (RVT == MVT::i32) {
1443 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1444 "Logic only correct for ppcf128!");
1445 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1446 APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1447 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1448 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1449 // FIXME: generated code sucks.
1450 return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1451 DAG.getNode(ISD::ADD, dl, MVT::i32,
1452 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1453 DAG.getNode(ISD::FSUB, dl,
1457 DAG.getConstant(0x80000000, MVT::i32)),
1458 DAG.getNode(ISD::FP_TO_SINT, dl,
1459 MVT::i32, N->getOperand(0)),
1463 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1464 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1465 return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1469 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1470 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1471 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1472 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1474 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1475 // against zero to select between true and false values.
1476 if (!NewRHS.getNode()) {
1477 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1478 CCCode = ISD::SETNE;
1481 // Update N to have the operands specified.
1482 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1483 N->getOperand(2), N->getOperand(3),
1484 DAG.getCondCode(CCCode)), 0);
1487 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1488 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1489 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1490 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1492 // If ExpandSetCCOperands returned a scalar, use it.
1493 if (!NewRHS.getNode()) {
1494 assert(NewLHS.getValueType() == N->getValueType(0) &&
1495 "Unexpected setcc expansion!");
1499 // Otherwise, update N to have the operands specified.
1500 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1501 DAG.getCondCode(CCCode)), 0);
1504 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1505 if (ISD::isNormalStore(N))
1506 return ExpandOp_NormalStore(N, OpNo);
1508 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1509 assert(OpNo == 1 && "Can only expand the stored value so far");
1510 StoreSDNode *ST = cast<StoreSDNode>(N);
1512 SDValue Chain = ST->getChain();
1513 SDValue Ptr = ST->getBasePtr();
1515 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1516 ST->getValue().getValueType());
1517 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1518 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1522 GetExpandedOp(ST->getValue(), Lo, Hi);
1524 return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1525 ST->getMemoryVT(), ST->getMemOperand());