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::FABS: R = SoftenFloatRes_FABS(N); break;
63 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
64 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
65 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
66 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
67 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
68 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
69 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
70 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
71 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
72 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
73 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
74 case ISD::SINT_TO_FP: R = SoftenFloatRes_SINT_TO_FP(N); break;
75 case ISD::UINT_TO_FP: R = SoftenFloatRes_UINT_TO_FP(N); break;
78 // If R is null, the sub-method took care of registering the result.
80 SetSoftenedFloat(SDValue(N, ResNo), R);
83 SDValue DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
84 return BitConvertToInteger(N->getOperand(0));
87 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
88 // Convert the inputs to integers, and build a new pair out of them.
89 return DAG.getNode(ISD::BUILD_PAIR,
90 TLI.getTypeToTransformTo(N->getValueType(0)),
91 BitConvertToInteger(N->getOperand(0)),
92 BitConvertToInteger(N->getOperand(1)));
95 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
96 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
97 TLI.getTypeToTransformTo(N->getValueType(0)));
100 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
101 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
102 unsigned Size = NVT.getSizeInBits();
104 // Mask = ~(1 << (Size-1))
105 SDValue Mask = DAG.getConstant(APInt::getAllOnesValue(Size).clear(Size-1),
107 SDValue Op = GetSoftenedFloat(N->getOperand(0));
108 return DAG.getNode(ISD::AND, NVT, Op, Mask);
111 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
112 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
113 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
114 GetSoftenedFloat(N->getOperand(1)) };
115 return MakeLibCall(GetFPLibCall(N->getValueType(0),
123 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
124 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
125 SDValue RHS = BitConvertToInteger(N->getOperand(1));
127 MVT LVT = LHS.getValueType();
128 MVT RVT = RHS.getValueType();
130 unsigned LSize = LVT.getSizeInBits();
131 unsigned RSize = RVT.getSizeInBits();
133 // First get the sign bit of second operand.
134 SDValue SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
135 DAG.getConstant(RSize - 1,
136 TLI.getShiftAmountTy()));
137 SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
139 // Shift right or sign-extend it if the two operands have different types.
140 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
142 SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
143 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
144 SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
145 } else if (SizeDiff < 0) {
146 SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
147 SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
148 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
151 // Clear the sign bit of the first operand.
152 SDValue Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
153 DAG.getConstant(LSize - 1,
154 TLI.getShiftAmountTy()));
155 Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
156 LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
158 // Or the value with the sign bit.
159 return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
162 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
163 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
164 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
165 GetSoftenedFloat(N->getOperand(1)) };
166 return MakeLibCall(GetFPLibCall(N->getValueType(0),
174 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
175 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
176 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
177 GetSoftenedFloat(N->getOperand(1)) };
178 return MakeLibCall(GetFPLibCall(N->getValueType(0),
186 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
187 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
188 SDValue Op = N->getOperand(0);
189 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
190 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
191 return MakeLibCall(LC, NVT, &Op, 1, false);
194 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
195 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
196 SDValue Op = N->getOperand(0);
197 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
198 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
199 return MakeLibCall(LC, NVT, &Op, 1, false);
202 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
203 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
204 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
205 return MakeLibCall(GetFPLibCall(N->getValueType(0),
209 RTLIB::POWI_PPCF128),
213 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
214 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
215 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
216 GetSoftenedFloat(N->getOperand(1)) };
217 return MakeLibCall(GetFPLibCall(N->getValueType(0),
225 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
226 LoadSDNode *L = cast<LoadSDNode>(N);
227 MVT VT = N->getValueType(0);
228 MVT NVT = TLI.getTypeToTransformTo(VT);
231 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
232 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
233 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
234 L->getSrcValue(), L->getSrcValueOffset(), NVT,
235 L->isVolatile(), L->getAlignment());
236 // Legalized the chain result - switch anything that used the old chain to
238 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
242 // Do a non-extending load followed by FP_EXTEND.
243 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
244 L->getMemoryVT(), L->getChain(),
245 L->getBasePtr(), L->getOffset(),
246 L->getSrcValue(), L->getSrcValueOffset(),
248 L->isVolatile(), L->getAlignment());
249 // Legalized the chain result - switch anything that used the old chain to
251 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
252 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NewL));
255 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
256 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
257 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
258 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
261 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
262 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
263 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
264 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
265 N->getOperand(1), LHS, RHS, N->getOperand(4));
268 SDValue DAGTypeLegalizer::SoftenFloatRes_SINT_TO_FP(SDNode *N) {
269 SDValue Op = N->getOperand(0);
270 MVT RVT = N->getValueType(0);
271 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), RVT);
272 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SINT_TO_FP!");
273 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
276 SDValue DAGTypeLegalizer::SoftenFloatRes_UINT_TO_FP(SDNode *N) {
277 SDValue Op = N->getOperand(0);
278 MVT RVT = N->getValueType(0);
279 RTLIB::Libcall LC = RTLIB::getUINTTOFP(Op.getValueType(), RVT);
280 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UINT_TO_FP!");
281 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
285 //===----------------------------------------------------------------------===//
286 // Operand Float to Integer Conversion..
287 //===----------------------------------------------------------------------===//
289 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
290 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
292 SDValue Res = SDValue();
294 switch (N->getOpcode()) {
297 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
298 N->dump(&DAG); cerr << "\n";
300 assert(0 && "Do not know how to soften this operator's operand!");
303 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
304 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
305 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
306 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
307 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
308 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
309 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
310 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
313 // If the result is null, the sub-method took care of registering results etc.
314 if (!Res.getNode()) return false;
316 // If the result is N, the sub-method updated N in place. Check to see if any
317 // operands are new, and if so, mark them.
318 if (Res.getNode() == N) {
319 // Mark N as new and remark N and its operands. This allows us to correctly
320 // revisit N if it needs another step of promotion and allows us to visit
321 // any new operands to N.
326 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
327 "Invalid operand expansion");
329 ReplaceValueWith(SDValue(N, 0), Res);
333 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
334 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
335 void DAGTypeLegalizer::SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
336 ISD::CondCode &CCCode) {
337 SDValue LHSInt = GetSoftenedFloat(NewLHS);
338 SDValue RHSInt = GetSoftenedFloat(NewRHS);
339 MVT VT = NewLHS.getValueType();
341 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
343 // Expand into one or more soft-fp libcall(s).
344 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
348 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
352 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
356 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
360 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
364 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
368 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
371 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
374 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
377 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
380 // SETONE = SETOLT | SETOGT
381 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
384 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
387 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
390 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
393 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
396 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
398 default: assert(false && "Do not know how to soften this setcc!");
402 MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
403 SDValue Ops[2] = { LHSInt, RHSInt };
404 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/);
405 NewRHS = DAG.getConstant(0, RetVT);
406 CCCode = TLI.getCmpLibcallCC(LC1);
407 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
408 SDValue Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
409 NewLHS, NewRHS, DAG.getCondCode(CCCode));
410 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/);
411 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
412 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
413 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
418 SDValue DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
419 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
420 GetSoftenedFloat(N->getOperand(0)));
423 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
424 MVT SVT = N->getOperand(0).getValueType();
425 MVT RVT = N->getValueType(0);
427 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
428 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
430 SDValue Op = GetSoftenedFloat(N->getOperand(0));
431 return MakeLibCall(LC, RVT, &Op, 1, false);
434 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
435 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
436 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
437 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
439 // If SoftenSetCCOperands returned a scalar, we need to compare the result
440 // against zero to select between true and false values.
441 if (NewRHS.getNode() == 0) {
442 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
446 // Update N to have the operands specified.
447 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
448 DAG.getCondCode(CCCode), NewLHS, NewRHS,
452 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
453 MVT RVT = N->getValueType(0);
454 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
455 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
456 SDValue Op = GetSoftenedFloat(N->getOperand(0));
457 return MakeLibCall(LC, RVT, &Op, 1, false);
460 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
461 MVT RVT = N->getValueType(0);
462 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
463 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
464 SDValue Op = GetSoftenedFloat(N->getOperand(0));
465 return MakeLibCall(LC, RVT, &Op, 1, false);
468 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
469 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
470 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
471 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
473 // If SoftenSetCCOperands returned a scalar, we need to compare the result
474 // against zero to select between true and false values.
475 if (NewRHS.getNode() == 0) {
476 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
480 // Update N to have the operands specified.
481 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
482 N->getOperand(2), N->getOperand(3),
483 DAG.getCondCode(CCCode));
486 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
487 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
488 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
489 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
491 // If SoftenSetCCOperands returned a scalar, use it.
492 if (NewRHS.getNode() == 0) {
493 assert(NewLHS.getValueType() == N->getValueType(0) &&
494 "Unexpected setcc expansion!");
498 // Otherwise, update N to have the operands specified.
499 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
500 DAG.getCondCode(CCCode));
503 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
504 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
505 assert(OpNo == 1 && "Can only soften the stored value!");
506 StoreSDNode *ST = cast<StoreSDNode>(N);
507 SDValue Val = ST->getValue();
509 if (ST->isTruncatingStore())
510 // Do an FP_ROUND followed by a non-truncating store.
511 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
512 Val, DAG.getIntPtrConstant(0)));
514 Val = GetSoftenedFloat(Val);
516 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
517 ST->getSrcValue(), ST->getSrcValueOffset(),
518 ST->isVolatile(), ST->getAlignment());
522 //===----------------------------------------------------------------------===//
523 // Float Result Expansion
524 //===----------------------------------------------------------------------===//
526 /// ExpandFloatResult - This method is called when the specified result of the
527 /// specified node is found to need expansion. At this point, the node may also
528 /// have invalid operands or may have other results that need promotion, we just
529 /// know that (at least) one result needs expansion.
530 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
531 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
535 // See if the target wants to custom expand this node.
536 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
537 TargetLowering::Custom) {
538 // If the target wants to, allow it to lower this itself.
539 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
540 // Everything that once used N now uses P. We are guaranteed that the
541 // result value types of N and the result value types of P match.
542 ReplaceNodeWith(N, P);
547 switch (N->getOpcode()) {
550 cerr << "ExpandFloatResult #" << ResNo << ": ";
551 N->dump(&DAG); cerr << "\n";
553 assert(0 && "Do not know how to expand the result of this operator!");
556 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
557 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
558 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
559 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
561 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
562 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
563 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
564 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
566 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
567 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
568 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
569 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
570 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
571 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
572 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
573 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
574 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
575 case ISD::SINT_TO_FP:
576 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
579 // If Lo/Hi is null, the sub-method took care of registering results etc.
581 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
584 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
586 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
587 assert(NVT.getSizeInBits() == integerPartWidth &&
588 "Do not know how to expand this float constant!");
589 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
590 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
591 &C.getRawData()[1])), NVT);
592 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
593 &C.getRawData()[0])), NVT);
596 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
598 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
599 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
604 N->getValueType(0), Ops, 2,
606 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
607 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
610 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
612 assert(N->getValueType(0) == MVT::ppcf128 &&
613 "Logic only correct for ppcf128!");
615 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
616 Hi = DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp);
617 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
618 Lo = DAG.getNode(ISD::SELECT_CC, Lo.getValueType(), Tmp, Hi, Lo,
619 DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo),
620 DAG.getCondCode(ISD::SETEQ));
623 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
625 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
626 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
631 N->getValueType(0), Ops, 2,
633 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
634 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
637 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
639 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
640 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
645 N->getValueType(0), Ops, 2,
647 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
648 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
651 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
653 GetExpandedFloat(N->getOperand(0), Lo, Hi);
654 Lo = DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo);
655 Hi = DAG.getNode(ISD::FNEG, Hi.getValueType(), Hi);
658 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
660 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
661 Hi = DAG.getNode(ISD::FP_EXTEND, NVT, N->getOperand(0));
662 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
665 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
667 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
668 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
673 N->getValueType(0), Ops, 2,
675 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
676 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
679 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
681 if (ISD::isNormalLoad(N)) {
682 ExpandRes_NormalLoad(N, Lo, Hi);
686 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
687 LoadSDNode *LD = cast<LoadSDNode>(N);
688 SDValue Chain = LD->getChain();
689 SDValue Ptr = LD->getBasePtr();
691 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
692 assert(NVT.isByteSized() && "Expanded type not byte sized!");
693 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
695 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
696 LD->getSrcValue(), LD->getSrcValueOffset(),
698 LD->isVolatile(), LD->getAlignment());
700 // Remember the chain.
701 Chain = Lo.getValue(1);
703 // The high part is undefined.
704 Hi = DAG.getNode(ISD::UNDEF, NVT);
706 // Modified the chain - switch anything that used the old chain to use the
708 ReplaceValueWith(SDValue(LD, 1), Chain);
711 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
713 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
714 MVT VT = N->getValueType(0);
715 MVT NVT = TLI.getTypeToTransformTo(VT);
716 SDValue Src = N->getOperand(0);
717 MVT SrcVT = Src.getValueType();
719 // First do an SINT_TO_FP, whether the original was signed or unsigned.
720 if (SrcVT.bitsLE(MVT::i32)) {
721 // The integer can be represented exactly in an f64.
722 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
723 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
724 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
726 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
727 if (SrcVT.bitsLE(MVT::i64)) {
728 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
729 LC = RTLIB::SINTTOFP_I64_PPCF128;
730 } else if (SrcVT.bitsLE(MVT::i128)) {
731 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
732 LC = RTLIB::SINTTOFP_I128_PPCF128;
734 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
736 Hi = MakeLibCall(LC, VT, &Src, 1, true);
737 assert(Hi.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
738 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
741 if (N->getOpcode() == ISD::SINT_TO_FP)
744 // Unsigned - fix up the SINT_TO_FP value just calculated.
745 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
746 SrcVT = Src.getValueType();
748 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
749 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
750 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
751 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
752 const uint64_t *Parts = 0;
754 switch (SrcVT.getSimpleVT()) {
756 assert(false && "Unsupported UINT_TO_FP!");
765 Lo = DAG.getNode(ISD::FADD, VT, Hi,
766 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
768 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
769 DAG.getCondCode(ISD::SETLT));
770 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
771 DAG.getConstant(1, TLI.getPointerTy()));
772 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
773 DAG.getConstant(0, TLI.getPointerTy()));
777 //===----------------------------------------------------------------------===//
778 // Float Operand Expansion
779 //===----------------------------------------------------------------------===//
781 /// ExpandFloatOperand - This method is called when the specified operand of the
782 /// specified node is found to need expansion. At this point, all of the result
783 /// types of the node are known to be legal, but other operands of the node may
784 /// need promotion or expansion as well as the specified one.
785 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
786 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
787 SDValue Res = SDValue();
789 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
790 == TargetLowering::Custom)
791 Res = TLI.LowerOperation(SDValue(N, OpNo), DAG);
793 if (Res.getNode() == 0) {
794 switch (N->getOpcode()) {
797 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
798 N->dump(&DAG); cerr << "\n";
800 assert(0 && "Do not know how to expand this operator's operand!");
803 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
804 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
805 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
807 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
808 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
809 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
810 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
811 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
812 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
813 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
818 // If the result is null, the sub-method took care of registering results etc.
819 if (!Res.getNode()) return false;
820 // If the result is N, the sub-method updated N in place. Check to see if any
821 // operands are new, and if so, mark them.
822 if (Res.getNode() == N) {
823 // Mark N as new and remark N and its operands. This allows us to correctly
824 // revisit N if it needs another step of expansion and allows us to visit
825 // any new operands to N.
830 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
831 "Invalid operand expansion");
833 ReplaceValueWith(SDValue(N, 0), Res);
837 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
838 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
839 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
841 ISD::CondCode &CCCode) {
842 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
843 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
844 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
846 MVT VT = NewLHS.getValueType();
847 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
849 // FIXME: This generated code sucks. We want to generate
850 // FCMP crN, hi1, hi2
852 // FCMP crN, lo1, lo2
853 // The following can be improved, but not that much.
854 SDValue Tmp1, Tmp2, Tmp3;
855 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
856 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
857 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
858 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
859 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
860 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
861 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
862 NewRHS = SDValue(); // LHS is the result, not a compare.
865 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
866 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
867 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
868 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
870 // If ExpandSetCCOperands returned a scalar, we need to compare the result
871 // against zero to select between true and false values.
872 if (NewRHS.getNode() == 0) {
873 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
877 // Update N to have the operands specified.
878 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
879 DAG.getCondCode(CCCode), NewLHS, NewRHS,
883 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
884 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
885 "Logic only correct for ppcf128!");
887 GetExpandedFloat(N->getOperand(0), Lo, Hi);
888 // Round it the rest of the way (e.g. to f32) if needed.
889 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
892 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
893 MVT RVT = N->getValueType(0);
894 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
895 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
896 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false);
899 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
900 MVT RVT = N->getValueType(0);
901 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
902 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
903 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
906 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
907 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
908 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
909 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
911 // If ExpandSetCCOperands returned a scalar, we need to compare the result
912 // against zero to select between true and false values.
913 if (NewRHS.getNode() == 0) {
914 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
918 // Update N to have the operands specified.
919 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
920 N->getOperand(2), N->getOperand(3),
921 DAG.getCondCode(CCCode));
924 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
925 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
926 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
927 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
929 // If ExpandSetCCOperands returned a scalar, use it.
930 if (NewRHS.getNode() == 0) {
931 assert(NewLHS.getValueType() == N->getValueType(0) &&
932 "Unexpected setcc expansion!");
936 // Otherwise, update N to have the operands specified.
937 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
938 DAG.getCondCode(CCCode));
941 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
942 if (ISD::isNormalStore(N))
943 return ExpandOp_NormalStore(N, OpNo);
945 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
946 assert(OpNo == 1 && "Can only expand the stored value so far");
947 StoreSDNode *ST = cast<StoreSDNode>(N);
949 SDValue Chain = ST->getChain();
950 SDValue Ptr = ST->getBasePtr();
952 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
953 assert(NVT.isByteSized() && "Expanded type not byte sized!");
954 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
957 GetExpandedOp(ST->getValue(), Lo, Hi);
959 return DAG.getTruncStore(Chain, Lo, Ptr,
960 ST->getSrcValue(), ST->getSrcValueOffset(),
962 ST->isVolatile(), ST->getAlignment());