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::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
63 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
64 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
65 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
66 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
67 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
68 case ISD::SINT_TO_FP: R = SoftenFloatRes_SINT_TO_FP(N); break;
69 case ISD::UINT_TO_FP: R = SoftenFloatRes_UINT_TO_FP(N); break;
71 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
72 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
73 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
74 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
77 // If R is null, the sub-method took care of registering the result.
79 SetSoftenedFloat(SDOperand(N, ResNo), R);
82 SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
83 return BitConvertToInteger(N->getOperand(0));
86 SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
87 // Convert the inputs to integers, and build a new pair out of them.
88 return DAG.getNode(ISD::BUILD_PAIR,
89 TLI.getTypeToTransformTo(N->getValueType(0)),
90 BitConvertToInteger(N->getOperand(0)),
91 BitConvertToInteger(N->getOperand(1)));
94 SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
95 return DAG.getConstant(N->getValueAPF().convertToAPInt(),
96 TLI.getTypeToTransformTo(N->getValueType(0)));
99 SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
100 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
101 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
102 GetSoftenedFloat(N->getOperand(1)) };
103 return MakeLibCall(GetFPLibCall(N->getValueType(0),
111 SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
112 SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
113 SDOperand RHS = BitConvertToInteger(N->getOperand(1));
115 MVT LVT = LHS.getValueType();
116 MVT RVT = RHS.getValueType();
118 unsigned LSize = LVT.getSizeInBits();
119 unsigned RSize = RVT.getSizeInBits();
121 // First get the sign bit of second operand.
122 SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
123 DAG.getConstant(RSize - 1,
124 TLI.getShiftAmountTy()));
125 SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
127 // Shift right or sign-extend it if the two operands have different types.
128 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
130 SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
131 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
132 SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
133 } else if (SizeDiff < 0) {
134 SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
135 SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
136 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
139 // Clear the sign bit of the first operand.
140 SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
141 DAG.getConstant(LSize - 1,
142 TLI.getShiftAmountTy()));
143 Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
144 LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
146 // Or the value with the sign bit.
147 return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
150 SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
151 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
152 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
153 GetSoftenedFloat(N->getOperand(1)) };
154 return MakeLibCall(GetFPLibCall(N->getValueType(0),
162 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
163 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
164 SDOperand Op = N->getOperand(0);
166 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
167 switch (Op.getValueType().getSimpleVT()) {
169 assert(false && "Unsupported FP_EXTEND!");
171 switch (N->getValueType(0).getSimpleVT()) {
173 assert(false && "Unsupported FP_EXTEND!");
175 LC = RTLIB::FPEXT_F32_F64;
179 return MakeLibCall(LC, NVT, &Op, 1, false);
182 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
183 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
184 SDOperand Op = N->getOperand(0);
186 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
187 switch (Op.getValueType().getSimpleVT()) {
189 assert(false && "Unsupported FP_ROUND!");
191 switch (N->getValueType(0).getSimpleVT()) {
193 assert(false && "Unsupported FP_ROUND!");
195 LC = RTLIB::FPROUND_F64_F32;
199 return MakeLibCall(LC, NVT, &Op, 1, false);
202 SDOperand DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
203 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
204 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
205 return MakeLibCall(GetFPLibCall(N->getValueType(0),
209 RTLIB::POWI_PPCF128),
213 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
214 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
215 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
216 GetSoftenedFloat(N->getOperand(1)) };
217 return MakeLibCall(GetFPLibCall(N->getValueType(0),
225 SDOperand 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(SDOperand(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(SDOperand(N, 1), NewL.getValue(1));
252 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NewL));
255 SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
256 SDOperand LHS = GetSoftenedFloat(N->getOperand(1));
257 SDOperand RHS = GetSoftenedFloat(N->getOperand(2));
258 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
261 SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
262 SDOperand LHS = GetSoftenedFloat(N->getOperand(2));
263 SDOperand 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 SDOperand DAGTypeLegalizer::SoftenFloatRes_SINT_TO_FP(SDNode *N) {
269 SDOperand Op = N->getOperand(0);
270 MVT RVT = N->getValueType(0);
272 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
273 switch (Op.getValueType().getSimpleVT()) {
275 switch (RVT.getSimpleVT()) {
277 LC = RTLIB::SINTTOFP_I32_F32;
280 LC = RTLIB::SINTTOFP_I32_F64;
287 switch (RVT.getSimpleVT()) {
289 LC = RTLIB::SINTTOFP_I64_F32;
292 LC = RTLIB::SINTTOFP_I64_F64;
295 LC = RTLIB::SINTTOFP_I64_F80;
298 LC = RTLIB::SINTTOFP_I64_PPCF128;
305 switch (RVT.getSimpleVT()) {
307 LC = RTLIB::SINTTOFP_I128_F32;
310 LC = RTLIB::SINTTOFP_I128_F64;
313 LC = RTLIB::SINTTOFP_I128_F80;
316 LC = RTLIB::SINTTOFP_I128_PPCF128;
325 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SINT_TO_FP!");
327 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
330 SDOperand DAGTypeLegalizer::SoftenFloatRes_UINT_TO_FP(SDNode *N) {
331 SDOperand Op = N->getOperand(0);
332 MVT RVT = N->getValueType(0);
334 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
335 switch (Op.getValueType().getSimpleVT()) {
337 switch (RVT.getSimpleVT()) {
339 LC = RTLIB::UINTTOFP_I32_F32;
342 LC = RTLIB::UINTTOFP_I32_F64;
349 switch (RVT.getSimpleVT()) {
351 LC = RTLIB::UINTTOFP_I64_F32;
354 LC = RTLIB::UINTTOFP_I64_F64;
363 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UINT_TO_FP!");
365 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
369 //===----------------------------------------------------------------------===//
370 // Operand Float to Integer Conversion..
371 //===----------------------------------------------------------------------===//
373 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
374 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
376 SDOperand Res = SDOperand();
378 switch (N->getOpcode()) {
381 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
382 N->dump(&DAG); cerr << "\n";
384 assert(0 && "Do not know how to soften this operator's operand!");
387 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
389 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
390 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
391 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
392 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
393 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
394 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
397 // If the result is null, the sub-method took care of registering results etc.
398 if (!Res.Val) return false;
400 // If the result is N, the sub-method updated N in place. Check to see if any
401 // operands are new, and if so, mark them.
403 // Mark N as new and remark N and its operands. This allows us to correctly
404 // revisit N if it needs another step of promotion and allows us to visit
405 // any new operands to N.
410 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
411 "Invalid operand expansion");
413 ReplaceValueWith(SDOperand(N, 0), Res);
417 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
418 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
419 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
420 ISD::CondCode &CCCode) {
421 SDOperand LHSInt = GetSoftenedFloat(NewLHS);
422 SDOperand RHSInt = GetSoftenedFloat(NewRHS);
423 MVT VT = NewLHS.getValueType();
425 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
427 // Expand into one or more soft-fp libcall(s).
428 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
432 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
436 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
440 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
444 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
448 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
452 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
455 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
458 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
461 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
464 // SETONE = SETOLT | SETOGT
465 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
468 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
471 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
474 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
477 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
480 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
482 default: assert(false && "Do not know how to soften this setcc!");
486 MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
487 SDOperand Ops[2] = { LHSInt, RHSInt };
488 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/);
489 NewRHS = DAG.getConstant(0, RetVT);
490 CCCode = TLI.getCmpLibcallCC(LC1);
491 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
492 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
493 NewLHS, NewRHS, DAG.getCondCode(CCCode));
494 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/);
495 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
496 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
497 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
498 NewRHS = SDOperand();
502 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
503 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
504 GetSoftenedFloat(N->getOperand(0)));
507 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
508 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
509 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
510 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
512 // If SoftenSetCCOperands returned a scalar, we need to compare the result
513 // against zero to select between true and false values.
514 if (NewRHS.Val == 0) {
515 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
519 // Update N to have the operands specified.
520 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
521 DAG.getCondCode(CCCode), NewLHS, NewRHS,
525 SDOperand DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
526 MVT SVT = N->getOperand(0).getValueType();
527 MVT RVT = N->getValueType(0);
529 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
530 switch (RVT.getSimpleVT()) {
532 switch (SVT.getSimpleVT()) {
534 LC = RTLIB::FPTOSINT_F32_I32;
537 LC = RTLIB::FPTOSINT_F64_I32;
540 LC = RTLIB::FPTOSINT_F80_I32;
543 LC = RTLIB::FPTOSINT_PPCF128_I32;
550 switch (SVT.getSimpleVT()) {
552 LC = RTLIB::FPTOSINT_F32_I64;
555 LC = RTLIB::FPTOSINT_F64_I64;
558 LC = RTLIB::FPTOSINT_F80_I64;
561 LC = RTLIB::FPTOSINT_PPCF128_I64;
568 switch (SVT.getSimpleVT()) {
570 LC = RTLIB::FPTOSINT_F32_I128;
573 LC = RTLIB::FPTOSINT_F64_I128;
576 LC = RTLIB::FPTOSINT_F80_I128;
579 LC = RTLIB::FPTOSINT_PPCF128_I128;
588 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
590 SDOperand Op = GetSoftenedFloat(N->getOperand(0));
591 return MakeLibCall(LC, RVT, &Op, 1, false);
594 SDOperand DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
595 MVT SVT = N->getOperand(0).getValueType();
596 MVT RVT = N->getValueType(0);
598 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
599 switch (RVT.getSimpleVT()) {
601 switch (SVT.getSimpleVT()) {
603 LC = RTLIB::FPTOUINT_F32_I32;
606 LC = RTLIB::FPTOUINT_F64_I32;
609 LC = RTLIB::FPTOUINT_F80_I32;
612 LC = RTLIB::FPTOUINT_PPCF128_I32;
619 switch (SVT.getSimpleVT()) {
621 LC = RTLIB::FPTOUINT_F32_I64;
624 LC = RTLIB::FPTOUINT_F64_I64;
627 LC = RTLIB::FPTOUINT_F80_I64;
630 LC = RTLIB::FPTOUINT_PPCF128_I64;
637 switch (SVT.getSimpleVT()) {
639 LC = RTLIB::FPTOUINT_F32_I128;
642 LC = RTLIB::FPTOUINT_F64_I128;
645 LC = RTLIB::FPTOUINT_F80_I128;
648 LC = RTLIB::FPTOUINT_PPCF128_I128;
657 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
659 SDOperand Op = GetSoftenedFloat(N->getOperand(0));
660 return MakeLibCall(LC, RVT, &Op, 1, false);
663 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
664 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
665 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
666 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
668 // If SoftenSetCCOperands returned a scalar, we need to compare the result
669 // against zero to select between true and false values.
670 if (NewRHS.Val == 0) {
671 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
675 // Update N to have the operands specified.
676 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
677 N->getOperand(2), N->getOperand(3),
678 DAG.getCondCode(CCCode));
681 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
682 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
683 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
684 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
686 // If SoftenSetCCOperands returned a scalar, use it.
687 if (NewRHS.Val == 0) {
688 assert(NewLHS.getValueType() == N->getValueType(0) &&
689 "Unexpected setcc expansion!");
693 // Otherwise, update N to have the operands specified.
694 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
695 DAG.getCondCode(CCCode));
698 SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
699 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
700 assert(OpNo == 1 && "Can only soften the stored value!");
701 StoreSDNode *ST = cast<StoreSDNode>(N);
702 SDOperand Val = ST->getValue();
704 if (ST->isTruncatingStore())
705 // Do an FP_ROUND followed by a non-truncating store.
706 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
707 Val, DAG.getIntPtrConstant(0)));
709 Val = GetSoftenedFloat(Val);
711 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
712 ST->getSrcValue(), ST->getSrcValueOffset(),
713 ST->isVolatile(), ST->getAlignment());
717 //===----------------------------------------------------------------------===//
718 // Float Result Expansion
719 //===----------------------------------------------------------------------===//
721 /// ExpandFloatResult - This method is called when the specified result of the
722 /// specified node is found to need expansion. At this point, the node may also
723 /// have invalid operands or may have other results that need promotion, we just
724 /// know that (at least) one result needs expansion.
725 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
726 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
728 Lo = Hi = SDOperand();
730 // See if the target wants to custom expand this node.
731 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
732 TargetLowering::Custom) {
733 // If the target wants to, allow it to lower this itself.
734 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
735 // Everything that once used N now uses P. We are guaranteed that the
736 // result value types of N and the result value types of P match.
737 ReplaceNodeWith(N, P);
742 switch (N->getOpcode()) {
745 cerr << "ExpandFloatResult #" << ResNo << ": ";
746 N->dump(&DAG); cerr << "\n";
748 assert(0 && "Do not know how to expand the result of this operator!");
751 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
752 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
753 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
754 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
756 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
757 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
758 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
759 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
761 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
762 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
763 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
764 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
765 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
766 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
767 case ISD::SINT_TO_FP:
768 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
771 // If Lo/Hi is null, the sub-method took care of registering results etc.
773 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
776 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
778 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
779 assert(NVT.getSizeInBits() == integerPartWidth &&
780 "Do not know how to expand this float constant!");
781 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
782 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
783 &C.getRawData()[1])), NVT);
784 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
785 &C.getRawData()[0])), NVT);
788 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
790 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
791 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
796 N->getValueType(0), Ops, 2,
798 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
799 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
802 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
804 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
805 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
810 N->getValueType(0), Ops, 2,
812 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
813 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
816 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
818 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
819 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
824 N->getValueType(0), Ops, 2,
826 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
827 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
830 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
832 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
833 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
838 N->getValueType(0), Ops, 2,
840 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
841 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
844 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
846 if (ISD::isNormalLoad(N)) {
847 ExpandRes_NormalLoad(N, Lo, Hi);
851 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
852 LoadSDNode *LD = cast<LoadSDNode>(N);
853 SDOperand Chain = LD->getChain();
854 SDOperand Ptr = LD->getBasePtr();
856 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
857 assert(NVT.isByteSized() && "Expanded type not byte sized!");
858 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
860 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
861 LD->getSrcValue(), LD->getSrcValueOffset(),
863 LD->isVolatile(), LD->getAlignment());
865 // Remember the chain.
866 Chain = Lo.getValue(1);
868 // The high part is undefined.
869 Hi = DAG.getNode(ISD::UNDEF, NVT);
871 // Modified the chain - switch anything that used the old chain to use the
873 ReplaceValueWith(SDOperand(LD, 1), Chain);
876 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
878 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
879 MVT VT = N->getValueType(0);
880 MVT NVT = TLI.getTypeToTransformTo(VT);
881 SDOperand Src = N->getOperand(0);
882 MVT SrcVT = Src.getValueType();
884 // First do an SINT_TO_FP, whether the original was signed or unsigned.
885 if (SrcVT.bitsLE(MVT::i32)) {
886 // The integer can be represented exactly in an f64.
887 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
888 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
889 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
891 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
892 if (SrcVT.bitsLE(MVT::i64)) {
893 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
894 LC = RTLIB::SINTTOFP_I64_PPCF128;
895 } else if (SrcVT.bitsLE(MVT::i128)) {
896 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
897 LC = RTLIB::SINTTOFP_I128_PPCF128;
899 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
901 Hi = MakeLibCall(LC, VT, &Src, 1, true);
902 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
903 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
906 if (N->getOpcode() == ISD::SINT_TO_FP)
909 // Unsigned - fix up the SINT_TO_FP value just calculated.
910 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
911 SrcVT = Src.getValueType();
913 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
914 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
915 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
916 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
917 const uint64_t *Parts = 0;
919 switch (SrcVT.getSimpleVT()) {
921 assert(false && "Unsupported UINT_TO_FP!");
930 Lo = DAG.getNode(ISD::FADD, VT, Hi,
931 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
933 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
934 DAG.getCondCode(ISD::SETLT));
935 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
936 DAG.getConstant(1, TLI.getPointerTy()));
937 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
938 DAG.getConstant(0, TLI.getPointerTy()));
942 //===----------------------------------------------------------------------===//
943 // Float Operand Expansion
944 //===----------------------------------------------------------------------===//
946 /// ExpandFloatOperand - This method is called when the specified operand of the
947 /// specified node is found to need expansion. At this point, all of the result
948 /// types of the node are known to be legal, but other operands of the node may
949 /// need promotion or expansion as well as the specified one.
950 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
951 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
952 SDOperand Res = SDOperand();
954 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
955 == TargetLowering::Custom)
956 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
959 switch (N->getOpcode()) {
962 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
963 N->dump(&DAG); cerr << "\n";
965 assert(0 && "Do not know how to expand this operator's operand!");
968 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
969 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
970 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
972 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
973 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
974 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
976 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
977 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
978 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
981 Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
986 // If the result is null, the sub-method took care of registering results etc.
987 if (!Res.Val) return false;
988 // If the result is N, the sub-method updated N in place. Check to see if any
989 // operands are new, and if so, mark them.
991 // Mark N as new and remark N and its operands. This allows us to correctly
992 // revisit N if it needs another step of expansion and allows us to visit
993 // any new operands to N.
998 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
999 "Invalid operand expansion");
1001 ReplaceValueWith(SDOperand(N, 0), Res);
1005 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1006 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1007 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
1009 ISD::CondCode &CCCode) {
1010 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1011 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1012 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1014 MVT VT = NewLHS.getValueType();
1015 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
1017 // FIXME: This generated code sucks. We want to generate
1018 // FCMP crN, hi1, hi2
1020 // FCMP crN, lo1, lo2
1021 // The following can be improved, but not that much.
1022 SDOperand Tmp1, Tmp2, Tmp3;
1023 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
1024 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
1025 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1026 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
1027 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
1028 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1029 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
1030 NewRHS = SDOperand(); // LHS is the result, not a compare.
1033 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1034 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1035 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1036 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1038 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1039 // against zero to select between true and false values.
1040 if (NewRHS.Val == 0) {
1041 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1042 CCCode = ISD::SETNE;
1045 // Update N to have the operands specified.
1046 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
1047 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1051 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1052 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1053 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1054 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1056 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1057 // against zero to select between true and false values.
1058 if (NewRHS.Val == 0) {
1059 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1060 CCCode = ISD::SETNE;
1063 // Update N to have the operands specified.
1064 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1065 N->getOperand(2), N->getOperand(3),
1066 DAG.getCondCode(CCCode));
1069 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1070 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1071 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1072 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1074 // If ExpandSetCCOperands returned a scalar, use it.
1075 if (NewRHS.Val == 0) {
1076 assert(NewLHS.getValueType() == N->getValueType(0) &&
1077 "Unexpected setcc expansion!");
1081 // Otherwise, update N to have the operands specified.
1082 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1083 DAG.getCondCode(CCCode));
1086 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1087 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1088 "Unsupported FP_TO_UINT!");
1090 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1091 switch (N->getValueType(0).getSimpleVT()) {
1093 assert(false && "Unsupported FP_TO_UINT!");
1095 LC = RTLIB::FPTOUINT_PPCF128_I32;
1098 LC = RTLIB::FPTOUINT_PPCF128_I64;
1101 LC = RTLIB::FPTOUINT_PPCF128_I128;
1105 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
1108 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1109 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1110 "Unsupported FP_TO_SINT!");
1112 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1113 switch (N->getValueType(0).getSimpleVT()) {
1115 assert(false && "Unsupported FP_TO_SINT!");
1117 LC = RTLIB::FPTOSINT_PPCF128_I32;
1119 LC = RTLIB::FPTOSINT_PPCF128_I64;
1122 LC = RTLIB::FPTOSINT_PPCF128_I64;
1126 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
1129 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1130 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1131 "Logic only correct for ppcf128!");
1133 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1134 // Round it the rest of the way (e.g. to f32) if needed.
1135 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
1138 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1139 if (ISD::isNormalStore(N))
1140 return ExpandOp_NormalStore(N, OpNo);
1142 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1143 assert(OpNo == 1 && "Can only expand the stored value so far");
1144 StoreSDNode *ST = cast<StoreSDNode>(N);
1146 SDOperand Chain = ST->getChain();
1147 SDOperand Ptr = ST->getBasePtr();
1149 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
1150 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1151 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1154 GetExpandedOp(ST->getValue(), Lo, Hi);
1156 return DAG.getTruncStore(Chain, Lo, Ptr,
1157 ST->getSrcValue(), ST->getSrcValueOffset(),
1159 ST->isVolatile(), ST->getAlignment());