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 SDOperand R = SDOperand();
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::FADD: R = SoftenFloatRes_FADD(N); break;
63 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
64 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
65 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
66 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
67 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
68 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
69 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
70 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
71 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
72 case ISD::SINT_TO_FP: R = SoftenFloatRes_SINT_TO_FP(N); break;
73 case ISD::UINT_TO_FP: R = SoftenFloatRes_UINT_TO_FP(N); break;
76 // If R is null, the sub-method took care of registering the result.
78 SetSoftenedFloat(SDOperand(N, ResNo), R);
81 SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
82 return BitConvertToInteger(N->getOperand(0));
85 SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
86 // Convert the inputs to integers, and build a new pair out of them.
87 return DAG.getNode(ISD::BUILD_PAIR,
88 TLI.getTypeToTransformTo(N->getValueType(0)),
89 BitConvertToInteger(N->getOperand(0)),
90 BitConvertToInteger(N->getOperand(1)));
93 SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
94 return DAG.getConstant(N->getValueAPF().convertToAPInt(),
95 TLI.getTypeToTransformTo(N->getValueType(0)));
98 SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
99 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
100 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
101 GetSoftenedFloat(N->getOperand(1)) };
102 return MakeLibCall(GetFPLibCall(N->getValueType(0),
110 SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
111 SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
112 SDOperand RHS = BitConvertToInteger(N->getOperand(1));
114 MVT LVT = LHS.getValueType();
115 MVT RVT = RHS.getValueType();
117 unsigned LSize = LVT.getSizeInBits();
118 unsigned RSize = RVT.getSizeInBits();
120 // First get the sign bit of second operand.
121 SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
122 DAG.getConstant(RSize - 1,
123 TLI.getShiftAmountTy()));
124 SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
126 // Shift right or sign-extend it if the two operands have different types.
127 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
129 SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
130 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
131 SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
132 } else if (SizeDiff < 0) {
133 SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
134 SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
135 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
138 // Clear the sign bit of the first operand.
139 SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
140 DAG.getConstant(LSize - 1,
141 TLI.getShiftAmountTy()));
142 Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
143 LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
145 // Or the value with the sign bit.
146 return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
149 SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
150 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
151 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
152 GetSoftenedFloat(N->getOperand(1)) };
153 return MakeLibCall(GetFPLibCall(N->getValueType(0),
161 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
162 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
163 SDOperand Op = N->getOperand(0);
164 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
165 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
166 return MakeLibCall(LC, NVT, &Op, 1, false);
169 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
170 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
171 SDOperand Op = N->getOperand(0);
172 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
173 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
174 return MakeLibCall(LC, NVT, &Op, 1, false);
177 SDOperand DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
178 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
179 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
180 return MakeLibCall(GetFPLibCall(N->getValueType(0),
184 RTLIB::POWI_PPCF128),
188 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
189 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
190 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
191 GetSoftenedFloat(N->getOperand(1)) };
192 return MakeLibCall(GetFPLibCall(N->getValueType(0),
200 SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
201 LoadSDNode *L = cast<LoadSDNode>(N);
202 MVT VT = N->getValueType(0);
203 MVT NVT = TLI.getTypeToTransformTo(VT);
206 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
207 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
208 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
209 L->getSrcValue(), L->getSrcValueOffset(), NVT,
210 L->isVolatile(), L->getAlignment());
211 // Legalized the chain result - switch anything that used the old chain to
213 ReplaceValueWith(SDOperand(N, 1), NewL.getValue(1));
217 // Do a non-extending load followed by FP_EXTEND.
218 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
219 L->getMemoryVT(), L->getChain(),
220 L->getBasePtr(), L->getOffset(),
221 L->getSrcValue(), L->getSrcValueOffset(),
223 L->isVolatile(), L->getAlignment());
224 // Legalized the chain result - switch anything that used the old chain to
226 ReplaceValueWith(SDOperand(N, 1), NewL.getValue(1));
227 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NewL));
230 SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
231 SDOperand LHS = GetSoftenedFloat(N->getOperand(1));
232 SDOperand RHS = GetSoftenedFloat(N->getOperand(2));
233 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
236 SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
237 SDOperand LHS = GetSoftenedFloat(N->getOperand(2));
238 SDOperand RHS = GetSoftenedFloat(N->getOperand(3));
239 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
240 N->getOperand(1), LHS, RHS, N->getOperand(4));
243 SDOperand DAGTypeLegalizer::SoftenFloatRes_SINT_TO_FP(SDNode *N) {
244 SDOperand Op = N->getOperand(0);
245 MVT RVT = N->getValueType(0);
246 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), RVT);
247 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SINT_TO_FP!");
248 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
251 SDOperand DAGTypeLegalizer::SoftenFloatRes_UINT_TO_FP(SDNode *N) {
252 SDOperand Op = N->getOperand(0);
253 MVT RVT = N->getValueType(0);
254 RTLIB::Libcall LC = RTLIB::getUINTTOFP(Op.getValueType(), RVT);
255 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UINT_TO_FP!");
256 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
260 //===----------------------------------------------------------------------===//
261 // Operand Float to Integer Conversion..
262 //===----------------------------------------------------------------------===//
264 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
265 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
267 SDOperand Res = SDOperand();
269 switch (N->getOpcode()) {
272 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
273 N->dump(&DAG); cerr << "\n";
275 assert(0 && "Do not know how to soften this operator's operand!");
278 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
279 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
280 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
281 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
282 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
283 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
284 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
287 // If the result is null, the sub-method took care of registering results etc.
288 if (!Res.Val) return false;
290 // If the result is N, the sub-method updated N in place. Check to see if any
291 // operands are new, and if so, mark them.
293 // Mark N as new and remark N and its operands. This allows us to correctly
294 // revisit N if it needs another step of promotion and allows us to visit
295 // any new operands to N.
300 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
301 "Invalid operand expansion");
303 ReplaceValueWith(SDOperand(N, 0), Res);
307 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
308 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
309 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
310 ISD::CondCode &CCCode) {
311 SDOperand LHSInt = GetSoftenedFloat(NewLHS);
312 SDOperand RHSInt = GetSoftenedFloat(NewRHS);
313 MVT VT = NewLHS.getValueType();
315 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
317 // Expand into one or more soft-fp libcall(s).
318 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
322 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
326 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
330 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
334 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
338 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
342 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
345 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
348 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
351 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
354 // SETONE = SETOLT | SETOGT
355 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
358 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
361 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
364 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
367 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
370 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
372 default: assert(false && "Do not know how to soften this setcc!");
376 MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
377 SDOperand Ops[2] = { LHSInt, RHSInt };
378 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/);
379 NewRHS = DAG.getConstant(0, RetVT);
380 CCCode = TLI.getCmpLibcallCC(LC1);
381 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
382 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
383 NewLHS, NewRHS, DAG.getCondCode(CCCode));
384 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/);
385 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
386 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
387 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
388 NewRHS = SDOperand();
392 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
393 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
394 GetSoftenedFloat(N->getOperand(0)));
397 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
398 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
399 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
400 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
402 // If SoftenSetCCOperands returned a scalar, we need to compare the result
403 // against zero to select between true and false values.
404 if (NewRHS.Val == 0) {
405 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
409 // Update N to have the operands specified.
410 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
411 DAG.getCondCode(CCCode), NewLHS, NewRHS,
415 SDOperand DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
416 MVT RVT = N->getValueType(0);
417 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
418 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
419 SDOperand Op = GetSoftenedFloat(N->getOperand(0));
420 return MakeLibCall(LC, RVT, &Op, 1, false);
423 SDOperand DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
424 MVT RVT = N->getValueType(0);
425 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
426 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
427 SDOperand Op = GetSoftenedFloat(N->getOperand(0));
428 return MakeLibCall(LC, RVT, &Op, 1, false);
431 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
432 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
433 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
434 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
436 // If SoftenSetCCOperands returned a scalar, we need to compare the result
437 // against zero to select between true and false values.
438 if (NewRHS.Val == 0) {
439 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
443 // Update N to have the operands specified.
444 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
445 N->getOperand(2), N->getOperand(3),
446 DAG.getCondCode(CCCode));
449 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
450 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
451 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
452 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
454 // If SoftenSetCCOperands returned a scalar, use it.
455 if (NewRHS.Val == 0) {
456 assert(NewLHS.getValueType() == N->getValueType(0) &&
457 "Unexpected setcc expansion!");
461 // Otherwise, update N to have the operands specified.
462 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
463 DAG.getCondCode(CCCode));
466 SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
467 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
468 assert(OpNo == 1 && "Can only soften the stored value!");
469 StoreSDNode *ST = cast<StoreSDNode>(N);
470 SDOperand Val = ST->getValue();
472 if (ST->isTruncatingStore())
473 // Do an FP_ROUND followed by a non-truncating store.
474 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
475 Val, DAG.getIntPtrConstant(0)));
477 Val = GetSoftenedFloat(Val);
479 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
480 ST->getSrcValue(), ST->getSrcValueOffset(),
481 ST->isVolatile(), ST->getAlignment());
485 //===----------------------------------------------------------------------===//
486 // Float Result Expansion
487 //===----------------------------------------------------------------------===//
489 /// ExpandFloatResult - This method is called when the specified result of the
490 /// specified node is found to need expansion. At this point, the node may also
491 /// have invalid operands or may have other results that need promotion, we just
492 /// know that (at least) one result needs expansion.
493 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
494 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
496 Lo = Hi = SDOperand();
498 // See if the target wants to custom expand this node.
499 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
500 TargetLowering::Custom) {
501 // If the target wants to, allow it to lower this itself.
502 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
503 // Everything that once used N now uses P. We are guaranteed that the
504 // result value types of N and the result value types of P match.
505 ReplaceNodeWith(N, P);
510 switch (N->getOpcode()) {
513 cerr << "ExpandFloatResult #" << ResNo << ": ";
514 N->dump(&DAG); cerr << "\n";
516 assert(0 && "Do not know how to expand the result of this operator!");
519 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
520 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
521 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
522 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
524 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
525 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
526 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
527 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
529 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
530 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
531 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
532 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
533 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
534 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
535 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
536 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
537 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
538 case ISD::SINT_TO_FP:
539 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
542 // If Lo/Hi is null, the sub-method took care of registering results etc.
544 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
547 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
549 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
550 assert(NVT.getSizeInBits() == integerPartWidth &&
551 "Do not know how to expand this float constant!");
552 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
553 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
554 &C.getRawData()[1])), NVT);
555 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
556 &C.getRawData()[0])), NVT);
559 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
561 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
562 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
567 N->getValueType(0), Ops, 2,
569 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
570 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
573 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDOperand &Lo,
575 assert(N->getValueType(0) == MVT::ppcf128 &&
576 "Logic only correct for ppcf128!");
578 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
579 Hi = DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp);
580 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
581 Lo = DAG.getNode(ISD::SELECT_CC, Lo.getValueType(), Tmp, Hi, Lo,
582 DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo),
583 DAG.getCondCode(ISD::SETEQ));
586 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
588 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
589 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
594 N->getValueType(0), Ops, 2,
596 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
597 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
600 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
602 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
603 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
608 N->getValueType(0), Ops, 2,
610 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
611 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
614 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDOperand &Lo,
616 GetExpandedFloat(N->getOperand(0), Lo, Hi);
617 Lo = DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo);
618 Hi = DAG.getNode(ISD::FNEG, Hi.getValueType(), Hi);
621 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDOperand &Lo,
623 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
624 Hi = DAG.getNode(ISD::FP_EXTEND, NVT, N->getOperand(0));
625 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
628 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
630 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
631 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
636 N->getValueType(0), Ops, 2,
638 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
639 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
642 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
644 if (ISD::isNormalLoad(N)) {
645 ExpandRes_NormalLoad(N, Lo, Hi);
649 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
650 LoadSDNode *LD = cast<LoadSDNode>(N);
651 SDOperand Chain = LD->getChain();
652 SDOperand Ptr = LD->getBasePtr();
654 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
655 assert(NVT.isByteSized() && "Expanded type not byte sized!");
656 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
658 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
659 LD->getSrcValue(), LD->getSrcValueOffset(),
661 LD->isVolatile(), LD->getAlignment());
663 // Remember the chain.
664 Chain = Lo.getValue(1);
666 // The high part is undefined.
667 Hi = DAG.getNode(ISD::UNDEF, NVT);
669 // Modified the chain - switch anything that used the old chain to use the
671 ReplaceValueWith(SDOperand(LD, 1), Chain);
674 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
676 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
677 MVT VT = N->getValueType(0);
678 MVT NVT = TLI.getTypeToTransformTo(VT);
679 SDOperand Src = N->getOperand(0);
680 MVT SrcVT = Src.getValueType();
682 // First do an SINT_TO_FP, whether the original was signed or unsigned.
683 if (SrcVT.bitsLE(MVT::i32)) {
684 // The integer can be represented exactly in an f64.
685 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
686 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
687 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
689 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
690 if (SrcVT.bitsLE(MVT::i64)) {
691 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
692 LC = RTLIB::SINTTOFP_I64_PPCF128;
693 } else if (SrcVT.bitsLE(MVT::i128)) {
694 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
695 LC = RTLIB::SINTTOFP_I128_PPCF128;
697 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
699 Hi = MakeLibCall(LC, VT, &Src, 1, true);
700 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
701 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
704 if (N->getOpcode() == ISD::SINT_TO_FP)
707 // Unsigned - fix up the SINT_TO_FP value just calculated.
708 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
709 SrcVT = Src.getValueType();
711 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
712 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
713 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
714 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
715 const uint64_t *Parts = 0;
717 switch (SrcVT.getSimpleVT()) {
719 assert(false && "Unsupported UINT_TO_FP!");
728 Lo = DAG.getNode(ISD::FADD, VT, Hi,
729 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
731 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
732 DAG.getCondCode(ISD::SETLT));
733 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
734 DAG.getConstant(1, TLI.getPointerTy()));
735 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
736 DAG.getConstant(0, TLI.getPointerTy()));
740 //===----------------------------------------------------------------------===//
741 // Float Operand Expansion
742 //===----------------------------------------------------------------------===//
744 /// ExpandFloatOperand - This method is called when the specified operand of the
745 /// specified node is found to need expansion. At this point, all of the result
746 /// types of the node are known to be legal, but other operands of the node may
747 /// need promotion or expansion as well as the specified one.
748 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
749 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
750 SDOperand Res = SDOperand();
752 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
753 == TargetLowering::Custom)
754 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
757 switch (N->getOpcode()) {
760 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
761 N->dump(&DAG); cerr << "\n";
763 assert(0 && "Do not know how to expand this operator's operand!");
766 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
767 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
768 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
770 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
771 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
772 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
773 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
774 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
775 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
776 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
781 // If the result is null, the sub-method took care of registering results etc.
782 if (!Res.Val) return false;
783 // If the result is N, the sub-method updated N in place. Check to see if any
784 // operands are new, and if so, mark them.
786 // Mark N as new and remark N and its operands. This allows us to correctly
787 // revisit N if it needs another step of expansion and allows us to visit
788 // any new operands to N.
793 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
794 "Invalid operand expansion");
796 ReplaceValueWith(SDOperand(N, 0), Res);
800 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
801 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
802 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
804 ISD::CondCode &CCCode) {
805 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
806 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
807 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
809 MVT VT = NewLHS.getValueType();
810 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
812 // FIXME: This generated code sucks. We want to generate
813 // FCMP crN, hi1, hi2
815 // FCMP crN, lo1, lo2
816 // The following can be improved, but not that much.
817 SDOperand Tmp1, Tmp2, Tmp3;
818 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
819 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
820 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
821 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
822 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
823 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
824 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
825 NewRHS = SDOperand(); // LHS is the result, not a compare.
828 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
829 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
830 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
831 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
833 // If ExpandSetCCOperands returned a scalar, we need to compare the result
834 // against zero to select between true and false values.
835 if (NewRHS.Val == 0) {
836 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
840 // Update N to have the operands specified.
841 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
842 DAG.getCondCode(CCCode), NewLHS, NewRHS,
846 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
847 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
848 "Logic only correct for ppcf128!");
850 GetExpandedFloat(N->getOperand(0), Lo, Hi);
851 // Round it the rest of the way (e.g. to f32) if needed.
852 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
855 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
856 MVT RVT = N->getValueType(0);
857 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
858 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
859 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false);
862 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
863 MVT RVT = N->getValueType(0);
864 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
865 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
866 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
869 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
870 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
871 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
872 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
874 // If ExpandSetCCOperands returned a scalar, we need to compare the result
875 // against zero to select between true and false values.
876 if (NewRHS.Val == 0) {
877 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
881 // Update N to have the operands specified.
882 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
883 N->getOperand(2), N->getOperand(3),
884 DAG.getCondCode(CCCode));
887 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
888 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
889 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
890 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
892 // If ExpandSetCCOperands returned a scalar, use it.
893 if (NewRHS.Val == 0) {
894 assert(NewLHS.getValueType() == N->getValueType(0) &&
895 "Unexpected setcc expansion!");
899 // Otherwise, update N to have the operands specified.
900 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
901 DAG.getCondCode(CCCode));
904 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
905 if (ISD::isNormalStore(N))
906 return ExpandOp_NormalStore(N, OpNo);
908 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
909 assert(OpNo == 1 && "Can only expand the stored value so far");
910 StoreSDNode *ST = cast<StoreSDNode>(N);
912 SDOperand Chain = ST->getChain();
913 SDOperand Ptr = ST->getBasePtr();
915 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
916 assert(NVT.isByteSized() && "Expanded type not byte sized!");
917 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
920 GetExpandedOp(ST->getValue(), Lo, Hi);
922 return DAG.getTruncStore(Chain, Lo, Ptr,
923 ST->getSrcValue(), ST->getSrcValueOffset(),
925 ST->isVolatile(), ST->getAlignment());