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"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
28 /// GetFPLibCall - Return the right libcall for the given floating point type.
29 static RTLIB::Libcall GetFPLibCall(MVT VT,
30 RTLIB::Libcall Call_F32,
31 RTLIB::Libcall Call_F64,
32 RTLIB::Libcall Call_F80,
33 RTLIB::Libcall Call_PPCF128) {
35 VT == MVT::f32 ? Call_F32 :
36 VT == MVT::f64 ? Call_F64 :
37 VT == MVT::f80 ? Call_F80 :
38 VT == MVT::ppcf128 ? Call_PPCF128 :
39 RTLIB::UNKNOWN_LIBCALL;
42 //===----------------------------------------------------------------------===//
43 // Result Float to Integer Conversion.
44 //===----------------------------------------------------------------------===//
46 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
47 DEBUG(cerr << "Soften float result " << ResNo << ": "; N->dump(&DAG);
49 SDOperand R = SDOperand();
51 // See if the target wants to custom expand this node.
52 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
53 TargetLowering::Custom) {
54 // If the target wants to, allow it to lower this itself.
55 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
56 // Everything that once used N now uses P. We are guaranteed that the
57 // result value types of N and the result value types of P match.
58 ReplaceNodeWith(N, P);
63 switch (N->getOpcode()) {
66 cerr << "SoftenFloatResult #" << ResNo << ": ";
67 N->dump(&DAG); cerr << "\n";
69 assert(0 && "Do not know how to convert the result of this operator!");
72 case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
73 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
75 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
77 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
78 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
80 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
82 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
83 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
84 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
87 // If R is null, the sub-method took care of registering the result.
89 SetSoftenedFloat(SDOperand(N, ResNo), R);
92 SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
93 return BitConvertToInteger(N->getOperand(0));
96 SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
97 // Convert the inputs to integers, and build a new pair out of them.
98 return DAG.getNode(ISD::BUILD_PAIR,
99 TLI.getTypeToTransformTo(N->getValueType(0)),
100 BitConvertToInteger(N->getOperand(0)),
101 BitConvertToInteger(N->getOperand(1)));
104 SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
105 return DAG.getConstant(N->getValueAPF().convertToAPInt(),
106 TLI.getTypeToTransformTo(N->getValueType(0)));
109 SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
110 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
111 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
112 GetSoftenedFloat(N->getOperand(1)) };
113 return MakeLibCall(GetFPLibCall(N->getValueType(0),
121 SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
122 SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
123 SDOperand RHS = BitConvertToInteger(N->getOperand(1));
125 MVT LVT = LHS.getValueType();
126 MVT RVT = RHS.getValueType();
128 unsigned LSize = LVT.getSizeInBits();
129 unsigned RSize = RVT.getSizeInBits();
131 // First get the sign bit of second operand.
132 SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
133 DAG.getConstant(RSize - 1,
134 TLI.getShiftAmountTy()));
135 SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
137 // Shift right or sign-extend it if the two operands have different types.
138 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
140 SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
141 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
142 SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
143 } else if (SizeDiff < 0) {
144 SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
145 SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
146 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
149 // Clear the sign bit of the first operand.
150 SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
151 DAG.getConstant(LSize - 1,
152 TLI.getShiftAmountTy()));
153 Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
154 LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
156 // Or the value with the sign bit.
157 return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
160 SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
161 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
162 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
163 GetSoftenedFloat(N->getOperand(1)) };
164 return MakeLibCall(GetFPLibCall(N->getValueType(0),
172 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
173 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
174 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
175 GetSoftenedFloat(N->getOperand(1)) };
176 return MakeLibCall(GetFPLibCall(N->getValueType(0),
184 SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
185 LoadSDNode *L = cast<LoadSDNode>(N);
186 MVT VT = N->getValueType(0);
187 MVT NVT = TLI.getTypeToTransformTo(VT);
189 if (L->getExtensionType() == ISD::NON_EXTLOAD)
190 return DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
191 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
192 L->getSrcValue(), L->getSrcValueOffset(), NVT,
193 L->isVolatile(), L->getAlignment());
195 // Do a non-extending load followed by FP_EXTEND.
196 SDOperand NL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
197 L->getMemoryVT(), L->getChain(),
198 L->getBasePtr(), L->getOffset(),
199 L->getSrcValue(), L->getSrcValueOffset(),
201 L->isVolatile(), L->getAlignment());
202 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NL));
205 SDOperand DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
206 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
207 MVT DestVT = N->getValueType(0);
208 SDOperand Op = N->getOperand(0);
210 if (Op.getValueType() == MVT::i32) {
211 // simple 32-bit [signed|unsigned] integer to float/double expansion
213 // Get the stack frame index of a 8 byte buffer.
214 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
216 // word offset constant for Hi/Lo address computation
218 DAG.getConstant(MVT(MVT::i32).getSizeInBits() / 8,
220 // set up Hi and Lo (into buffer) address based on endian
221 SDOperand Hi = StackSlot;
222 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, Offset);
223 if (TLI.isLittleEndian())
226 // if signed map to unsigned space
229 // constant used to invert sign bit (signed to unsigned mapping)
230 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
231 OpMapped = DAG.getNode(ISD::XOR, MVT::i32, Op, SignBit);
235 // store the lo of the constructed double - based on integer input
236 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
237 OpMapped, Lo, NULL, 0);
238 // initial hi portion of constructed double
239 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
240 // store the hi of the constructed double - biased exponent
241 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
242 // load the constructed double
243 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
244 // FP constant to bias correct the final result
245 SDOperand Bias = DAG.getConstantFP(isSigned ?
246 BitsToDouble(0x4330000080000000ULL)
247 : BitsToDouble(0x4330000000000000ULL),
250 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
253 // handle final rounding
254 if (DestVT == MVT::f64) {
257 } else if (DestVT.bitsLT(MVT::f64)) {
258 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
259 DAG.getIntPtrConstant(0));
260 } else if (DestVT.bitsGT(MVT::f64)) {
261 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
263 return BitConvertToInteger(Result);
265 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
266 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op);
268 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op), Op,
269 DAG.getConstant(0, Op.getValueType()),
271 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
272 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
273 SignSet, Four, Zero);
275 // If the sign bit of the integer is set, the large number will be treated
276 // as a negative number. To counteract this, the dynamic code adds an
277 // offset depending on the data type.
279 switch (Op.getValueType().getSimpleVT()) {
280 default: assert(0 && "Unsupported integer type!");
281 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
282 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
283 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
284 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
286 if (TLI.isLittleEndian()) FF <<= 32;
287 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
289 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
290 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
291 SDOperand FudgeInReg;
292 if (DestVT == MVT::f32)
293 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
294 PseudoSourceValue::getConstantPool(), 0);
296 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestVT,
297 DAG.getEntryNode(), CPIdx,
298 PseudoSourceValue::getConstantPool(), 0,
302 return BitConvertToInteger(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
306 //===----------------------------------------------------------------------===//
307 // Operand Float to Integer Conversion..
308 //===----------------------------------------------------------------------===//
310 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
311 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
315 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
316 == TargetLowering::Custom)
317 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
320 switch (N->getOpcode()) {
323 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
324 N->dump(&DAG); cerr << "\n";
326 assert(0 && "Do not know how to convert this operator's operand!");
329 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
331 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
332 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
333 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
337 // If the result is null, the sub-method took care of registering results etc.
338 if (!Res.Val) return false;
340 // If the result is N, the sub-method updated N in place. Check to see if any
341 // operands are new, and if so, mark them.
343 // Mark N as new and remark N and its operands. This allows us to correctly
344 // revisit N if it needs another step of promotion and allows us to visit
345 // any new operands to N.
350 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
351 "Invalid operand expansion");
353 ReplaceValueWith(SDOperand(N, 0), Res);
357 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
358 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
359 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
360 ISD::CondCode &CCCode) {
361 SDOperand LHSInt = GetSoftenedFloat(NewLHS);
362 SDOperand RHSInt = GetSoftenedFloat(NewRHS);
363 MVT VT = NewLHS.getValueType();
364 MVT NVT = LHSInt.getValueType();
366 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
368 // Expand into one or more soft-fp libcall(s).
369 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
373 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
377 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
381 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
385 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
389 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
393 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
396 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
401 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
404 // SETONE = SETOLT | SETOGT
405 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
408 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
411 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
414 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
417 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
420 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
422 default: assert(false && "Do not know how to soften this setcc!");
426 SDOperand Ops[2] = { LHSInt, RHSInt };
427 NewLHS = MakeLibCall(LC1, NVT, Ops, 2, false/*sign irrelevant*/);
428 NewRHS = DAG.getConstant(0, NVT);
429 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
430 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
432 DAG.getCondCode(TLI.getCmpLibcallCC(LC1)));
433 NewLHS = MakeLibCall(LC2, NVT, Ops, 2, false/*sign irrelevant*/);
434 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
435 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
436 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
437 NewRHS = SDOperand();
441 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
442 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
443 GetSoftenedFloat(N->getOperand(0)));
446 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
447 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
448 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
449 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
451 // If SoftenSetCCOperands returned a scalar, we need to compare the result
452 // against zero to select between true and false values.
453 if (NewRHS.Val == 0) {
454 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
458 // Update N to have the operands specified.
459 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
460 DAG.getCondCode(CCCode), NewLHS, NewRHS,
464 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
465 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
466 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
467 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
469 // If SoftenSetCCOperands returned a scalar, we need to compare the result
470 // against zero to select between true and false values.
471 if (NewRHS.Val == 0) {
472 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
476 // Update N to have the operands specified.
477 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
478 N->getOperand(2), N->getOperand(3),
479 DAG.getCondCode(CCCode));
482 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
483 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
484 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
485 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
487 // If SoftenSetCCOperands returned a scalar, use it.
488 if (NewRHS.Val == 0) {
489 assert(NewLHS.getValueType() == N->getValueType(0) &&
490 "Unexpected setcc expansion!");
494 // Otherwise, update N to have the operands specified.
495 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
496 DAG.getCondCode(CCCode));
500 //===----------------------------------------------------------------------===//
501 // Float Result Expansion
502 //===----------------------------------------------------------------------===//
504 /// ExpandFloatResult - This method is called when the specified result of the
505 /// specified node is found to need expansion. At this point, the node may also
506 /// have invalid operands or may have other results that need promotion, we just
507 /// know that (at least) one result needs expansion.
508 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
509 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
511 Lo = Hi = SDOperand();
513 // See if the target wants to custom expand this node.
514 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
515 TargetLowering::Custom) {
516 // If the target wants to, allow it to lower this itself.
517 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
518 // Everything that once used N now uses P. We are guaranteed that the
519 // result value types of N and the result value types of P match.
520 ReplaceNodeWith(N, P);
525 switch (N->getOpcode()) {
528 cerr << "ExpandFloatResult #" << ResNo << ": ";
529 N->dump(&DAG); cerr << "\n";
531 assert(0 && "Do not know how to expand the result of this operator!");
534 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
535 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
536 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
537 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
539 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
540 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
541 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
542 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
544 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
545 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
546 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
547 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
548 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
549 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
550 case ISD::SINT_TO_FP:
551 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
554 // If Lo/Hi is null, the sub-method took care of registering results etc.
556 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
559 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
561 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
562 assert(NVT.getSizeInBits() == integerPartWidth &&
563 "Do not know how to expand this float constant!");
564 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
565 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
566 &C.getRawData()[1])), NVT);
567 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
568 &C.getRawData()[0])), NVT);
571 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
573 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
574 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
579 N->getValueType(0), Ops, 2,
581 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
582 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
585 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
587 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
588 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
593 N->getValueType(0), Ops, 2,
595 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
596 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
599 void DAGTypeLegalizer::ExpandFloatRes_FMUL(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_FSUB(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_LOAD(SDNode *N, SDOperand &Lo,
629 if (ISD::isNormalLoad(N)) {
630 ExpandRes_NormalLoad(N, Lo, Hi);
634 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
635 LoadSDNode *LD = cast<LoadSDNode>(N);
636 SDOperand Chain = LD->getChain();
637 SDOperand Ptr = LD->getBasePtr();
639 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
640 assert(NVT.isByteSized() && "Expanded type not byte sized!");
641 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
643 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
644 LD->getSrcValue(), LD->getSrcValueOffset(),
646 LD->isVolatile(), LD->getAlignment());
648 // Remember the chain.
649 Chain = Lo.getValue(1);
651 // The high part is undefined.
652 Hi = DAG.getNode(ISD::UNDEF, NVT);
654 // Modified the chain - switch anything that used the old chain to use the
656 ReplaceValueWith(SDOperand(LD, 1), Chain);
659 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
661 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
662 MVT VT = N->getValueType(0);
663 MVT NVT = TLI.getTypeToTransformTo(VT);
664 SDOperand Src = N->getOperand(0);
665 MVT SrcVT = Src.getValueType();
667 // First do an SINT_TO_FP, whether the original was signed or unsigned.
668 if (SrcVT.bitsLE(MVT::i32)) {
669 // The integer can be represented exactly in an f64.
670 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
671 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
672 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
674 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
675 if (SrcVT.bitsLE(MVT::i64)) {
676 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
677 LC = RTLIB::SINTTOFP_I64_PPCF128;
678 } else if (SrcVT.bitsLE(MVT::i128)) {
679 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
680 LC = RTLIB::SINTTOFP_I128_PPCF128;
682 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
684 Hi = MakeLibCall(LC, VT, &Src, 1, true);
685 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
686 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
689 if (N->getOpcode() == ISD::SINT_TO_FP)
692 // Unsigned - fix up the SINT_TO_FP value just calculated.
693 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
694 SrcVT = Src.getValueType();
696 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
697 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
698 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
699 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
700 const uint64_t *Parts = 0;
702 switch (SrcVT.getSimpleVT()) {
704 assert(false && "Unsupported UINT_TO_FP!");
713 Lo = DAG.getNode(ISD::FADD, VT, Hi,
714 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
716 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
717 DAG.getCondCode(ISD::SETLT));
718 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
719 DAG.getConstant(1, TLI.getPointerTy()));
720 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
721 DAG.getConstant(0, TLI.getPointerTy()));
725 //===----------------------------------------------------------------------===//
726 // Float Operand Expansion
727 //===----------------------------------------------------------------------===//
729 /// ExpandFloatOperand - This method is called when the specified operand of the
730 /// specified node is found to need expansion. At this point, all of the result
731 /// types of the node are known to be legal, but other operands of the node may
732 /// need promotion or expansion as well as the specified one.
733 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
734 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
737 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
738 == TargetLowering::Custom)
739 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
742 switch (N->getOpcode()) {
745 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
746 N->dump(&DAG); cerr << "\n";
748 assert(0 && "Do not know how to expand this operator's operand!");
751 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
752 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
753 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
755 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
756 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
757 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
759 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
760 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
761 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
764 Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
769 // If the result is null, the sub-method took care of registering results etc.
770 if (!Res.Val) return false;
771 // If the result is N, the sub-method updated N in place. Check to see if any
772 // operands are new, and if so, mark them.
774 // Mark N as new and remark N and its operands. This allows us to correctly
775 // revisit N if it needs another step of expansion and allows us to visit
776 // any new operands to N.
781 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
782 "Invalid operand expansion");
784 ReplaceValueWith(SDOperand(N, 0), Res);
788 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
789 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
790 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
792 ISD::CondCode &CCCode) {
793 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
794 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
795 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
797 MVT VT = NewLHS.getValueType();
798 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
800 // FIXME: This generated code sucks. We want to generate
801 // FCMP crN, hi1, hi2
803 // FCMP crN, lo1, lo2
804 // The following can be improved, but not that much.
805 SDOperand Tmp1, Tmp2, Tmp3;
806 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
807 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
808 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
809 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
810 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
811 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
812 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
813 NewRHS = SDOperand(); // LHS is the result, not a compare.
816 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
817 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
818 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
819 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
821 // If ExpandSetCCOperands returned a scalar, we need to compare the result
822 // against zero to select between true and false values.
823 if (NewRHS.Val == 0) {
824 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
828 // Update N to have the operands specified.
829 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
830 DAG.getCondCode(CCCode), NewLHS, NewRHS,
834 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
835 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
836 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
837 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
839 // If ExpandSetCCOperands returned a scalar, we need to compare the result
840 // against zero to select between true and false values.
841 if (NewRHS.Val == 0) {
842 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
846 // Update N to have the operands specified.
847 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
848 N->getOperand(2), N->getOperand(3),
849 DAG.getCondCode(CCCode));
852 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
853 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
854 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
855 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
857 // If ExpandSetCCOperands returned a scalar, use it.
858 if (NewRHS.Val == 0) {
859 assert(NewLHS.getValueType() == N->getValueType(0) &&
860 "Unexpected setcc expansion!");
864 // Otherwise, update N to have the operands specified.
865 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
866 DAG.getCondCode(CCCode));
869 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
870 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
871 "Unsupported FP_TO_UINT!");
873 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
874 switch (N->getValueType(0).getSimpleVT()) {
876 assert(false && "Unsupported FP_TO_UINT!");
878 LC = RTLIB::FPTOUINT_PPCF128_I32;
881 LC = RTLIB::FPTOUINT_PPCF128_I64;
884 LC = RTLIB::FPTOUINT_PPCF128_I128;
888 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
891 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
892 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
893 "Unsupported FP_TO_SINT!");
895 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
896 switch (N->getValueType(0).getSimpleVT()) {
898 assert(false && "Unsupported FP_TO_SINT!");
900 LC = RTLIB::FPTOSINT_PPCF128_I32;
902 LC = RTLIB::FPTOSINT_PPCF128_I64;
905 LC = RTLIB::FPTOSINT_PPCF128_I64;
909 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
912 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
913 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
914 "Logic only correct for ppcf128!");
916 GetExpandedFloat(N->getOperand(0), Lo, Hi);
917 // Round it the rest of the way (e.g. to f32) if needed.
918 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
921 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
922 if (ISD::isNormalStore(N))
923 return ExpandOp_NormalStore(N, OpNo);
925 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
926 assert(OpNo == 1 && "Can only expand the stored value so far");
927 StoreSDNode *ST = cast<StoreSDNode>(N);
929 SDOperand Chain = ST->getChain();
930 SDOperand Ptr = ST->getBasePtr();
932 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
933 assert(NVT.isByteSized() && "Expanded type not byte sized!");
934 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
937 GetExpandedOp(ST->getValue(), Lo, Hi);
939 return DAG.getTruncStore(Chain, Lo, Ptr,
940 ST->getSrcValue(), ST->getSrcValueOffset(),
942 ST->isVolatile(), ST->getAlignment());