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 /// GetFPLibCall - Return the right libcall for the given floating point type.
28 static RTLIB::Libcall GetFPLibCall(EVT VT,
29 RTLIB::Libcall Call_F32,
30 RTLIB::Libcall Call_F64,
31 RTLIB::Libcall Call_F80,
32 RTLIB::Libcall Call_F128,
33 RTLIB::Libcall Call_PPCF128) {
35 VT == MVT::f32 ? Call_F32 :
36 VT == MVT::f64 ? Call_F64 :
37 VT == MVT::f80 ? Call_F80 :
38 VT == MVT::f128 ? Call_F128 :
39 VT == MVT::ppcf128 ? Call_PPCF128 :
40 RTLIB::UNKNOWN_LIBCALL;
43 //===----------------------------------------------------------------------===//
44 // Result Float to Integer Conversion.
45 //===----------------------------------------------------------------------===//
47 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
48 DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
50 SDValue R = SDValue();
52 switch (N->getOpcode()) {
55 dbgs() << "SoftenFloatResult #" << ResNo << ": ";
56 N->dump(&DAG); dbgs() << "\n";
58 llvm_unreachable("Do not know how to soften the result of this operator!");
60 case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
61 case ISD::BITCAST: R = SoftenFloatRes_BITCAST(N); break;
62 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
64 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
66 case ISD::EXTRACT_VECTOR_ELT:
67 R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
68 case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
69 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
70 case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
71 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
72 case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
73 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
74 case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
75 case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
76 case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
77 case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
78 case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
79 case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
80 case ISD::FMA: R = SoftenFloatRes_FMA(N); break;
81 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
82 case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
83 case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
84 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
85 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
86 case ISD::FP16_TO_FP32:R = SoftenFloatRes_FP16_TO_FP32(N); break;
87 case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
88 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
89 case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
90 case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
91 case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
92 case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
93 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
94 case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
95 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
96 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
97 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
99 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
100 case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
101 case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
104 // If R is null, the sub-method took care of registering the result.
106 SetSoftenedFloat(SDValue(N, ResNo), R);
109 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
110 return BitConvertToInteger(N->getOperand(0));
113 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
115 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
116 return BitConvertToInteger(Op);
119 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
120 // Convert the inputs to integers, and build a new pair out of them.
121 return DAG.getNode(ISD::BUILD_PAIR, N->getDebugLoc(),
122 TLI.getTypeToTransformTo(*DAG.getContext(),
124 BitConvertToInteger(N->getOperand(0)),
125 BitConvertToInteger(N->getOperand(1)));
128 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
129 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
130 TLI.getTypeToTransformTo(*DAG.getContext(),
131 N->getValueType(0)));
134 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
135 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
136 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(),
137 NewOp.getValueType().getVectorElementType(),
138 NewOp, N->getOperand(1));
141 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
142 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
143 unsigned Size = NVT.getSizeInBits();
145 // Mask = ~(1 << (Size-1))
146 APInt API = APInt::getAllOnesValue(Size);
147 API.clearBit(Size-1);
148 SDValue Mask = DAG.getConstant(API, NVT);
149 SDValue Op = GetSoftenedFloat(N->getOperand(0));
150 return DAG.getNode(ISD::AND, N->getDebugLoc(), NVT, Op, Mask);
153 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
154 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
155 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
156 GetSoftenedFloat(N->getOperand(1)) };
157 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
163 NVT, Ops, 2, false, N->getDebugLoc());
166 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
167 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
168 SDValue Op = GetSoftenedFloat(N->getOperand(0));
169 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
174 RTLIB::CEIL_PPCF128),
175 NVT, &Op, 1, false, N->getDebugLoc());
178 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
179 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
180 SDValue RHS = BitConvertToInteger(N->getOperand(1));
181 DebugLoc dl = N->getDebugLoc();
183 EVT LVT = LHS.getValueType();
184 EVT RVT = RHS.getValueType();
186 unsigned LSize = LVT.getSizeInBits();
187 unsigned RSize = RVT.getSizeInBits();
189 // First get the sign bit of second operand.
190 SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
191 DAG.getConstant(RSize - 1,
192 TLI.getShiftAmountTy(RVT)));
193 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
195 // Shift right or sign-extend it if the two operands have different types.
196 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
198 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
199 DAG.getConstant(SizeDiff,
200 TLI.getShiftAmountTy(SignBit.getValueType())));
201 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
202 } else if (SizeDiff < 0) {
203 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
204 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
205 DAG.getConstant(-SizeDiff,
206 TLI.getShiftAmountTy(SignBit.getValueType())));
209 // Clear the sign bit of the first operand.
210 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
211 DAG.getConstant(LSize - 1,
212 TLI.getShiftAmountTy(LVT)));
213 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
214 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
216 // Or the value with the sign bit.
217 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
220 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
221 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
222 SDValue Op = GetSoftenedFloat(N->getOperand(0));
223 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
229 NVT, &Op, 1, false, N->getDebugLoc());
232 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
233 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
234 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
235 GetSoftenedFloat(N->getOperand(1)) };
236 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
242 NVT, Ops, 2, false, N->getDebugLoc());
245 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
246 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
247 SDValue Op = GetSoftenedFloat(N->getOperand(0));
248 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
254 NVT, &Op, 1, false, N->getDebugLoc());
257 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
258 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
259 SDValue Op = GetSoftenedFloat(N->getOperand(0));
260 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
265 RTLIB::EXP2_PPCF128),
266 NVT, &Op, 1, false, N->getDebugLoc());
269 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
270 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
271 SDValue Op = GetSoftenedFloat(N->getOperand(0));
272 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
277 RTLIB::FLOOR_PPCF128),
278 NVT, &Op, 1, false, N->getDebugLoc());
281 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
282 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
283 SDValue Op = GetSoftenedFloat(N->getOperand(0));
284 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
290 NVT, &Op, 1, false, N->getDebugLoc());
293 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
294 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
295 SDValue Op = GetSoftenedFloat(N->getOperand(0));
296 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
301 RTLIB::LOG2_PPCF128),
302 NVT, &Op, 1, false, N->getDebugLoc());
305 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
306 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
307 SDValue Op = GetSoftenedFloat(N->getOperand(0));
308 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
313 RTLIB::LOG10_PPCF128),
314 NVT, &Op, 1, false, N->getDebugLoc());
317 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
318 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
319 SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
320 GetSoftenedFloat(N->getOperand(1)),
321 GetSoftenedFloat(N->getOperand(2)) };
322 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
328 NVT, Ops, 3, false, N->getDebugLoc());
331 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
332 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
333 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
334 GetSoftenedFloat(N->getOperand(1)) };
335 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
341 NVT, Ops, 2, false, N->getDebugLoc());
344 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
345 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
346 SDValue Op = GetSoftenedFloat(N->getOperand(0));
347 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
348 RTLIB::NEARBYINT_F32,
349 RTLIB::NEARBYINT_F64,
350 RTLIB::NEARBYINT_F80,
351 RTLIB::NEARBYINT_F128,
352 RTLIB::NEARBYINT_PPCF128),
353 NVT, &Op, 1, false, N->getDebugLoc());
356 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
357 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
358 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
359 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
360 GetSoftenedFloat(N->getOperand(0)) };
361 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
367 NVT, Ops, 2, false, N->getDebugLoc());
370 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
371 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
372 SDValue Op = N->getOperand(0);
373 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
374 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
375 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, N->getDebugLoc());
378 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
380 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP32(SDNode *N) {
381 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
382 SDValue Op = N->getOperand(0);
383 return TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, NVT, &Op, 1, false,
387 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
388 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
389 SDValue Op = N->getOperand(0);
390 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
391 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
392 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, N->getDebugLoc());
395 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
396 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
397 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
398 GetSoftenedFloat(N->getOperand(1)) };
399 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
405 NVT, Ops, 2, false, N->getDebugLoc());
408 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
409 assert(N->getOperand(1).getValueType() == MVT::i32 &&
410 "Unsupported power type!");
411 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
412 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
413 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
418 RTLIB::POWI_PPCF128),
419 NVT, Ops, 2, false, N->getDebugLoc());
422 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
423 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
424 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
425 GetSoftenedFloat(N->getOperand(1)) };
426 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
432 NVT, Ops, 2, false, N->getDebugLoc());
435 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
436 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
437 SDValue Op = GetSoftenedFloat(N->getOperand(0));
438 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
443 RTLIB::RINT_PPCF128),
444 NVT, &Op, 1, false, N->getDebugLoc());
447 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
448 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
449 SDValue Op = GetSoftenedFloat(N->getOperand(0));
450 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
456 NVT, &Op, 1, false, N->getDebugLoc());
459 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
460 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
461 SDValue Op = GetSoftenedFloat(N->getOperand(0));
462 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
467 RTLIB::SQRT_PPCF128),
468 NVT, &Op, 1, false, N->getDebugLoc());
471 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
472 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
473 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
474 GetSoftenedFloat(N->getOperand(1)) };
475 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
481 NVT, Ops, 2, false, N->getDebugLoc());
484 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
485 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
486 SDValue Op = GetSoftenedFloat(N->getOperand(0));
487 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
492 RTLIB::TRUNC_PPCF128),
493 NVT, &Op, 1, false, N->getDebugLoc());
496 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
497 LoadSDNode *L = cast<LoadSDNode>(N);
498 EVT VT = N->getValueType(0);
499 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
500 DebugLoc dl = N->getDebugLoc();
503 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
504 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
505 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
506 L->getPointerInfo(), NVT, L->isVolatile(),
507 L->isNonTemporal(), false, L->getAlignment());
508 // Legalized the chain result - switch anything that used the old chain to
510 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
514 // Do a non-extending load followed by FP_EXTEND.
515 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
516 L->getMemoryVT(), dl, L->getChain(),
517 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
518 L->getMemoryVT(), L->isVolatile(),
519 L->isNonTemporal(), false, L->getAlignment());
520 // Legalized the chain result - switch anything that used the old chain to
522 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
523 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
526 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
527 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
528 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
529 return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
530 LHS.getValueType(), N->getOperand(0),LHS,RHS);
533 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
534 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
535 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
536 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
537 LHS.getValueType(), N->getOperand(0),
538 N->getOperand(1), LHS, RHS, N->getOperand(4));
541 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
542 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
543 N->getValueType(0)));
546 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
547 SDValue Chain = N->getOperand(0); // Get the chain.
548 SDValue Ptr = N->getOperand(1); // Get the pointer.
549 EVT VT = N->getValueType(0);
550 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
551 DebugLoc dl = N->getDebugLoc();
554 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
555 N->getConstantOperandVal(3));
557 // Legalized the chain result - switch anything that used the old chain to
559 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
563 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
564 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
565 EVT SVT = N->getOperand(0).getValueType();
566 EVT RVT = N->getValueType(0);
568 DebugLoc dl = N->getDebugLoc();
570 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
571 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
572 // match. Look for an appropriate libcall.
573 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
574 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
575 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
576 NVT = (MVT::SimpleValueType)t;
577 // The source needs to big enough to hold the operand.
579 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
581 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
583 // Sign/zero extend the argument if the libcall takes a larger type.
584 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
585 NVT, N->getOperand(0));
586 return TLI.makeLibCall(DAG, LC,
587 TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
592 //===----------------------------------------------------------------------===//
593 // Operand Float to Integer Conversion..
594 //===----------------------------------------------------------------------===//
596 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
597 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
599 SDValue Res = SDValue();
601 switch (N->getOpcode()) {
604 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
605 N->dump(&DAG); dbgs() << "\n";
607 llvm_unreachable("Do not know how to soften this operator's operand!");
609 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
610 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
611 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
612 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
613 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
614 case ISD::FP32_TO_FP16:Res = SoftenFloatOp_FP32_TO_FP16(N); break;
615 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
616 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
617 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
620 // If the result is null, the sub-method took care of registering results etc.
621 if (!Res.getNode()) return false;
623 // If the result is N, the sub-method updated N in place. Tell the legalizer
625 if (Res.getNode() == N)
628 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
629 "Invalid operand expansion");
631 ReplaceValueWith(SDValue(N, 0), Res);
635 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
636 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getValueType(0),
637 GetSoftenedFloat(N->getOperand(0)));
640 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
641 EVT SVT = N->getOperand(0).getValueType();
642 EVT RVT = N->getValueType(0);
644 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
645 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
647 SDValue Op = GetSoftenedFloat(N->getOperand(0));
648 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
651 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
652 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
653 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
655 EVT VT = NewLHS.getValueType();
656 NewLHS = GetSoftenedFloat(NewLHS);
657 NewRHS = GetSoftenedFloat(NewRHS);
658 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
660 // If softenSetCCOperands returned a scalar, we need to compare the result
661 // against zero to select between true and false values.
662 if (NewRHS.getNode() == 0) {
663 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
667 // Update N to have the operands specified.
668 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
669 DAG.getCondCode(CCCode), NewLHS, NewRHS,
674 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
675 EVT RVT = N->getValueType(0);
676 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
677 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
678 SDValue Op = GetSoftenedFloat(N->getOperand(0));
679 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
682 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
683 EVT RVT = N->getValueType(0);
684 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
685 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
686 SDValue Op = GetSoftenedFloat(N->getOperand(0));
687 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
690 SDValue DAGTypeLegalizer::SoftenFloatOp_FP32_TO_FP16(SDNode *N) {
691 EVT RVT = N->getValueType(0);
692 RTLIB::Libcall LC = RTLIB::FPROUND_F32_F16;
693 SDValue Op = GetSoftenedFloat(N->getOperand(0));
694 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
697 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
698 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
699 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
701 EVT VT = NewLHS.getValueType();
702 NewLHS = GetSoftenedFloat(NewLHS);
703 NewRHS = GetSoftenedFloat(NewRHS);
704 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
706 // If softenSetCCOperands returned a scalar, we need to compare the result
707 // against zero to select between true and false values.
708 if (NewRHS.getNode() == 0) {
709 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
713 // Update N to have the operands specified.
714 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
715 N->getOperand(2), N->getOperand(3),
716 DAG.getCondCode(CCCode)),
720 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
721 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
722 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
724 EVT VT = NewLHS.getValueType();
725 NewLHS = GetSoftenedFloat(NewLHS);
726 NewRHS = GetSoftenedFloat(NewRHS);
727 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
729 // If softenSetCCOperands returned a scalar, use it.
730 if (NewRHS.getNode() == 0) {
731 assert(NewLHS.getValueType() == N->getValueType(0) &&
732 "Unexpected setcc expansion!");
736 // Otherwise, update N to have the operands specified.
737 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
738 DAG.getCondCode(CCCode)),
742 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
743 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
744 assert(OpNo == 1 && "Can only soften the stored value!");
745 StoreSDNode *ST = cast<StoreSDNode>(N);
746 SDValue Val = ST->getValue();
747 DebugLoc dl = N->getDebugLoc();
749 if (ST->isTruncatingStore())
750 // Do an FP_ROUND followed by a non-truncating store.
751 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
752 Val, DAG.getIntPtrConstant(0)));
754 Val = GetSoftenedFloat(Val);
756 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
757 ST->getPointerInfo(),
758 ST->isVolatile(), ST->isNonTemporal(),
763 //===----------------------------------------------------------------------===//
764 // Float Result Expansion
765 //===----------------------------------------------------------------------===//
767 /// ExpandFloatResult - This method is called when the specified result of the
768 /// specified node is found to need expansion. At this point, the node may also
769 /// have invalid operands or may have other results that need promotion, we just
770 /// know that (at least) one result needs expansion.
771 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
772 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
776 // See if the target wants to custom expand this node.
777 if (CustomLowerNode(N, N->getValueType(ResNo), true))
780 switch (N->getOpcode()) {
783 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
784 N->dump(&DAG); dbgs() << "\n";
786 llvm_unreachable("Do not know how to expand the result of this operator!");
788 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
789 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
790 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
792 case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
793 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
794 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
795 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
796 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
797 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
799 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
800 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
801 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
802 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
803 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
804 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
805 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
806 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
807 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
808 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
809 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
810 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
811 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
812 case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
813 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
814 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
815 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
816 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
817 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
818 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
819 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
820 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
821 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
822 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
823 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
824 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
825 case ISD::SINT_TO_FP:
826 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
827 case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
830 // If Lo/Hi is null, the sub-method took care of registering results etc.
832 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
835 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
837 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
838 assert(NVT.getSizeInBits() == integerPartWidth &&
839 "Do not know how to expand this float constant!");
840 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
841 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
842 APInt(integerPartWidth, C.getRawData()[1])),
844 Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
845 APInt(integerPartWidth, C.getRawData()[0])),
849 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
851 assert(N->getValueType(0) == MVT::ppcf128 &&
852 "Logic only correct for ppcf128!");
853 DebugLoc dl = N->getDebugLoc();
855 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
856 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
857 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
858 Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
859 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
860 DAG.getCondCode(ISD::SETEQ));
863 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
865 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
866 RTLIB::ADD_F32, RTLIB::ADD_F64,
867 RTLIB::ADD_F80, RTLIB::ADD_F128,
870 GetPairElements(Call, Lo, Hi);
873 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
874 SDValue &Lo, SDValue &Hi) {
875 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
876 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
877 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
878 RTLIB::CEIL_PPCF128),
880 GetPairElements(Call, Lo, Hi);
883 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
884 SDValue &Lo, SDValue &Hi) {
885 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
889 RTLIB::COPYSIGN_F128,
890 RTLIB::COPYSIGN_PPCF128),
892 GetPairElements(Call, Lo, Hi);
895 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
896 SDValue &Lo, SDValue &Hi) {
897 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
898 RTLIB::COS_F32, RTLIB::COS_F64,
899 RTLIB::COS_F80, RTLIB::COS_F128,
902 GetPairElements(Call, Lo, Hi);
905 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
907 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
908 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
914 N->getValueType(0), Ops, 2, false,
916 GetPairElements(Call, Lo, Hi);
919 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
920 SDValue &Lo, SDValue &Hi) {
921 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
922 RTLIB::EXP_F32, RTLIB::EXP_F64,
923 RTLIB::EXP_F80, RTLIB::EXP_F128,
926 GetPairElements(Call, Lo, Hi);
929 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
930 SDValue &Lo, SDValue &Hi) {
931 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
932 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
933 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
934 RTLIB::EXP2_PPCF128),
936 GetPairElements(Call, Lo, Hi);
939 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
940 SDValue &Lo, SDValue &Hi) {
941 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
942 RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
943 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
944 RTLIB::FLOOR_PPCF128),
946 GetPairElements(Call, Lo, Hi);
949 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
950 SDValue &Lo, SDValue &Hi) {
951 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
952 RTLIB::LOG_F32, RTLIB::LOG_F64,
953 RTLIB::LOG_F80, RTLIB::LOG_F128,
956 GetPairElements(Call, Lo, Hi);
959 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
960 SDValue &Lo, SDValue &Hi) {
961 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
962 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
963 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
964 RTLIB::LOG2_PPCF128),
966 GetPairElements(Call, Lo, Hi);
969 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
970 SDValue &Lo, SDValue &Hi) {
971 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
972 RTLIB::LOG10_F32, RTLIB::LOG10_F64,
973 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
974 RTLIB::LOG10_PPCF128),
976 GetPairElements(Call, Lo, Hi);
979 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
981 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
982 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
988 N->getValueType(0), Ops, 3, false,
990 GetPairElements(Call, Lo, Hi);
993 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
995 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
996 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1001 RTLIB::MUL_PPCF128),
1002 N->getValueType(0), Ops, 2, false,
1004 GetPairElements(Call, Lo, Hi);
1007 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1008 SDValue &Lo, SDValue &Hi) {
1009 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1010 RTLIB::NEARBYINT_F32,
1011 RTLIB::NEARBYINT_F64,
1012 RTLIB::NEARBYINT_F80,
1013 RTLIB::NEARBYINT_F128,
1014 RTLIB::NEARBYINT_PPCF128),
1016 GetPairElements(Call, Lo, Hi);
1019 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1021 DebugLoc dl = N->getDebugLoc();
1022 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1023 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1024 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1027 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1029 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1030 Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
1031 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1032 APInt(NVT.getSizeInBits(), 0)), NVT);
1035 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1036 SDValue &Lo, SDValue &Hi) {
1037 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1038 RTLIB::POW_F32, RTLIB::POW_F64,
1039 RTLIB::POW_F80, RTLIB::POW_F128,
1040 RTLIB::POW_PPCF128),
1042 GetPairElements(Call, Lo, Hi);
1045 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1046 SDValue &Lo, SDValue &Hi) {
1047 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1048 RTLIB::POWI_F32, RTLIB::POWI_F64,
1049 RTLIB::POWI_F80, RTLIB::POWI_F128,
1050 RTLIB::POWI_PPCF128),
1052 GetPairElements(Call, Lo, Hi);
1055 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1056 SDValue &Lo, SDValue &Hi) {
1057 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1058 RTLIB::REM_F32, RTLIB::REM_F64,
1059 RTLIB::REM_F80, RTLIB::REM_F128,
1060 RTLIB::REM_PPCF128),
1062 GetPairElements(Call, Lo, Hi);
1065 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1066 SDValue &Lo, SDValue &Hi) {
1067 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1068 RTLIB::RINT_F32, RTLIB::RINT_F64,
1069 RTLIB::RINT_F80, RTLIB::RINT_F128,
1070 RTLIB::RINT_PPCF128),
1072 GetPairElements(Call, Lo, Hi);
1075 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1076 SDValue &Lo, SDValue &Hi) {
1077 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1078 RTLIB::SIN_F32, RTLIB::SIN_F64,
1079 RTLIB::SIN_F80, RTLIB::SIN_F128,
1080 RTLIB::SIN_PPCF128),
1082 GetPairElements(Call, Lo, Hi);
1085 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1086 SDValue &Lo, SDValue &Hi) {
1087 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1088 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1089 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1090 RTLIB::SQRT_PPCF128),
1092 GetPairElements(Call, Lo, Hi);
1095 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1097 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1098 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1103 RTLIB::SUB_PPCF128),
1104 N->getValueType(0), Ops, 2, false,
1106 GetPairElements(Call, Lo, Hi);
1109 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1110 SDValue &Lo, SDValue &Hi) {
1111 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1112 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1113 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1114 RTLIB::TRUNC_PPCF128),
1116 GetPairElements(Call, Lo, Hi);
1119 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1121 if (ISD::isNormalLoad(N)) {
1122 ExpandRes_NormalLoad(N, Lo, Hi);
1126 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1127 LoadSDNode *LD = cast<LoadSDNode>(N);
1128 SDValue Chain = LD->getChain();
1129 SDValue Ptr = LD->getBasePtr();
1130 DebugLoc dl = N->getDebugLoc();
1132 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1133 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1134 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1136 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1137 LD->getPointerInfo(), LD->getMemoryVT(), LD->isVolatile(),
1138 LD->isNonTemporal(), LD->getAlignment());
1140 // Remember the chain.
1141 Chain = Hi.getValue(1);
1143 // The low part is zero.
1144 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1145 APInt(NVT.getSizeInBits(), 0)), NVT);
1147 // Modified the chain - switch anything that used the old chain to use the
1149 ReplaceValueWith(SDValue(LD, 1), Chain);
1152 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1154 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1155 EVT VT = N->getValueType(0);
1156 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1157 SDValue Src = N->getOperand(0);
1158 EVT SrcVT = Src.getValueType();
1159 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1160 DebugLoc dl = N->getDebugLoc();
1162 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1163 // When promoting partial word types to i32 we must honor the signedness,
1165 if (SrcVT.bitsLE(MVT::i32)) {
1166 // The integer can be represented exactly in an f64.
1167 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1169 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1170 APInt(NVT.getSizeInBits(), 0)), NVT);
1171 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1173 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1174 if (SrcVT.bitsLE(MVT::i64)) {
1175 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1177 LC = RTLIB::SINTTOFP_I64_PPCF128;
1178 } else if (SrcVT.bitsLE(MVT::i128)) {
1179 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1180 LC = RTLIB::SINTTOFP_I128_PPCF128;
1182 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1184 Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl);
1185 GetPairElements(Hi, Lo, Hi);
1191 // Unsigned - fix up the SINT_TO_FP value just calculated.
1192 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1193 SrcVT = Src.getValueType();
1195 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1196 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1197 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1198 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1199 ArrayRef<uint64_t> Parts;
1201 switch (SrcVT.getSimpleVT().SimpleTy) {
1203 llvm_unreachable("Unsupported UINT_TO_FP!");
1215 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1216 DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1219 Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
1220 Lo, Hi, DAG.getCondCode(ISD::SETLT));
1221 GetPairElements(Lo, Lo, Hi);
1225 //===----------------------------------------------------------------------===//
1226 // Float Operand Expansion
1227 //===----------------------------------------------------------------------===//
1229 /// ExpandFloatOperand - This method is called when the specified operand of the
1230 /// specified node is found to need expansion. At this point, all of the result
1231 /// types of the node are known to be legal, but other operands of the node may
1232 /// need promotion or expansion as well as the specified one.
1233 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1234 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1235 SDValue Res = SDValue();
1237 // See if the target wants to custom expand this node.
1238 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1241 switch (N->getOpcode()) {
1244 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1245 N->dump(&DAG); dbgs() << "\n";
1247 llvm_unreachable("Do not know how to expand this operator's operand!");
1249 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1250 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1251 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1253 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1254 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1255 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1256 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1257 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1258 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1259 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1263 // If the result is null, the sub-method took care of registering results etc.
1264 if (!Res.getNode()) return false;
1266 // If the result is N, the sub-method updated N in place. Tell the legalizer
1268 if (Res.getNode() == N)
1271 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1272 "Invalid operand expansion");
1274 ReplaceValueWith(SDValue(N, 0), Res);
1278 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1279 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1280 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1282 ISD::CondCode &CCCode,
1284 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1285 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1286 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1288 assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1290 // FIXME: This generated code sucks. We want to generate
1291 // FCMPU crN, hi1, hi2
1293 // FCMPU crN, lo1, lo2
1294 // The following can be improved, but not that much.
1295 SDValue Tmp1, Tmp2, Tmp3;
1296 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1297 LHSHi, RHSHi, ISD::SETOEQ);
1298 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
1299 LHSLo, RHSLo, CCCode);
1300 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1301 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1302 LHSHi, RHSHi, ISD::SETUNE);
1303 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1304 LHSHi, RHSHi, CCCode);
1305 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1306 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1307 NewRHS = SDValue(); // LHS is the result, not a compare.
1310 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1311 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1312 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1313 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1315 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1316 // against zero to select between true and false values.
1317 if (NewRHS.getNode() == 0) {
1318 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1319 CCCode = ISD::SETNE;
1322 // Update N to have the operands specified.
1323 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1324 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1325 N->getOperand(4)), 0);
1328 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1329 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1330 "Logic only correct for ppcf128!");
1332 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1333 // Round it the rest of the way (e.g. to f32) if needed.
1334 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
1335 N->getValueType(0), Hi, N->getOperand(1));
1338 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1339 EVT RVT = N->getValueType(0);
1340 DebugLoc dl = N->getDebugLoc();
1342 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1343 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1344 if (RVT == MVT::i32) {
1345 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1346 "Logic only correct for ppcf128!");
1347 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1348 N->getOperand(0), DAG.getValueType(MVT::f64));
1349 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1350 DAG.getIntPtrConstant(1));
1351 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1354 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1355 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1356 return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl);
1359 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1360 EVT RVT = N->getValueType(0);
1361 DebugLoc dl = N->getDebugLoc();
1363 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1364 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1365 if (RVT == MVT::i32) {
1366 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1367 "Logic only correct for ppcf128!");
1368 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1369 APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1370 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1371 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1372 // FIXME: generated code sucks.
1373 return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
1374 DAG.getNode(ISD::ADD, dl, MVT::i32,
1375 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1376 DAG.getNode(ISD::FSUB, dl,
1380 DAG.getConstant(0x80000000, MVT::i32)),
1381 DAG.getNode(ISD::FP_TO_SINT, dl,
1382 MVT::i32, N->getOperand(0)),
1383 DAG.getCondCode(ISD::SETGE));
1386 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1387 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1388 return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1392 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1393 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1394 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1395 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1397 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1398 // against zero to select between true and false values.
1399 if (NewRHS.getNode() == 0) {
1400 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1401 CCCode = ISD::SETNE;
1404 // Update N to have the operands specified.
1405 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1406 N->getOperand(2), N->getOperand(3),
1407 DAG.getCondCode(CCCode)), 0);
1410 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1411 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1412 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1413 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1415 // If ExpandSetCCOperands returned a scalar, use it.
1416 if (NewRHS.getNode() == 0) {
1417 assert(NewLHS.getValueType() == N->getValueType(0) &&
1418 "Unexpected setcc expansion!");
1422 // Otherwise, update N to have the operands specified.
1423 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1424 DAG.getCondCode(CCCode)), 0);
1427 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1428 if (ISD::isNormalStore(N))
1429 return ExpandOp_NormalStore(N, OpNo);
1431 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1432 assert(OpNo == 1 && "Can only expand the stored value so far");
1433 StoreSDNode *ST = cast<StoreSDNode>(N);
1435 SDValue Chain = ST->getChain();
1436 SDValue Ptr = ST->getBasePtr();
1438 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1439 ST->getValue().getValueType());
1440 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1441 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1445 GetExpandedOp(ST->getValue(), Lo, Hi);
1447 return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
1448 ST->getPointerInfo(),
1449 ST->getMemoryVT(), ST->isVolatile(),
1450 ST->isNonTemporal(), ST->getAlignment());