1 //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements float type expansion and softening for LegalizeTypes.
11 // Softening is the act of turning a computation in an illegal floating point
12 // type into a computation in an integer type of the same size; also known as
13 // "soft float". For example, turning f32 arithmetic into operations using i32.
14 // The resulting integer value is the same as what you would get by performing
15 // the floating point operation and bitcasting the result to the integer type.
16 // Expansion is the act of changing a computation in an illegal type to be a
17 // computation in two identical registers of a smaller type. For example,
18 // implementing ppcf128 arithmetic in two f64 registers.
20 //===----------------------------------------------------------------------===//
22 #include "LegalizeTypes.h"
25 /// GetFPLibCall - Return the right libcall for the given floating point type.
26 static RTLIB::Libcall GetFPLibCall(MVT VT,
27 RTLIB::Libcall Call_F32,
28 RTLIB::Libcall Call_F64,
29 RTLIB::Libcall Call_F80,
30 RTLIB::Libcall Call_PPCF128) {
32 VT == MVT::f32 ? Call_F32 :
33 VT == MVT::f64 ? Call_F64 :
34 VT == MVT::f80 ? Call_F80 :
35 VT == MVT::ppcf128 ? Call_PPCF128 :
36 RTLIB::UNKNOWN_LIBCALL;
39 //===----------------------------------------------------------------------===//
40 // Result Float to Integer Conversion.
41 //===----------------------------------------------------------------------===//
43 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
44 DEBUG(cerr << "Soften float result " << ResNo << ": "; N->dump(&DAG);
46 SDValue R = SDValue();
48 switch (N->getOpcode()) {
51 cerr << "SoftenFloatResult #" << ResNo << ": ";
52 N->dump(&DAG); cerr << "\n";
54 assert(0 && "Do not know how to soften the result of this operator!");
57 case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
58 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
60 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
62 case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
63 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
64 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
65 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
66 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
67 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
68 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
69 case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
70 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
71 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
72 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
73 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
74 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
75 case ISD::SINT_TO_FP: R = SoftenFloatRes_SINT_TO_FP(N); break;
76 case ISD::UINT_TO_FP: R = SoftenFloatRes_UINT_TO_FP(N); break;
79 // If R is null, the sub-method took care of registering the result.
81 SetSoftenedFloat(SDValue(N, ResNo), R);
84 SDValue DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
85 return BitConvertToInteger(N->getOperand(0));
88 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
89 // Convert the inputs to integers, and build a new pair out of them.
90 return DAG.getNode(ISD::BUILD_PAIR,
91 TLI.getTypeToTransformTo(N->getValueType(0)),
92 BitConvertToInteger(N->getOperand(0)),
93 BitConvertToInteger(N->getOperand(1)));
96 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
97 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
98 TLI.getTypeToTransformTo(N->getValueType(0)));
101 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
102 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
103 unsigned Size = NVT.getSizeInBits();
105 // Mask = ~(1 << (Size-1))
106 SDValue Mask = DAG.getConstant(APInt::getAllOnesValue(Size).clear(Size-1),
108 SDValue Op = GetSoftenedFloat(N->getOperand(0));
109 return DAG.getNode(ISD::AND, NVT, Op, Mask);
112 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
113 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
114 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
115 GetSoftenedFloat(N->getOperand(1)) };
116 return MakeLibCall(GetFPLibCall(N->getValueType(0),
124 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
125 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
126 SDValue RHS = BitConvertToInteger(N->getOperand(1));
128 MVT LVT = LHS.getValueType();
129 MVT RVT = RHS.getValueType();
131 unsigned LSize = LVT.getSizeInBits();
132 unsigned RSize = RVT.getSizeInBits();
134 // First get the sign bit of second operand.
135 SDValue SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
136 DAG.getConstant(RSize - 1,
137 TLI.getShiftAmountTy()));
138 SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
140 // Shift right or sign-extend it if the two operands have different types.
141 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
143 SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
144 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
145 SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
146 } else if (SizeDiff < 0) {
147 SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
148 SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
149 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
152 // Clear the sign bit of the first operand.
153 SDValue Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
154 DAG.getConstant(LSize - 1,
155 TLI.getShiftAmountTy()));
156 Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
157 LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
159 // Or the value with the sign bit.
160 return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
163 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
164 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
165 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
166 GetSoftenedFloat(N->getOperand(1)) };
167 return MakeLibCall(GetFPLibCall(N->getValueType(0),
175 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
176 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
177 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
178 GetSoftenedFloat(N->getOperand(1)) };
179 return MakeLibCall(GetFPLibCall(N->getValueType(0),
187 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
188 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
189 SDValue Op = N->getOperand(0);
190 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
191 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
192 return MakeLibCall(LC, NVT, &Op, 1, false);
195 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
196 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
197 SDValue Op = N->getOperand(0);
198 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
199 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
200 return MakeLibCall(LC, NVT, &Op, 1, false);
203 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
204 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
205 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
206 GetSoftenedFloat(N->getOperand(1)) };
207 return MakeLibCall(GetFPLibCall(N->getValueType(0),
215 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
216 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
217 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
218 return MakeLibCall(GetFPLibCall(N->getValueType(0),
222 RTLIB::POWI_PPCF128),
226 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
227 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
228 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
229 GetSoftenedFloat(N->getOperand(1)) };
230 return MakeLibCall(GetFPLibCall(N->getValueType(0),
238 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
239 LoadSDNode *L = cast<LoadSDNode>(N);
240 MVT VT = N->getValueType(0);
241 MVT NVT = TLI.getTypeToTransformTo(VT);
244 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
245 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
246 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
247 L->getSrcValue(), L->getSrcValueOffset(), NVT,
248 L->isVolatile(), L->getAlignment());
249 // Legalized the chain result - switch anything that used the old chain to
251 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
255 // Do a non-extending load followed by FP_EXTEND.
256 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
257 L->getMemoryVT(), L->getChain(),
258 L->getBasePtr(), L->getOffset(),
259 L->getSrcValue(), L->getSrcValueOffset(),
261 L->isVolatile(), L->getAlignment());
262 // Legalized the chain result - switch anything that used the old chain to
264 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
265 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NewL));
268 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
269 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
270 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
271 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
274 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
275 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
276 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
277 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
278 N->getOperand(1), LHS, RHS, N->getOperand(4));
281 SDValue DAGTypeLegalizer::SoftenFloatRes_SINT_TO_FP(SDNode *N) {
282 SDValue Op = N->getOperand(0);
283 MVT RVT = N->getValueType(0);
284 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), RVT);
285 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SINT_TO_FP!");
286 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
289 SDValue DAGTypeLegalizer::SoftenFloatRes_UINT_TO_FP(SDNode *N) {
290 SDValue Op = N->getOperand(0);
291 MVT RVT = N->getValueType(0);
292 RTLIB::Libcall LC = RTLIB::getUINTTOFP(Op.getValueType(), RVT);
293 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UINT_TO_FP!");
294 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false);
298 //===----------------------------------------------------------------------===//
299 // Operand Float to Integer Conversion..
300 //===----------------------------------------------------------------------===//
302 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
303 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
305 SDValue Res = SDValue();
307 switch (N->getOpcode()) {
310 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
311 N->dump(&DAG); cerr << "\n";
313 assert(0 && "Do not know how to soften this operator's operand!");
316 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
317 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
318 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
319 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
320 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
321 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
322 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
323 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
326 // If the result is null, the sub-method took care of registering results etc.
327 if (!Res.getNode()) return false;
329 // If the result is N, the sub-method updated N in place. Check to see if any
330 // operands are new, and if so, mark them.
331 if (Res.getNode() == N) {
332 // Mark N as new and remark N and its operands. This allows us to correctly
333 // revisit N if it needs another step of promotion and allows us to visit
334 // any new operands to N.
339 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
340 "Invalid operand expansion");
342 ReplaceValueWith(SDValue(N, 0), Res);
346 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
347 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
348 void DAGTypeLegalizer::SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
349 ISD::CondCode &CCCode) {
350 SDValue LHSInt = GetSoftenedFloat(NewLHS);
351 SDValue RHSInt = GetSoftenedFloat(NewRHS);
352 MVT VT = NewLHS.getValueType();
354 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
356 // Expand into one or more soft-fp libcall(s).
357 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
361 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
365 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
369 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
373 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
377 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
381 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
384 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
387 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
390 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
393 // SETONE = SETOLT | SETOGT
394 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
397 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
400 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
403 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
406 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
409 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
411 default: assert(false && "Do not know how to soften this setcc!");
415 MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
416 SDValue Ops[2] = { LHSInt, RHSInt };
417 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/);
418 NewRHS = DAG.getConstant(0, RetVT);
419 CCCode = TLI.getCmpLibcallCC(LC1);
420 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
421 SDValue Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
422 NewLHS, NewRHS, DAG.getCondCode(CCCode));
423 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/);
424 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
425 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
426 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
431 SDValue DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
432 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
433 GetSoftenedFloat(N->getOperand(0)));
436 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
437 MVT SVT = N->getOperand(0).getValueType();
438 MVT RVT = N->getValueType(0);
440 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
441 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
443 SDValue Op = GetSoftenedFloat(N->getOperand(0));
444 return MakeLibCall(LC, RVT, &Op, 1, false);
447 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
448 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
449 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
450 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
452 // If SoftenSetCCOperands returned a scalar, we need to compare the result
453 // against zero to select between true and false values.
454 if (NewRHS.getNode() == 0) {
455 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
459 // Update N to have the operands specified.
460 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
461 DAG.getCondCode(CCCode), NewLHS, NewRHS,
465 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
466 MVT RVT = N->getValueType(0);
467 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
468 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
469 SDValue Op = GetSoftenedFloat(N->getOperand(0));
470 return MakeLibCall(LC, RVT, &Op, 1, false);
473 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
474 MVT RVT = N->getValueType(0);
475 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
476 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
477 SDValue Op = GetSoftenedFloat(N->getOperand(0));
478 return MakeLibCall(LC, RVT, &Op, 1, false);
481 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
482 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
483 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
484 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
486 // If SoftenSetCCOperands returned a scalar, we need to compare the result
487 // against zero to select between true and false values.
488 if (NewRHS.getNode() == 0) {
489 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
493 // Update N to have the operands specified.
494 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
495 N->getOperand(2), N->getOperand(3),
496 DAG.getCondCode(CCCode));
499 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
500 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
501 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
502 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
504 // If SoftenSetCCOperands returned a scalar, use it.
505 if (NewRHS.getNode() == 0) {
506 assert(NewLHS.getValueType() == N->getValueType(0) &&
507 "Unexpected setcc expansion!");
511 // Otherwise, update N to have the operands specified.
512 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
513 DAG.getCondCode(CCCode));
516 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
517 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
518 assert(OpNo == 1 && "Can only soften the stored value!");
519 StoreSDNode *ST = cast<StoreSDNode>(N);
520 SDValue Val = ST->getValue();
522 if (ST->isTruncatingStore())
523 // Do an FP_ROUND followed by a non-truncating store.
524 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
525 Val, DAG.getIntPtrConstant(0)));
527 Val = GetSoftenedFloat(Val);
529 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
530 ST->getSrcValue(), ST->getSrcValueOffset(),
531 ST->isVolatile(), ST->getAlignment());
535 //===----------------------------------------------------------------------===//
536 // Float Result Expansion
537 //===----------------------------------------------------------------------===//
539 /// ExpandFloatResult - This method is called when the specified result of the
540 /// specified node is found to need expansion. At this point, the node may also
541 /// have invalid operands or may have other results that need promotion, we just
542 /// know that (at least) one result needs expansion.
543 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
544 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
548 // See if the target wants to custom expand this node.
549 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
550 TargetLowering::Custom) {
551 // If the target wants to, allow it to lower this itself.
552 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
553 // Everything that once used N now uses P. We are guaranteed that the
554 // result value types of N and the result value types of P match.
555 ReplaceNodeWith(N, P);
560 switch (N->getOpcode()) {
563 cerr << "ExpandFloatResult #" << ResNo << ": ";
564 N->dump(&DAG); cerr << "\n";
566 assert(0 && "Do not know how to expand the result of this operator!");
569 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
570 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
571 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
572 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
574 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
575 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
576 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
577 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
579 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
580 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
581 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
582 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
583 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
584 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
585 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
586 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
587 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
588 case ISD::SINT_TO_FP:
589 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
592 // If Lo/Hi is null, the sub-method took care of registering results etc.
594 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
597 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
599 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
600 assert(NVT.getSizeInBits() == integerPartWidth &&
601 "Do not know how to expand this float constant!");
602 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
603 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
604 &C.getRawData()[1])), NVT);
605 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
606 &C.getRawData()[0])), NVT);
609 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
611 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
612 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
617 N->getValueType(0), Ops, 2,
619 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
620 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
623 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
625 assert(N->getValueType(0) == MVT::ppcf128 &&
626 "Logic only correct for ppcf128!");
628 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
629 Hi = DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp);
630 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
631 Lo = DAG.getNode(ISD::SELECT_CC, Lo.getValueType(), Tmp, Hi, Lo,
632 DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo),
633 DAG.getCondCode(ISD::SETEQ));
636 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
638 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
639 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
644 N->getValueType(0), Ops, 2,
646 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
647 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
650 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
652 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
653 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
658 N->getValueType(0), Ops, 2,
660 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
661 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
664 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
666 GetExpandedFloat(N->getOperand(0), Lo, Hi);
667 Lo = DAG.getNode(ISD::FNEG, Lo.getValueType(), Lo);
668 Hi = DAG.getNode(ISD::FNEG, Hi.getValueType(), Hi);
671 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
673 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
674 Hi = DAG.getNode(ISD::FP_EXTEND, NVT, N->getOperand(0));
675 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
678 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
680 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
681 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
686 N->getValueType(0), Ops, 2,
688 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
689 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
692 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
694 if (ISD::isNormalLoad(N)) {
695 ExpandRes_NormalLoad(N, Lo, Hi);
699 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
700 LoadSDNode *LD = cast<LoadSDNode>(N);
701 SDValue Chain = LD->getChain();
702 SDValue Ptr = LD->getBasePtr();
704 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
705 assert(NVT.isByteSized() && "Expanded type not byte sized!");
706 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
708 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
709 LD->getSrcValue(), LD->getSrcValueOffset(),
711 LD->isVolatile(), LD->getAlignment());
713 // Remember the chain.
714 Chain = Lo.getValue(1);
716 // The high part is undefined.
717 Hi = DAG.getNode(ISD::UNDEF, NVT);
719 // Modified the chain - switch anything that used the old chain to use the
721 ReplaceValueWith(SDValue(LD, 1), Chain);
724 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
726 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
727 MVT VT = N->getValueType(0);
728 MVT NVT = TLI.getTypeToTransformTo(VT);
729 SDValue Src = N->getOperand(0);
730 MVT SrcVT = Src.getValueType();
732 // First do an SINT_TO_FP, whether the original was signed or unsigned.
733 if (SrcVT.bitsLE(MVT::i32)) {
734 // The integer can be represented exactly in an f64.
735 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
736 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
737 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
739 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
740 if (SrcVT.bitsLE(MVT::i64)) {
741 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
742 LC = RTLIB::SINTTOFP_I64_PPCF128;
743 } else if (SrcVT.bitsLE(MVT::i128)) {
744 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
745 LC = RTLIB::SINTTOFP_I128_PPCF128;
747 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
749 Hi = MakeLibCall(LC, VT, &Src, 1, true);
750 assert(Hi.getNode()->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
751 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
754 if (N->getOpcode() == ISD::SINT_TO_FP)
757 // Unsigned - fix up the SINT_TO_FP value just calculated.
758 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
759 SrcVT = Src.getValueType();
761 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
762 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
763 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
764 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
765 const uint64_t *Parts = 0;
767 switch (SrcVT.getSimpleVT()) {
769 assert(false && "Unsupported UINT_TO_FP!");
778 Lo = DAG.getNode(ISD::FADD, VT, Hi,
779 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
781 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
782 DAG.getCondCode(ISD::SETLT));
783 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo, DAG.getIntPtrConstant(1));
784 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo, DAG.getIntPtrConstant(0));
788 //===----------------------------------------------------------------------===//
789 // Float Operand Expansion
790 //===----------------------------------------------------------------------===//
792 /// ExpandFloatOperand - This method is called when the specified operand of the
793 /// specified node is found to need expansion. At this point, all of the result
794 /// types of the node are known to be legal, but other operands of the node may
795 /// need promotion or expansion as well as the specified one.
796 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
797 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
798 SDValue Res = SDValue();
800 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
801 == TargetLowering::Custom)
802 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
804 if (Res.getNode() == 0) {
805 switch (N->getOpcode()) {
808 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
809 N->dump(&DAG); cerr << "\n";
811 assert(0 && "Do not know how to expand this operator's operand!");
814 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
815 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
816 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
818 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
819 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
820 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
821 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
822 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
823 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
824 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
829 // If the result is null, the sub-method took care of registering results etc.
830 if (!Res.getNode()) return false;
831 // If the result is N, the sub-method updated N in place. Check to see if any
832 // operands are new, and if so, mark them.
833 if (Res.getNode() == N) {
834 // Mark N as new and remark N and its operands. This allows us to correctly
835 // revisit N if it needs another step of expansion and allows us to visit
836 // any new operands to N.
841 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
842 "Invalid operand expansion");
844 ReplaceValueWith(SDValue(N, 0), Res);
848 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
849 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
850 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
852 ISD::CondCode &CCCode) {
853 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
854 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
855 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
857 MVT VT = NewLHS.getValueType();
858 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
860 // FIXME: This generated code sucks. We want to generate
861 // FCMP crN, hi1, hi2
863 // FCMP crN, lo1, lo2
864 // The following can be improved, but not that much.
865 SDValue Tmp1, Tmp2, Tmp3;
866 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
867 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
868 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
869 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
870 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
871 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
872 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
873 NewRHS = SDValue(); // LHS is the result, not a compare.
876 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
877 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
878 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
879 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
881 // If ExpandSetCCOperands returned a scalar, we need to compare the result
882 // against zero to select between true and false values.
883 if (NewRHS.getNode() == 0) {
884 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
888 // Update N to have the operands specified.
889 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
890 DAG.getCondCode(CCCode), NewLHS, NewRHS,
894 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
895 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
896 "Logic only correct for ppcf128!");
898 GetExpandedFloat(N->getOperand(0), Lo, Hi);
899 // Round it the rest of the way (e.g. to f32) if needed.
900 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
903 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
904 MVT RVT = N->getValueType(0);
905 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
906 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
907 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false);
910 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
911 MVT RVT = N->getValueType(0);
912 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
913 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
914 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
917 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
918 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
919 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
920 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
922 // If ExpandSetCCOperands returned a scalar, we need to compare the result
923 // against zero to select between true and false values.
924 if (NewRHS.getNode() == 0) {
925 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
929 // Update N to have the operands specified.
930 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
931 N->getOperand(2), N->getOperand(3),
932 DAG.getCondCode(CCCode));
935 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
936 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
937 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
938 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
940 // If ExpandSetCCOperands returned a scalar, use it.
941 if (NewRHS.getNode() == 0) {
942 assert(NewLHS.getValueType() == N->getValueType(0) &&
943 "Unexpected setcc expansion!");
947 // Otherwise, update N to have the operands specified.
948 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
949 DAG.getCondCode(CCCode));
952 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
953 if (ISD::isNormalStore(N))
954 return ExpandOp_NormalStore(N, OpNo);
956 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
957 assert(OpNo == 1 && "Can only expand the stored value so far");
958 StoreSDNode *ST = cast<StoreSDNode>(N);
960 SDValue Chain = ST->getChain();
961 SDValue Ptr = ST->getBasePtr();
963 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
964 assert(NVT.isByteSized() && "Expanded type not byte sized!");
965 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
968 GetExpandedOp(ST->getValue(), Lo, Hi);
970 return DAG.getTruncStore(Chain, Lo, Ptr,
971 ST->getSrcValue(), ST->getSrcValueOffset(),
973 ST->isVolatile(), ST->getAlignment());