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;
334 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
338 // If the result is null, the sub-method took care of registering results etc.
339 if (!Res.Val) return false;
341 // If the result is N, the sub-method updated N in place. Check to see if any
342 // operands are new, and if so, mark them.
344 // Mark N as new and remark N and its operands. This allows us to correctly
345 // revisit N if it needs another step of promotion and allows us to visit
346 // any new operands to N.
351 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
352 "Invalid operand expansion");
354 ReplaceValueWith(SDOperand(N, 0), Res);
358 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
359 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
360 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
361 ISD::CondCode &CCCode) {
362 SDOperand LHSInt = GetSoftenedFloat(NewLHS);
363 SDOperand RHSInt = GetSoftenedFloat(NewRHS);
364 MVT VT = NewLHS.getValueType();
365 MVT NVT = LHSInt.getValueType();
367 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
369 // Expand into one or more soft-fp libcall(s).
370 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
374 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
378 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
382 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
386 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
390 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
394 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
397 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
402 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
405 // SETONE = SETOLT | SETOGT
406 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
409 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
412 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
415 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
418 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
421 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
423 default: assert(false && "Do not know how to soften this setcc!");
427 SDOperand Ops[2] = { LHSInt, RHSInt };
428 NewLHS = MakeLibCall(LC1, NVT, Ops, 2, false/*sign irrelevant*/);
429 NewRHS = DAG.getConstant(0, NVT);
430 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
431 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
433 DAG.getCondCode(TLI.getCmpLibcallCC(LC1)));
434 NewLHS = MakeLibCall(LC2, NVT, Ops, 2, false/*sign irrelevant*/);
435 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
436 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
437 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
438 NewRHS = SDOperand();
442 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
443 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
444 GetSoftenedFloat(N->getOperand(0)));
447 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
448 SDOperand 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.Val == 0) {
455 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
459 // Update N to have the operands specified.
460 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
461 DAG.getCondCode(CCCode), NewLHS, NewRHS,
465 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
466 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
467 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
468 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
470 // If SoftenSetCCOperands returned a scalar, we need to compare the result
471 // against zero to select between true and false values.
472 if (NewRHS.Val == 0) {
473 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
477 // Update N to have the operands specified.
478 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
479 N->getOperand(2), N->getOperand(3),
480 DAG.getCondCode(CCCode));
483 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
484 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
485 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
486 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
488 // If SoftenSetCCOperands returned a scalar, use it.
489 if (NewRHS.Val == 0) {
490 assert(NewLHS.getValueType() == N->getValueType(0) &&
491 "Unexpected setcc expansion!");
495 // Otherwise, update N to have the operands specified.
496 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
497 DAG.getCondCode(CCCode));
500 SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
501 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
502 assert(OpNo == 1 && "Can only soften the stored value!");
503 StoreSDNode *ST = cast<StoreSDNode>(N);
504 SDOperand Val = ST->getValue();
506 if (ST->isTruncatingStore())
507 // Do an FP_ROUND followed by a non-truncating store.
508 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
509 Val, DAG.getIntPtrConstant(0)));
511 Val = GetSoftenedFloat(Val);
513 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
514 ST->getSrcValue(), ST->getSrcValueOffset(),
515 ST->isVolatile(), ST->getAlignment());
519 //===----------------------------------------------------------------------===//
520 // Float Result Expansion
521 //===----------------------------------------------------------------------===//
523 /// ExpandFloatResult - This method is called when the specified result of the
524 /// specified node is found to need expansion. At this point, the node may also
525 /// have invalid operands or may have other results that need promotion, we just
526 /// know that (at least) one result needs expansion.
527 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
528 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
530 Lo = Hi = SDOperand();
532 // See if the target wants to custom expand this node.
533 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
534 TargetLowering::Custom) {
535 // If the target wants to, allow it to lower this itself.
536 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
537 // Everything that once used N now uses P. We are guaranteed that the
538 // result value types of N and the result value types of P match.
539 ReplaceNodeWith(N, P);
544 switch (N->getOpcode()) {
547 cerr << "ExpandFloatResult #" << ResNo << ": ";
548 N->dump(&DAG); cerr << "\n";
550 assert(0 && "Do not know how to expand the result of this operator!");
553 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
554 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
555 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
556 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
558 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
559 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
560 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
561 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
563 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
564 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
565 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
566 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
567 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
568 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
569 case ISD::SINT_TO_FP:
570 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
573 // If Lo/Hi is null, the sub-method took care of registering results etc.
575 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
578 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
580 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
581 assert(NVT.getSizeInBits() == integerPartWidth &&
582 "Do not know how to expand this float constant!");
583 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
584 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
585 &C.getRawData()[1])), NVT);
586 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
587 &C.getRawData()[0])), NVT);
590 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
592 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
593 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
598 N->getValueType(0), Ops, 2,
600 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
601 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
604 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
606 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
607 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
612 N->getValueType(0), Ops, 2,
614 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
615 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
618 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
620 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
621 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
626 N->getValueType(0), Ops, 2,
628 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
629 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
632 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
634 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
635 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
640 N->getValueType(0), Ops, 2,
642 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
643 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
646 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
648 if (ISD::isNormalLoad(N)) {
649 ExpandRes_NormalLoad(N, Lo, Hi);
653 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
654 LoadSDNode *LD = cast<LoadSDNode>(N);
655 SDOperand Chain = LD->getChain();
656 SDOperand Ptr = LD->getBasePtr();
658 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
659 assert(NVT.isByteSized() && "Expanded type not byte sized!");
660 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
662 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
663 LD->getSrcValue(), LD->getSrcValueOffset(),
665 LD->isVolatile(), LD->getAlignment());
667 // Remember the chain.
668 Chain = Lo.getValue(1);
670 // The high part is undefined.
671 Hi = DAG.getNode(ISD::UNDEF, NVT);
673 // Modified the chain - switch anything that used the old chain to use the
675 ReplaceValueWith(SDOperand(LD, 1), Chain);
678 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
680 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
681 MVT VT = N->getValueType(0);
682 MVT NVT = TLI.getTypeToTransformTo(VT);
683 SDOperand Src = N->getOperand(0);
684 MVT SrcVT = Src.getValueType();
686 // First do an SINT_TO_FP, whether the original was signed or unsigned.
687 if (SrcVT.bitsLE(MVT::i32)) {
688 // The integer can be represented exactly in an f64.
689 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
690 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
691 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
693 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
694 if (SrcVT.bitsLE(MVT::i64)) {
695 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
696 LC = RTLIB::SINTTOFP_I64_PPCF128;
697 } else if (SrcVT.bitsLE(MVT::i128)) {
698 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
699 LC = RTLIB::SINTTOFP_I128_PPCF128;
701 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
703 Hi = MakeLibCall(LC, VT, &Src, 1, true);
704 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
705 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
708 if (N->getOpcode() == ISD::SINT_TO_FP)
711 // Unsigned - fix up the SINT_TO_FP value just calculated.
712 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
713 SrcVT = Src.getValueType();
715 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
716 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
717 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
718 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
719 const uint64_t *Parts = 0;
721 switch (SrcVT.getSimpleVT()) {
723 assert(false && "Unsupported UINT_TO_FP!");
732 Lo = DAG.getNode(ISD::FADD, VT, Hi,
733 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
735 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
736 DAG.getCondCode(ISD::SETLT));
737 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
738 DAG.getConstant(1, TLI.getPointerTy()));
739 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
740 DAG.getConstant(0, TLI.getPointerTy()));
744 //===----------------------------------------------------------------------===//
745 // Float Operand Expansion
746 //===----------------------------------------------------------------------===//
748 /// ExpandFloatOperand - This method is called when the specified operand of the
749 /// specified node is found to need expansion. At this point, all of the result
750 /// types of the node are known to be legal, but other operands of the node may
751 /// need promotion or expansion as well as the specified one.
752 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
753 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
756 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
757 == TargetLowering::Custom)
758 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
761 switch (N->getOpcode()) {
764 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
765 N->dump(&DAG); cerr << "\n";
767 assert(0 && "Do not know how to expand this operator's operand!");
770 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
771 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
772 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
774 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
775 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
776 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
778 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
779 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
780 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
783 Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
788 // If the result is null, the sub-method took care of registering results etc.
789 if (!Res.Val) return false;
790 // If the result is N, the sub-method updated N in place. Check to see if any
791 // operands are new, and if so, mark them.
793 // Mark N as new and remark N and its operands. This allows us to correctly
794 // revisit N if it needs another step of expansion and allows us to visit
795 // any new operands to N.
800 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
801 "Invalid operand expansion");
803 ReplaceValueWith(SDOperand(N, 0), Res);
807 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
808 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
809 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
811 ISD::CondCode &CCCode) {
812 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
813 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
814 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
816 MVT VT = NewLHS.getValueType();
817 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
819 // FIXME: This generated code sucks. We want to generate
820 // FCMP crN, hi1, hi2
822 // FCMP crN, lo1, lo2
823 // The following can be improved, but not that much.
824 SDOperand Tmp1, Tmp2, Tmp3;
825 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
826 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
827 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
828 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
829 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
830 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
831 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
832 NewRHS = SDOperand(); // LHS is the result, not a compare.
835 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
836 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
837 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
838 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
840 // If ExpandSetCCOperands returned a scalar, we need to compare the result
841 // against zero to select between true and false values.
842 if (NewRHS.Val == 0) {
843 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
847 // Update N to have the operands specified.
848 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
849 DAG.getCondCode(CCCode), NewLHS, NewRHS,
853 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
854 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
855 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
856 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
858 // If ExpandSetCCOperands returned a scalar, we need to compare the result
859 // against zero to select between true and false values.
860 if (NewRHS.Val == 0) {
861 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
865 // Update N to have the operands specified.
866 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
867 N->getOperand(2), N->getOperand(3),
868 DAG.getCondCode(CCCode));
871 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
872 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
873 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
874 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
876 // If ExpandSetCCOperands returned a scalar, use it.
877 if (NewRHS.Val == 0) {
878 assert(NewLHS.getValueType() == N->getValueType(0) &&
879 "Unexpected setcc expansion!");
883 // Otherwise, update N to have the operands specified.
884 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
885 DAG.getCondCode(CCCode));
888 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
889 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
890 "Unsupported FP_TO_UINT!");
892 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
893 switch (N->getValueType(0).getSimpleVT()) {
895 assert(false && "Unsupported FP_TO_UINT!");
897 LC = RTLIB::FPTOUINT_PPCF128_I32;
900 LC = RTLIB::FPTOUINT_PPCF128_I64;
903 LC = RTLIB::FPTOUINT_PPCF128_I128;
907 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
910 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
911 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
912 "Unsupported FP_TO_SINT!");
914 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
915 switch (N->getValueType(0).getSimpleVT()) {
917 assert(false && "Unsupported FP_TO_SINT!");
919 LC = RTLIB::FPTOSINT_PPCF128_I32;
921 LC = RTLIB::FPTOSINT_PPCF128_I64;
924 LC = RTLIB::FPTOSINT_PPCF128_I64;
928 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
931 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
932 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
933 "Logic only correct for ppcf128!");
935 GetExpandedFloat(N->getOperand(0), Lo, Hi);
936 // Round it the rest of the way (e.g. to f32) if needed.
937 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
940 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
941 if (ISD::isNormalStore(N))
942 return ExpandOp_NormalStore(N, OpNo);
944 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
945 assert(OpNo == 1 && "Can only expand the stored value so far");
946 StoreSDNode *ST = cast<StoreSDNode>(N);
948 SDOperand Chain = ST->getChain();
949 SDOperand Ptr = ST->getBasePtr();
951 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
952 assert(NVT.isByteSized() && "Expanded type not byte sized!");
953 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
956 GetExpandedOp(ST->getValue(), Lo, Hi);
958 return DAG.getTruncStore(Chain, Lo, Ptr,
959 ST->getSrcValue(), ST->getSrcValueOffset(),
961 ST->isVolatile(), ST->getAlignment());