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::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
535 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
536 case ISD::SINT_TO_FP:
537 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
540 // If Lo/Hi is null, the sub-method took care of registering results etc.
542 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
545 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
547 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
548 assert(NVT.getSizeInBits() == integerPartWidth &&
549 "Do not know how to expand this float constant!");
550 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
551 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
552 &C.getRawData()[1])), NVT);
553 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
554 &C.getRawData()[0])), NVT);
557 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
559 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
560 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
565 N->getValueType(0), Ops, 2,
567 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
568 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
571 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDOperand &Lo,
573 assert(N->getValueType(0) == MVT::ppcf128 &&
574 "Logic only correct for ppcf128!");
576 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
577 Hi = DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp);
578 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
579 Lo = DAG.getNode(ISD::SELECT_CC, Lo.getValueType(), Tmp, Hi, Lo,
580 DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo),
581 DAG.getCondCode(ISD::SETEQ));
584 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
586 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
587 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
592 N->getValueType(0), Ops, 2,
594 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
595 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
598 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
600 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
601 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
606 N->getValueType(0), Ops, 2,
608 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
609 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
612 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
614 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
615 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
620 N->getValueType(0), Ops, 2,
622 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
623 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
626 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
628 if (ISD::isNormalLoad(N)) {
629 ExpandRes_NormalLoad(N, Lo, Hi);
633 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
634 LoadSDNode *LD = cast<LoadSDNode>(N);
635 SDOperand Chain = LD->getChain();
636 SDOperand Ptr = LD->getBasePtr();
638 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
639 assert(NVT.isByteSized() && "Expanded type not byte sized!");
640 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
642 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
643 LD->getSrcValue(), LD->getSrcValueOffset(),
645 LD->isVolatile(), LD->getAlignment());
647 // Remember the chain.
648 Chain = Lo.getValue(1);
650 // The high part is undefined.
651 Hi = DAG.getNode(ISD::UNDEF, NVT);
653 // Modified the chain - switch anything that used the old chain to use the
655 ReplaceValueWith(SDOperand(LD, 1), Chain);
658 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
660 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
661 MVT VT = N->getValueType(0);
662 MVT NVT = TLI.getTypeToTransformTo(VT);
663 SDOperand Src = N->getOperand(0);
664 MVT SrcVT = Src.getValueType();
666 // First do an SINT_TO_FP, whether the original was signed or unsigned.
667 if (SrcVT.bitsLE(MVT::i32)) {
668 // The integer can be represented exactly in an f64.
669 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
670 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
671 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
673 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
674 if (SrcVT.bitsLE(MVT::i64)) {
675 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
676 LC = RTLIB::SINTTOFP_I64_PPCF128;
677 } else if (SrcVT.bitsLE(MVT::i128)) {
678 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
679 LC = RTLIB::SINTTOFP_I128_PPCF128;
681 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
683 Hi = MakeLibCall(LC, VT, &Src, 1, true);
684 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
685 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
688 if (N->getOpcode() == ISD::SINT_TO_FP)
691 // Unsigned - fix up the SINT_TO_FP value just calculated.
692 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
693 SrcVT = Src.getValueType();
695 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
696 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
697 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
698 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
699 const uint64_t *Parts = 0;
701 switch (SrcVT.getSimpleVT()) {
703 assert(false && "Unsupported UINT_TO_FP!");
712 Lo = DAG.getNode(ISD::FADD, VT, Hi,
713 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
715 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
716 DAG.getCondCode(ISD::SETLT));
717 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
718 DAG.getConstant(1, TLI.getPointerTy()));
719 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
720 DAG.getConstant(0, TLI.getPointerTy()));
724 //===----------------------------------------------------------------------===//
725 // Float Operand Expansion
726 //===----------------------------------------------------------------------===//
728 /// ExpandFloatOperand - This method is called when the specified operand of the
729 /// specified node is found to need expansion. At this point, all of the result
730 /// types of the node are known to be legal, but other operands of the node may
731 /// need promotion or expansion as well as the specified one.
732 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
733 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
734 SDOperand Res = SDOperand();
736 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
737 == TargetLowering::Custom)
738 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
741 switch (N->getOpcode()) {
744 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
745 N->dump(&DAG); cerr << "\n";
747 assert(0 && "Do not know how to expand this operator's operand!");
750 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
751 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
752 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
754 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
755 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
756 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
757 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
758 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
759 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
760 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
765 // If the result is null, the sub-method took care of registering results etc.
766 if (!Res.Val) return false;
767 // If the result is N, the sub-method updated N in place. Check to see if any
768 // operands are new, and if so, mark them.
770 // Mark N as new and remark N and its operands. This allows us to correctly
771 // revisit N if it needs another step of expansion and allows us to visit
772 // any new operands to N.
777 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
778 "Invalid operand expansion");
780 ReplaceValueWith(SDOperand(N, 0), Res);
784 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
785 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
786 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
788 ISD::CondCode &CCCode) {
789 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
790 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
791 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
793 MVT VT = NewLHS.getValueType();
794 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
796 // FIXME: This generated code sucks. We want to generate
797 // FCMP crN, hi1, hi2
799 // FCMP crN, lo1, lo2
800 // The following can be improved, but not that much.
801 SDOperand Tmp1, Tmp2, Tmp3;
802 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
803 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
804 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
805 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
806 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
807 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
808 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
809 NewRHS = SDOperand(); // LHS is the result, not a compare.
812 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
813 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
814 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
815 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
817 // If ExpandSetCCOperands returned a scalar, we need to compare the result
818 // against zero to select between true and false values.
819 if (NewRHS.Val == 0) {
820 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
824 // Update N to have the operands specified.
825 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
826 DAG.getCondCode(CCCode), NewLHS, NewRHS,
830 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
831 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
832 "Logic only correct for ppcf128!");
834 GetExpandedFloat(N->getOperand(0), Lo, Hi);
835 // Round it the rest of the way (e.g. to f32) if needed.
836 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
839 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
840 MVT RVT = N->getValueType(0);
841 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
842 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
843 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false);
846 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
847 MVT RVT = N->getValueType(0);
848 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
849 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
850 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
853 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
854 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
855 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
856 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
858 // If ExpandSetCCOperands returned a scalar, we need to compare the result
859 // against zero to select between true and false values.
860 if (NewRHS.Val == 0) {
861 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
865 // Update N to have the operands specified.
866 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
867 N->getOperand(2), N->getOperand(3),
868 DAG.getCondCode(CCCode));
871 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
872 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
873 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
874 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
876 // If ExpandSetCCOperands returned a scalar, use it.
877 if (NewRHS.Val == 0) {
878 assert(NewLHS.getValueType() == N->getValueType(0) &&
879 "Unexpected setcc expansion!");
883 // Otherwise, update N to have the operands specified.
884 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
885 DAG.getCondCode(CCCode));
888 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
889 if (ISD::isNormalStore(N))
890 return ExpandOp_NormalStore(N, OpNo);
892 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
893 assert(OpNo == 1 && "Can only expand the stored value so far");
894 StoreSDNode *ST = cast<StoreSDNode>(N);
896 SDOperand Chain = ST->getChain();
897 SDOperand Ptr = ST->getBasePtr();
899 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
900 assert(NVT.isByteSized() && "Expanded type not byte sized!");
901 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
904 GetExpandedOp(ST->getValue(), Lo, Hi);
906 return DAG.getTruncStore(Chain, Lo, Ptr,
907 ST->getSrcValue(), ST->getSrcValueOffset(),
909 ST->isVolatile(), ST->getAlignment());