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"
25 /// GetFPLibCall - Return the right libcall for the given floating point type.
26 static RTLIB::Libcall GetFPLibCall(MVT VT,
27 RTLIB::Libcall Call_F32,
28 RTLIB::Libcall Call_F64,
29 RTLIB::Libcall Call_F80,
30 RTLIB::Libcall Call_PPCF128) {
32 VT == MVT::f32 ? Call_F32 :
33 VT == MVT::f64 ? Call_F64 :
34 VT == MVT::f80 ? Call_F80 :
35 VT == MVT::ppcf128 ? Call_PPCF128 :
36 RTLIB::UNKNOWN_LIBCALL;
39 //===----------------------------------------------------------------------===//
40 // Result Float to Integer Conversion.
41 //===----------------------------------------------------------------------===//
43 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
44 DEBUG(cerr << "Soften float result " << ResNo << ": "; N->dump(&DAG);
46 SDValue R = SDValue();
48 switch (N->getOpcode()) {
51 cerr << "SoftenFloatResult #" << ResNo << ": ";
52 N->dump(&DAG); cerr << "\n";
54 assert(0 && "Do not know how to soften the result of this operator!");
57 case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
58 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
60 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
62 case ISD::EXTRACT_VECTOR_ELT:
63 R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
64 case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
65 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
66 case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
67 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
68 case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
69 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
70 case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
71 case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
72 case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
73 case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
74 case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
75 case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
76 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
77 case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
78 case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
79 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
80 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
81 case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
82 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
83 case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
84 case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
85 case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
86 case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
87 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
88 case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
89 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
90 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
91 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
93 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
94 case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
95 case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
98 // If R is null, the sub-method took care of registering the result.
100 SetSoftenedFloat(SDValue(N, ResNo), R);
103 SDValue DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
104 return BitConvertToInteger(N->getOperand(0));
107 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
108 // Convert the inputs to integers, and build a new pair out of them.
109 return DAG.getNode(ISD::BUILD_PAIR, N->getDebugLoc(),
110 TLI.getTypeToTransformTo(N->getValueType(0)),
111 BitConvertToInteger(N->getOperand(0)),
112 BitConvertToInteger(N->getOperand(1)));
115 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
116 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
117 TLI.getTypeToTransformTo(N->getValueType(0)));
120 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
121 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
122 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(),
123 NewOp.getValueType().getVectorElementType(),
124 NewOp, N->getOperand(1));
127 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
128 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
129 unsigned Size = NVT.getSizeInBits();
131 // Mask = ~(1 << (Size-1))
132 SDValue Mask = DAG.getConstant(APInt::getAllOnesValue(Size).clear(Size-1),
134 SDValue Op = GetSoftenedFloat(N->getOperand(0));
135 return DAG.getNode(ISD::AND, N->getDebugLoc(), NVT, Op, Mask);
138 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
139 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
140 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
141 GetSoftenedFloat(N->getOperand(1)) };
142 return MakeLibCall(GetFPLibCall(N->getValueType(0),
147 NVT, Ops, 2, false, N->getDebugLoc());
150 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
151 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
152 SDValue Op = GetSoftenedFloat(N->getOperand(0));
153 return MakeLibCall(GetFPLibCall(N->getValueType(0),
157 RTLIB::CEIL_PPCF128),
158 NVT, &Op, 1, false, N->getDebugLoc());
161 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
162 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
163 SDValue RHS = BitConvertToInteger(N->getOperand(1));
164 DebugLoc dl = N->getDebugLoc();
166 MVT LVT = LHS.getValueType();
167 MVT RVT = RHS.getValueType();
169 unsigned LSize = LVT.getSizeInBits();
170 unsigned RSize = RVT.getSizeInBits();
172 // First get the sign bit of second operand.
173 SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
174 DAG.getConstant(RSize - 1,
175 TLI.getShiftAmountTy()));
176 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
178 // Shift right or sign-extend it if the two operands have different types.
179 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
181 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
182 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
183 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
184 } else if (SizeDiff < 0) {
185 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
186 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
187 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
190 // Clear the sign bit of the first operand.
191 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
192 DAG.getConstant(LSize - 1,
193 TLI.getShiftAmountTy()));
194 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
195 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
197 // Or the value with the sign bit.
198 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
201 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
202 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
203 SDValue Op = GetSoftenedFloat(N->getOperand(0));
204 return MakeLibCall(GetFPLibCall(N->getValueType(0),
209 NVT, &Op, 1, false, N->getDebugLoc());
212 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
213 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
214 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
215 GetSoftenedFloat(N->getOperand(1)) };
216 return MakeLibCall(GetFPLibCall(N->getValueType(0),
221 NVT, Ops, 2, false, N->getDebugLoc());
224 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
225 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
226 SDValue Op = GetSoftenedFloat(N->getOperand(0));
227 return MakeLibCall(GetFPLibCall(N->getValueType(0),
232 NVT, &Op, 1, false, N->getDebugLoc());
235 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
236 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
237 SDValue Op = GetSoftenedFloat(N->getOperand(0));
238 return MakeLibCall(GetFPLibCall(N->getValueType(0),
242 RTLIB::EXP2_PPCF128),
243 NVT, &Op, 1, false, N->getDebugLoc());
246 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
247 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
248 SDValue Op = GetSoftenedFloat(N->getOperand(0));
249 return MakeLibCall(GetFPLibCall(N->getValueType(0),
253 RTLIB::FLOOR_PPCF128),
254 NVT, &Op, 1, false, N->getDebugLoc());
257 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
258 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
259 SDValue Op = GetSoftenedFloat(N->getOperand(0));
260 return MakeLibCall(GetFPLibCall(N->getValueType(0),
265 NVT, &Op, 1, false, N->getDebugLoc());
268 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
269 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
270 SDValue Op = GetSoftenedFloat(N->getOperand(0));
271 return MakeLibCall(GetFPLibCall(N->getValueType(0),
275 RTLIB::LOG2_PPCF128),
276 NVT, &Op, 1, false, N->getDebugLoc());
279 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
280 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
281 SDValue Op = GetSoftenedFloat(N->getOperand(0));
282 return MakeLibCall(GetFPLibCall(N->getValueType(0),
286 RTLIB::LOG10_PPCF128),
287 NVT, &Op, 1, false, N->getDebugLoc());
290 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
291 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
292 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
293 GetSoftenedFloat(N->getOperand(1)) };
294 return MakeLibCall(GetFPLibCall(N->getValueType(0),
299 NVT, Ops, 2, false, N->getDebugLoc());
302 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
303 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
304 SDValue Op = GetSoftenedFloat(N->getOperand(0));
305 return MakeLibCall(GetFPLibCall(N->getValueType(0),
306 RTLIB::NEARBYINT_F32,
307 RTLIB::NEARBYINT_F64,
308 RTLIB::NEARBYINT_F80,
309 RTLIB::NEARBYINT_PPCF128),
310 NVT, &Op, 1, false, N->getDebugLoc());
313 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
314 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
315 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
316 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
317 GetSoftenedFloat(N->getOperand(0)) };
318 return MakeLibCall(GetFPLibCall(N->getValueType(0),
323 NVT, Ops, 2, false, N->getDebugLoc());
326 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
327 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
328 SDValue Op = N->getOperand(0);
329 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
330 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
331 return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
334 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
335 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
336 SDValue Op = N->getOperand(0);
337 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
338 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
339 return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
342 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
343 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
344 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
345 GetSoftenedFloat(N->getOperand(1)) };
346 return MakeLibCall(GetFPLibCall(N->getValueType(0),
351 NVT, Ops, 2, false, N->getDebugLoc());
354 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
355 assert(N->getOperand(1).getValueType() == MVT::i32 &&
356 "Unsupported power type!");
357 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
358 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
359 return MakeLibCall(GetFPLibCall(N->getValueType(0),
363 RTLIB::POWI_PPCF128),
364 NVT, Ops, 2, false, N->getDebugLoc());
367 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
368 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
369 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
370 GetSoftenedFloat(N->getOperand(1)) };
371 return MakeLibCall(GetFPLibCall(N->getValueType(0),
376 NVT, Ops, 2, false, N->getDebugLoc());
379 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
380 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
381 SDValue Op = GetSoftenedFloat(N->getOperand(0));
382 return MakeLibCall(GetFPLibCall(N->getValueType(0),
386 RTLIB::RINT_PPCF128),
387 NVT, &Op, 1, false, N->getDebugLoc());
390 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
391 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
392 SDValue Op = GetSoftenedFloat(N->getOperand(0));
393 return MakeLibCall(GetFPLibCall(N->getValueType(0),
398 NVT, &Op, 1, false, N->getDebugLoc());
401 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
402 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
403 SDValue Op = GetSoftenedFloat(N->getOperand(0));
404 return MakeLibCall(GetFPLibCall(N->getValueType(0),
408 RTLIB::SQRT_PPCF128),
409 NVT, &Op, 1, false, N->getDebugLoc());
412 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
413 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
414 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
415 GetSoftenedFloat(N->getOperand(1)) };
416 return MakeLibCall(GetFPLibCall(N->getValueType(0),
421 NVT, Ops, 2, false, N->getDebugLoc());
424 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
425 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
426 SDValue Op = GetSoftenedFloat(N->getOperand(0));
427 return MakeLibCall(GetFPLibCall(N->getValueType(0),
431 RTLIB::TRUNC_PPCF128),
432 NVT, &Op, 1, false, N->getDebugLoc());
435 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
436 LoadSDNode *L = cast<LoadSDNode>(N);
437 MVT VT = N->getValueType(0);
438 MVT NVT = TLI.getTypeToTransformTo(VT);
439 DebugLoc dl = N->getDebugLoc();
442 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
443 NewL = DAG.getLoad(L->getAddressingMode(), dl, L->getExtensionType(),
444 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
445 L->getSrcValue(), L->getSrcValueOffset(), NVT,
446 L->isVolatile(), L->getAlignment());
447 // Legalized the chain result - switch anything that used the old chain to
449 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
453 // Do a non-extending load followed by FP_EXTEND.
454 NewL = DAG.getLoad(L->getAddressingMode(), dl, ISD::NON_EXTLOAD,
455 L->getMemoryVT(), L->getChain(),
456 L->getBasePtr(), L->getOffset(),
457 L->getSrcValue(), L->getSrcValueOffset(),
459 L->isVolatile(), L->getAlignment());
460 // Legalized the chain result - switch anything that used the old chain to
462 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
463 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
466 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
467 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
468 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
469 return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
470 LHS.getValueType(), N->getOperand(0),LHS,RHS);
473 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
474 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
475 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
476 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
477 LHS.getValueType(), N->getOperand(0),
478 N->getOperand(1), LHS, RHS, N->getOperand(4));
481 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
482 return DAG.getUNDEF(TLI.getTypeToTransformTo(N->getValueType(0)));
485 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
486 SDValue Chain = N->getOperand(0); // Get the chain.
487 SDValue Ptr = N->getOperand(1); // Get the pointer.
488 MVT VT = N->getValueType(0);
489 MVT NVT = TLI.getTypeToTransformTo(VT);
490 DebugLoc dl = N->getDebugLoc();
493 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2));
495 // Legalized the chain result - switch anything that used the old chain to
497 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
501 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
502 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
503 MVT SVT = N->getOperand(0).getValueType();
504 MVT RVT = N->getValueType(0);
506 DebugLoc dl = N->getDebugLoc();
508 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
509 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
510 // match. Look for an appropriate libcall.
511 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
512 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
513 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
514 NVT = (MVT::SimpleValueType)t;
515 // The source needs to big enough to hold the operand.
517 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
519 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
521 // Sign/zero extend the argument if the libcall takes a larger type.
522 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
523 NVT, N->getOperand(0));
524 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false, dl);
528 //===----------------------------------------------------------------------===//
529 // Operand Float to Integer Conversion..
530 //===----------------------------------------------------------------------===//
532 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
533 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
535 SDValue Res = SDValue();
537 switch (N->getOpcode()) {
540 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
541 N->dump(&DAG); cerr << "\n";
543 assert(0 && "Do not know how to soften this operator's operand!");
546 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
547 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
548 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
549 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
550 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
551 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
552 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
553 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
556 // If the result is null, the sub-method took care of registering results etc.
557 if (!Res.getNode()) return false;
559 // If the result is N, the sub-method updated N in place. Tell the legalizer
561 if (Res.getNode() == N)
564 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
565 "Invalid operand expansion");
567 ReplaceValueWith(SDValue(N, 0), Res);
571 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
572 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
573 void DAGTypeLegalizer::SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
574 ISD::CondCode &CCCode, DebugLoc dl) {
575 SDValue LHSInt = GetSoftenedFloat(NewLHS);
576 SDValue RHSInt = GetSoftenedFloat(NewRHS);
577 MVT VT = NewLHS.getValueType();
579 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
581 // Expand into one or more soft-fp libcall(s).
582 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
586 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
590 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
594 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
598 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
602 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
606 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
609 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
612 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
615 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
618 // SETONE = SETOLT | SETOGT
619 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
622 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
625 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
628 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
631 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
634 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
636 default: assert(false && "Do not know how to soften this setcc!");
640 MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
641 SDValue Ops[2] = { LHSInt, RHSInt };
642 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
643 NewRHS = DAG.getConstant(0, RetVT);
644 CCCode = TLI.getCmpLibcallCC(LC1);
645 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
646 SDValue Tmp = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT),
647 NewLHS, NewRHS, DAG.getCondCode(CCCode));
648 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
649 NewLHS = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT), NewLHS,
650 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
651 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
656 SDValue DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
657 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), N->getValueType(0),
658 GetSoftenedFloat(N->getOperand(0)));
661 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
662 MVT SVT = N->getOperand(0).getValueType();
663 MVT RVT = N->getValueType(0);
665 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
666 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
668 SDValue Op = GetSoftenedFloat(N->getOperand(0));
669 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
672 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
673 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
674 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
675 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
677 // If SoftenSetCCOperands returned a scalar, we need to compare the result
678 // against zero to select between true and false values.
679 if (NewRHS.getNode() == 0) {
680 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
684 // Update N to have the operands specified.
685 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
686 DAG.getCondCode(CCCode), NewLHS, NewRHS,
690 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
691 MVT RVT = N->getValueType(0);
692 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
693 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
694 SDValue Op = GetSoftenedFloat(N->getOperand(0));
695 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
698 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
699 MVT RVT = N->getValueType(0);
700 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
701 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
702 SDValue Op = GetSoftenedFloat(N->getOperand(0));
703 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
706 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
707 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
708 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
709 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
711 // If SoftenSetCCOperands returned a scalar, we need to compare the result
712 // against zero to select between true and false values.
713 if (NewRHS.getNode() == 0) {
714 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
718 // Update N to have the operands specified.
719 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
720 N->getOperand(2), N->getOperand(3),
721 DAG.getCondCode(CCCode));
724 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
725 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
726 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
727 SoftenSetCCOperands(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 DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
738 DAG.getCondCode(CCCode));
741 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
742 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
743 assert(OpNo == 1 && "Can only soften the stored value!");
744 StoreSDNode *ST = cast<StoreSDNode>(N);
745 SDValue Val = ST->getValue();
746 DebugLoc dl = N->getDebugLoc();
748 if (ST->isTruncatingStore())
749 // Do an FP_ROUND followed by a non-truncating store.
750 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
751 Val, DAG.getIntPtrConstant(0)));
753 Val = GetSoftenedFloat(Val);
755 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
756 ST->getSrcValue(), ST->getSrcValueOffset(),
757 ST->isVolatile(), ST->getAlignment());
761 //===----------------------------------------------------------------------===//
762 // Float Result Expansion
763 //===----------------------------------------------------------------------===//
765 /// ExpandFloatResult - This method is called when the specified result of the
766 /// specified node is found to need expansion. At this point, the node may also
767 /// have invalid operands or may have other results that need promotion, we just
768 /// know that (at least) one result needs expansion.
769 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
770 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
774 // See if the target wants to custom expand this node.
775 if (CustomLowerResults(N, N->getValueType(ResNo), true))
778 switch (N->getOpcode()) {
781 cerr << "ExpandFloatResult #" << ResNo << ": ";
782 N->dump(&DAG); cerr << "\n";
784 assert(0 && "Do not know how to expand the result of this operator!");
787 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
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::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
793 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
794 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
795 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
796 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
798 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
799 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
800 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
801 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
802 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
803 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
804 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
805 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
806 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
807 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
808 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
809 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
810 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
811 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
812 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
813 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
814 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
815 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
816 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
817 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
818 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
819 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
820 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
821 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
822 case ISD::SINT_TO_FP:
823 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
826 // If Lo/Hi is null, the sub-method took care of registering results etc.
828 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
831 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
833 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
834 assert(NVT.getSizeInBits() == integerPartWidth &&
835 "Do not know how to expand this float constant!");
836 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
837 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
838 &C.getRawData()[1])), NVT);
839 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
840 &C.getRawData()[0])), NVT);
843 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
845 assert(N->getValueType(0) == MVT::ppcf128 &&
846 "Logic only correct for ppcf128!");
847 DebugLoc dl = N->getDebugLoc();
849 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
850 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
851 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
852 Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
853 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
854 DAG.getCondCode(ISD::SETEQ));
857 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
859 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
860 RTLIB::ADD_F32, RTLIB::ADD_F64,
861 RTLIB::ADD_F80, RTLIB::ADD_PPCF128),
863 GetPairElements(Call, Lo, Hi);
866 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
867 SDValue &Lo, SDValue &Hi) {
868 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
869 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
870 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128),
872 GetPairElements(Call, Lo, Hi);
875 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
876 SDValue &Lo, SDValue &Hi) {
877 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
878 RTLIB::COS_F32, RTLIB::COS_F64,
879 RTLIB::COS_F80, RTLIB::COS_PPCF128),
881 GetPairElements(Call, Lo, Hi);
884 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
886 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
887 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
892 N->getValueType(0), Ops, 2, false,
894 GetPairElements(Call, Lo, Hi);
897 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
898 SDValue &Lo, SDValue &Hi) {
899 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
900 RTLIB::EXP_F32, RTLIB::EXP_F64,
901 RTLIB::EXP_F80, RTLIB::EXP_PPCF128),
903 GetPairElements(Call, Lo, Hi);
906 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
907 SDValue &Lo, SDValue &Hi) {
908 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
909 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
910 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128),
912 GetPairElements(Call, Lo, Hi);
915 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
916 SDValue &Lo, SDValue &Hi) {
917 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
918 RTLIB::FLOOR_F32,RTLIB::FLOOR_F64,
919 RTLIB::FLOOR_F80,RTLIB::FLOOR_PPCF128),
921 GetPairElements(Call, Lo, Hi);
924 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
925 SDValue &Lo, SDValue &Hi) {
926 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
927 RTLIB::LOG_F32, RTLIB::LOG_F64,
928 RTLIB::LOG_F80, RTLIB::LOG_PPCF128),
930 GetPairElements(Call, Lo, Hi);
933 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
934 SDValue &Lo, SDValue &Hi) {
935 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
936 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
937 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128),
939 GetPairElements(Call, Lo, Hi);
942 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
943 SDValue &Lo, SDValue &Hi) {
944 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
945 RTLIB::LOG10_F32,RTLIB::LOG10_F64,
946 RTLIB::LOG10_F80,RTLIB::LOG10_PPCF128),
948 GetPairElements(Call, Lo, Hi);
951 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
953 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
954 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
959 N->getValueType(0), Ops, 2, false,
961 GetPairElements(Call, Lo, Hi);
964 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
965 SDValue &Lo, SDValue &Hi) {
966 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
967 RTLIB::NEARBYINT_F32,
968 RTLIB::NEARBYINT_F64,
969 RTLIB::NEARBYINT_F80,
970 RTLIB::NEARBYINT_PPCF128),
972 GetPairElements(Call, Lo, Hi);
975 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
977 DebugLoc dl = N->getDebugLoc();
978 GetExpandedFloat(N->getOperand(0), Lo, Hi);
979 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
980 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
983 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
985 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
986 Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
987 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
990 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
991 SDValue &Lo, SDValue &Hi) {
992 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
993 RTLIB::POW_F32, RTLIB::POW_F64,
994 RTLIB::POW_F80, RTLIB::POW_PPCF128),
996 GetPairElements(Call, Lo, Hi);
999 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1000 SDValue &Lo, SDValue &Hi) {
1001 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1002 RTLIB::POWI_F32, RTLIB::POWI_F64,
1003 RTLIB::POWI_F80, RTLIB::POWI_PPCF128),
1005 GetPairElements(Call, Lo, Hi);
1008 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1009 SDValue &Lo, SDValue &Hi) {
1010 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1011 RTLIB::RINT_F32, RTLIB::RINT_F64,
1012 RTLIB::RINT_F80, RTLIB::RINT_PPCF128),
1014 GetPairElements(Call, Lo, Hi);
1017 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1018 SDValue &Lo, SDValue &Hi) {
1019 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1020 RTLIB::SIN_F32, RTLIB::SIN_F64,
1021 RTLIB::SIN_F80, RTLIB::SIN_PPCF128),
1023 GetPairElements(Call, Lo, Hi);
1026 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1027 SDValue &Lo, SDValue &Hi) {
1028 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1029 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1030 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128),
1032 GetPairElements(Call, Lo, Hi);
1035 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1037 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1038 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
1042 RTLIB::SUB_PPCF128),
1043 N->getValueType(0), Ops, 2, false,
1045 GetPairElements(Call, Lo, Hi);
1048 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1049 SDValue &Lo, SDValue &Hi) {
1050 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1051 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1052 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128),
1054 GetPairElements(Call, Lo, Hi);
1057 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1059 if (ISD::isNormalLoad(N)) {
1060 ExpandRes_NormalLoad(N, Lo, Hi);
1064 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1065 LoadSDNode *LD = cast<LoadSDNode>(N);
1066 SDValue Chain = LD->getChain();
1067 SDValue Ptr = LD->getBasePtr();
1068 DebugLoc dl = N->getDebugLoc();
1070 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
1071 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1072 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1074 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1075 LD->getSrcValue(), LD->getSrcValueOffset(),
1077 LD->isVolatile(), LD->getAlignment());
1079 // Remember the chain.
1080 Chain = Hi.getValue(1);
1082 // The low part is zero.
1083 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1085 // Modified the chain - switch anything that used the old chain to use the
1087 ReplaceValueWith(SDValue(LD, 1), Chain);
1090 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1092 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1093 MVT VT = N->getValueType(0);
1094 MVT NVT = TLI.getTypeToTransformTo(VT);
1095 SDValue Src = N->getOperand(0);
1096 MVT SrcVT = Src.getValueType();
1097 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1098 DebugLoc dl = N->getDebugLoc();
1100 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1101 // When promoting partial word types to i32 we must honor the signedness,
1103 if (SrcVT.bitsLE(MVT::i32)) {
1104 // The integer can be represented exactly in an f64.
1105 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1107 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1108 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1110 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1111 if (SrcVT.bitsLE(MVT::i64)) {
1112 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1114 LC = RTLIB::SINTTOFP_I64_PPCF128;
1115 } else if (SrcVT.bitsLE(MVT::i128)) {
1116 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1117 LC = RTLIB::SINTTOFP_I128_PPCF128;
1119 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1121 Hi = MakeLibCall(LC, VT, &Src, 1, true, dl);
1122 GetPairElements(Hi, Lo, Hi);
1128 // Unsigned - fix up the SINT_TO_FP value just calculated.
1129 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1130 SrcVT = Src.getValueType();
1132 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1133 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1134 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1135 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1136 const uint64_t *Parts = 0;
1138 switch (SrcVT.getSimpleVT()) {
1140 assert(false && "Unsupported UINT_TO_FP!");
1152 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1153 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
1155 Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
1156 Lo, Hi, DAG.getCondCode(ISD::SETLT));
1157 GetPairElements(Lo, Lo, Hi);
1161 //===----------------------------------------------------------------------===//
1162 // Float Operand Expansion
1163 //===----------------------------------------------------------------------===//
1165 /// ExpandFloatOperand - This method is called when the specified operand of the
1166 /// specified node is found to need expansion. At this point, all of the result
1167 /// types of the node are known to be legal, but other operands of the node may
1168 /// need promotion or expansion as well as the specified one.
1169 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1170 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
1171 SDValue Res = SDValue();
1173 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1174 == TargetLowering::Custom)
1175 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1177 if (Res.getNode() == 0) {
1178 switch (N->getOpcode()) {
1181 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
1182 N->dump(&DAG); cerr << "\n";
1184 assert(0 && "Do not know how to expand this operator's operand!");
1187 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
1188 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1189 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1191 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1192 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1193 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1194 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1195 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1196 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1197 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1202 // If the result is null, the sub-method took care of registering results etc.
1203 if (!Res.getNode()) return false;
1205 // If the result is N, the sub-method updated N in place. Tell the legalizer
1207 if (Res.getNode() == N)
1210 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1211 "Invalid operand expansion");
1213 ReplaceValueWith(SDValue(N, 0), Res);
1217 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1218 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1219 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1221 ISD::CondCode &CCCode,
1223 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1224 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1225 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1227 MVT VT = NewLHS.getValueType();
1228 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
1230 // FIXME: This generated code sucks. We want to generate
1231 // FCMPU crN, hi1, hi2
1233 // FCMPU crN, lo1, lo2
1234 // The following can be improved, but not that much.
1235 SDValue Tmp1, Tmp2, Tmp3;
1236 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1237 LHSHi, RHSHi, ISD::SETOEQ);
1238 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
1239 LHSLo, RHSLo, CCCode);
1240 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1241 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1242 LHSHi, RHSHi, ISD::SETUNE);
1243 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1244 LHSHi, RHSHi, CCCode);
1245 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1246 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1247 NewRHS = SDValue(); // LHS is the result, not a compare.
1250 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1251 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1252 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1253 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1255 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1256 // against zero to select between true and false values.
1257 if (NewRHS.getNode() == 0) {
1258 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1259 CCCode = ISD::SETNE;
1262 // Update N to have the operands specified.
1263 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
1264 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1268 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1269 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1270 "Logic only correct for ppcf128!");
1272 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1273 // Round it the rest of the way (e.g. to f32) if needed.
1274 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
1275 N->getValueType(0), Hi, N->getOperand(1));
1278 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1279 MVT RVT = N->getValueType(0);
1280 DebugLoc dl = N->getDebugLoc();
1282 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1283 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1284 if (RVT == MVT::i32) {
1285 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1286 "Logic only correct for ppcf128!");
1287 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1288 N->getOperand(0), DAG.getValueType(MVT::f64));
1289 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1290 DAG.getIntPtrConstant(1));
1291 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1294 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1295 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1296 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false, dl);
1299 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1300 MVT RVT = N->getValueType(0);
1301 DebugLoc dl = N->getDebugLoc();
1303 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1304 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1305 if (RVT == MVT::i32) {
1306 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1307 "Logic only correct for ppcf128!");
1308 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1309 APFloat APF = APFloat(APInt(128, 2, TwoE31));
1310 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1311 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1312 // FIXME: generated code sucks.
1313 return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
1314 DAG.getNode(ISD::ADD, dl, MVT::i32,
1315 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1316 DAG.getNode(ISD::FSUB, dl,
1320 DAG.getConstant(0x80000000, MVT::i32)),
1321 DAG.getNode(ISD::FP_TO_SINT, dl,
1322 MVT::i32, N->getOperand(0)),
1323 DAG.getCondCode(ISD::SETGE));
1326 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1327 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1328 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false, dl);
1331 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1332 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1333 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1334 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1336 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1337 // against zero to select between true and false values.
1338 if (NewRHS.getNode() == 0) {
1339 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1340 CCCode = ISD::SETNE;
1343 // Update N to have the operands specified.
1344 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
1345 N->getOperand(2), N->getOperand(3),
1346 DAG.getCondCode(CCCode));
1349 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1350 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1351 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1352 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1354 // If ExpandSetCCOperands returned a scalar, use it.
1355 if (NewRHS.getNode() == 0) {
1356 assert(NewLHS.getValueType() == N->getValueType(0) &&
1357 "Unexpected setcc expansion!");
1361 // Otherwise, update N to have the operands specified.
1362 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
1363 DAG.getCondCode(CCCode));
1366 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1367 if (ISD::isNormalStore(N))
1368 return ExpandOp_NormalStore(N, OpNo);
1370 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1371 assert(OpNo == 1 && "Can only expand the stored value so far");
1372 StoreSDNode *ST = cast<StoreSDNode>(N);
1374 SDValue Chain = ST->getChain();
1375 SDValue Ptr = ST->getBasePtr();
1377 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
1378 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1379 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1382 GetExpandedOp(ST->getValue(), Lo, Hi);
1384 return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
1385 ST->getSrcValue(), ST->getSrcValueOffset(),
1387 ST->isVolatile(), ST->getAlignment());