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::FDIV: R = SoftenFloatRes_FDIV(N); break;
65 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
66 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
67 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
68 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
69 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
70 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
71 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
72 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
73 case ISD::SINT_TO_FP: R = SoftenFloatRes_SINT_TO_FP(N); break;
74 case ISD::UINT_TO_FP: R = SoftenFloatRes_UINT_TO_FP(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_FDIV(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_FMUL(SDNode *N) {
163 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
164 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
165 GetSoftenedFloat(N->getOperand(1)) };
166 return MakeLibCall(GetFPLibCall(N->getValueType(0),
174 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
175 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
176 SDOperand Op = N->getOperand(0);
177 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
178 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
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);
185 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
186 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
187 return MakeLibCall(LC, NVT, &Op, 1, false);
190 SDOperand DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
191 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
192 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
193 return MakeLibCall(GetFPLibCall(N->getValueType(0),
197 RTLIB::POWI_PPCF128),
201 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
202 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
203 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
204 GetSoftenedFloat(N->getOperand(1)) };
205 return MakeLibCall(GetFPLibCall(N->getValueType(0),
213 SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
214 LoadSDNode *L = cast<LoadSDNode>(N);
215 MVT VT = N->getValueType(0);
216 MVT NVT = TLI.getTypeToTransformTo(VT);
219 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
220 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
221 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
222 L->getSrcValue(), L->getSrcValueOffset(), NVT,
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));
230 // Do a non-extending load followed by FP_EXTEND.
231 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
232 L->getMemoryVT(), L->getChain(),
233 L->getBasePtr(), L->getOffset(),
234 L->getSrcValue(), L->getSrcValueOffset(),
236 L->isVolatile(), L->getAlignment());
237 // Legalized the chain result - switch anything that used the old chain to
239 ReplaceValueWith(SDOperand(N, 1), NewL.getValue(1));
240 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NewL));
243 SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
244 SDOperand LHS = GetSoftenedFloat(N->getOperand(1));
245 SDOperand RHS = GetSoftenedFloat(N->getOperand(2));
246 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
249 SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
250 SDOperand LHS = GetSoftenedFloat(N->getOperand(2));
251 SDOperand RHS = GetSoftenedFloat(N->getOperand(3));
252 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
253 N->getOperand(1), LHS, RHS, N->getOperand(4));
256 SDOperand DAGTypeLegalizer::SoftenFloatRes_SINT_TO_FP(SDNode *N) {
257 SDOperand Op = N->getOperand(0);
258 MVT RVT = N->getValueType(0);
259 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), RVT);
260 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SINT_TO_FP!");
261 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
264 SDOperand DAGTypeLegalizer::SoftenFloatRes_UINT_TO_FP(SDNode *N) {
265 SDOperand Op = N->getOperand(0);
266 MVT RVT = N->getValueType(0);
267 RTLIB::Libcall LC = RTLIB::getUINTTOFP(Op.getValueType(), RVT);
268 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UINT_TO_FP!");
269 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
273 //===----------------------------------------------------------------------===//
274 // Operand Float to Integer Conversion..
275 //===----------------------------------------------------------------------===//
277 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
278 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
280 SDOperand Res = SDOperand();
282 switch (N->getOpcode()) {
285 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
286 N->dump(&DAG); cerr << "\n";
288 assert(0 && "Do not know how to soften this operator's operand!");
291 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
292 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
293 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
294 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
295 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
296 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
297 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
300 // If the result is null, the sub-method took care of registering results etc.
301 if (!Res.Val) return false;
303 // If the result is N, the sub-method updated N in place. Check to see if any
304 // operands are new, and if so, mark them.
306 // Mark N as new and remark N and its operands. This allows us to correctly
307 // revisit N if it needs another step of promotion and allows us to visit
308 // any new operands to N.
313 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
314 "Invalid operand expansion");
316 ReplaceValueWith(SDOperand(N, 0), Res);
320 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
321 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
322 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
323 ISD::CondCode &CCCode) {
324 SDOperand LHSInt = GetSoftenedFloat(NewLHS);
325 SDOperand RHSInt = GetSoftenedFloat(NewRHS);
326 MVT VT = NewLHS.getValueType();
328 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
330 // Expand into one or more soft-fp libcall(s).
331 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
335 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
339 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
343 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
347 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
351 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
355 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
358 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
361 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
364 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
367 // SETONE = SETOLT | SETOGT
368 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
371 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
374 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
377 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
380 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
383 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
385 default: assert(false && "Do not know how to soften this setcc!");
389 MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
390 SDOperand Ops[2] = { LHSInt, RHSInt };
391 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/);
392 NewRHS = DAG.getConstant(0, RetVT);
393 CCCode = TLI.getCmpLibcallCC(LC1);
394 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
395 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
396 NewLHS, NewRHS, DAG.getCondCode(CCCode));
397 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/);
398 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
399 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
400 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
401 NewRHS = SDOperand();
405 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
406 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
407 GetSoftenedFloat(N->getOperand(0)));
410 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
411 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
412 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
413 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
415 // If SoftenSetCCOperands returned a scalar, we need to compare the result
416 // against zero to select between true and false values.
417 if (NewRHS.Val == 0) {
418 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
422 // Update N to have the operands specified.
423 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
424 DAG.getCondCode(CCCode), NewLHS, NewRHS,
428 SDOperand DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
429 MVT RVT = N->getValueType(0);
430 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
431 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
432 SDOperand Op = GetSoftenedFloat(N->getOperand(0));
433 return MakeLibCall(LC, RVT, &Op, 1, false);
436 SDOperand DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
437 MVT RVT = N->getValueType(0);
438 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
439 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
440 SDOperand Op = GetSoftenedFloat(N->getOperand(0));
441 return MakeLibCall(LC, RVT, &Op, 1, false);
444 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
445 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
446 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
447 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
449 // If SoftenSetCCOperands returned a scalar, we need to compare the result
450 // against zero to select between true and false values.
451 if (NewRHS.Val == 0) {
452 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
456 // Update N to have the operands specified.
457 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
458 N->getOperand(2), N->getOperand(3),
459 DAG.getCondCode(CCCode));
462 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
463 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
464 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
465 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
467 // If SoftenSetCCOperands returned a scalar, use it.
468 if (NewRHS.Val == 0) {
469 assert(NewLHS.getValueType() == N->getValueType(0) &&
470 "Unexpected setcc expansion!");
474 // Otherwise, update N to have the operands specified.
475 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
476 DAG.getCondCode(CCCode));
479 SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
480 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
481 assert(OpNo == 1 && "Can only soften the stored value!");
482 StoreSDNode *ST = cast<StoreSDNode>(N);
483 SDOperand Val = ST->getValue();
485 if (ST->isTruncatingStore())
486 // Do an FP_ROUND followed by a non-truncating store.
487 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
488 Val, DAG.getIntPtrConstant(0)));
490 Val = GetSoftenedFloat(Val);
492 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
493 ST->getSrcValue(), ST->getSrcValueOffset(),
494 ST->isVolatile(), ST->getAlignment());
498 //===----------------------------------------------------------------------===//
499 // Float Result Expansion
500 //===----------------------------------------------------------------------===//
502 /// ExpandFloatResult - This method is called when the specified result of the
503 /// specified node is found to need expansion. At this point, the node may also
504 /// have invalid operands or may have other results that need promotion, we just
505 /// know that (at least) one result needs expansion.
506 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
507 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
509 Lo = Hi = SDOperand();
511 // See if the target wants to custom expand this node.
512 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
513 TargetLowering::Custom) {
514 // If the target wants to, allow it to lower this itself.
515 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
516 // Everything that once used N now uses P. We are guaranteed that the
517 // result value types of N and the result value types of P match.
518 ReplaceNodeWith(N, P);
523 switch (N->getOpcode()) {
526 cerr << "ExpandFloatResult #" << ResNo << ": ";
527 N->dump(&DAG); cerr << "\n";
529 assert(0 && "Do not know how to expand the result of this operator!");
532 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
533 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
534 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
535 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
537 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
538 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
539 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
540 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
542 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
543 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
544 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
545 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
546 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
547 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
548 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
549 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
550 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
551 case ISD::SINT_TO_FP:
552 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
555 // If Lo/Hi is null, the sub-method took care of registering results etc.
557 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
560 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
562 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
563 assert(NVT.getSizeInBits() == integerPartWidth &&
564 "Do not know how to expand this float constant!");
565 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
566 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
567 &C.getRawData()[1])), NVT);
568 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
569 &C.getRawData()[0])), NVT);
572 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
574 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
575 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
580 N->getValueType(0), Ops, 2,
582 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
583 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
586 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDOperand &Lo,
588 assert(N->getValueType(0) == MVT::ppcf128 &&
589 "Logic only correct for ppcf128!");
591 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
592 Hi = DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp);
593 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
594 Lo = DAG.getNode(ISD::SELECT_CC, Lo.getValueType(), Tmp, Hi, Lo,
595 DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo),
596 DAG.getCondCode(ISD::SETEQ));
599 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
601 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
602 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
607 N->getValueType(0), Ops, 2,
609 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
610 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
613 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
615 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
616 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
621 N->getValueType(0), Ops, 2,
623 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
624 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
627 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDOperand &Lo,
629 GetExpandedFloat(N->getOperand(0), Lo, Hi);
630 Lo = DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo);
631 Hi = DAG.getNode(ISD::FNEG, Hi.getValueType(), Hi);
634 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDOperand &Lo,
636 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
637 Hi = DAG.getNode(ISD::FP_EXTEND, NVT, N->getOperand(0));
638 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
641 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
643 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
644 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
649 N->getValueType(0), Ops, 2,
651 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
652 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
655 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
657 if (ISD::isNormalLoad(N)) {
658 ExpandRes_NormalLoad(N, Lo, Hi);
662 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
663 LoadSDNode *LD = cast<LoadSDNode>(N);
664 SDOperand Chain = LD->getChain();
665 SDOperand Ptr = LD->getBasePtr();
667 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
668 assert(NVT.isByteSized() && "Expanded type not byte sized!");
669 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
671 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
672 LD->getSrcValue(), LD->getSrcValueOffset(),
674 LD->isVolatile(), LD->getAlignment());
676 // Remember the chain.
677 Chain = Lo.getValue(1);
679 // The high part is undefined.
680 Hi = DAG.getNode(ISD::UNDEF, NVT);
682 // Modified the chain - switch anything that used the old chain to use the
684 ReplaceValueWith(SDOperand(LD, 1), Chain);
687 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
689 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
690 MVT VT = N->getValueType(0);
691 MVT NVT = TLI.getTypeToTransformTo(VT);
692 SDOperand Src = N->getOperand(0);
693 MVT SrcVT = Src.getValueType();
695 // First do an SINT_TO_FP, whether the original was signed or unsigned.
696 if (SrcVT.bitsLE(MVT::i32)) {
697 // The integer can be represented exactly in an f64.
698 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
699 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
700 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
702 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
703 if (SrcVT.bitsLE(MVT::i64)) {
704 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
705 LC = RTLIB::SINTTOFP_I64_PPCF128;
706 } else if (SrcVT.bitsLE(MVT::i128)) {
707 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
708 LC = RTLIB::SINTTOFP_I128_PPCF128;
710 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
712 Hi = MakeLibCall(LC, VT, &Src, 1, true);
713 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
714 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
717 if (N->getOpcode() == ISD::SINT_TO_FP)
720 // Unsigned - fix up the SINT_TO_FP value just calculated.
721 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
722 SrcVT = Src.getValueType();
724 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
725 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
726 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
727 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
728 const uint64_t *Parts = 0;
730 switch (SrcVT.getSimpleVT()) {
732 assert(false && "Unsupported UINT_TO_FP!");
741 Lo = DAG.getNode(ISD::FADD, VT, Hi,
742 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
744 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
745 DAG.getCondCode(ISD::SETLT));
746 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
747 DAG.getConstant(1, TLI.getPointerTy()));
748 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
749 DAG.getConstant(0, TLI.getPointerTy()));
753 //===----------------------------------------------------------------------===//
754 // Float Operand Expansion
755 //===----------------------------------------------------------------------===//
757 /// ExpandFloatOperand - This method is called when the specified operand of the
758 /// specified node is found to need expansion. At this point, all of the result
759 /// types of the node are known to be legal, but other operands of the node may
760 /// need promotion or expansion as well as the specified one.
761 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
762 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
763 SDOperand Res = SDOperand();
765 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
766 == TargetLowering::Custom)
767 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
770 switch (N->getOpcode()) {
773 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
774 N->dump(&DAG); cerr << "\n";
776 assert(0 && "Do not know how to expand this operator's operand!");
779 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
780 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
781 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
783 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
784 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
785 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
786 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
787 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
788 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
789 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
794 // If the result is null, the sub-method took care of registering results etc.
795 if (!Res.Val) return false;
796 // If the result is N, the sub-method updated N in place. Check to see if any
797 // operands are new, and if so, mark them.
799 // Mark N as new and remark N and its operands. This allows us to correctly
800 // revisit N if it needs another step of expansion and allows us to visit
801 // any new operands to N.
806 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
807 "Invalid operand expansion");
809 ReplaceValueWith(SDOperand(N, 0), Res);
813 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
814 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
815 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
817 ISD::CondCode &CCCode) {
818 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
819 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
820 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
822 MVT VT = NewLHS.getValueType();
823 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
825 // FIXME: This generated code sucks. We want to generate
826 // FCMP crN, hi1, hi2
828 // FCMP crN, lo1, lo2
829 // The following can be improved, but not that much.
830 SDOperand Tmp1, Tmp2, Tmp3;
831 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
832 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
833 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
834 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
835 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
836 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
837 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
838 NewRHS = SDOperand(); // LHS is the result, not a compare.
841 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
842 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
843 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
844 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
846 // If ExpandSetCCOperands returned a scalar, we need to compare the result
847 // against zero to select between true and false values.
848 if (NewRHS.Val == 0) {
849 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
853 // Update N to have the operands specified.
854 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
855 DAG.getCondCode(CCCode), NewLHS, NewRHS,
859 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
860 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
861 "Logic only correct for ppcf128!");
863 GetExpandedFloat(N->getOperand(0), Lo, Hi);
864 // Round it the rest of the way (e.g. to f32) if needed.
865 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
868 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
869 MVT RVT = N->getValueType(0);
870 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
871 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
872 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false);
875 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
876 MVT RVT = N->getValueType(0);
877 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
878 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
879 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
882 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
883 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
884 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
885 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
887 // If ExpandSetCCOperands returned a scalar, we need to compare the result
888 // against zero to select between true and false values.
889 if (NewRHS.Val == 0) {
890 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
894 // Update N to have the operands specified.
895 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
896 N->getOperand(2), N->getOperand(3),
897 DAG.getCondCode(CCCode));
900 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
901 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
902 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
903 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
905 // If ExpandSetCCOperands returned a scalar, use it.
906 if (NewRHS.Val == 0) {
907 assert(NewLHS.getValueType() == N->getValueType(0) &&
908 "Unexpected setcc expansion!");
912 // Otherwise, update N to have the operands specified.
913 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
914 DAG.getCondCode(CCCode));
917 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
918 if (ISD::isNormalStore(N))
919 return ExpandOp_NormalStore(N, OpNo);
921 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
922 assert(OpNo == 1 && "Can only expand the stored value so far");
923 StoreSDNode *ST = cast<StoreSDNode>(N);
925 SDOperand Chain = ST->getChain();
926 SDOperand Ptr = ST->getBasePtr();
928 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
929 assert(NVT.isByteSized() && "Expanded type not byte sized!");
930 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
933 GetExpandedOp(ST->getValue(), Lo, Hi);
935 return DAG.getTruncStore(Chain, Lo, Ptr,
936 ST->getSrcValue(), ST->getSrcValueOffset(),
938 ST->isVolatile(), ST->getAlignment());