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 soften 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::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
79 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
80 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
82 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
84 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
85 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
86 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
89 // If R is null, the sub-method took care of registering the result.
91 SetSoftenedFloat(SDOperand(N, ResNo), R);
94 SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
95 return BitConvertToInteger(N->getOperand(0));
98 SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
99 // Convert the inputs to integers, and build a new pair out of them.
100 return DAG.getNode(ISD::BUILD_PAIR,
101 TLI.getTypeToTransformTo(N->getValueType(0)),
102 BitConvertToInteger(N->getOperand(0)),
103 BitConvertToInteger(N->getOperand(1)));
106 SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
107 return DAG.getConstant(N->getValueAPF().convertToAPInt(),
108 TLI.getTypeToTransformTo(N->getValueType(0)));
111 SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
112 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
113 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
114 GetSoftenedFloat(N->getOperand(1)) };
115 return MakeLibCall(GetFPLibCall(N->getValueType(0),
123 SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
124 SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
125 SDOperand RHS = BitConvertToInteger(N->getOperand(1));
127 MVT LVT = LHS.getValueType();
128 MVT RVT = RHS.getValueType();
130 unsigned LSize = LVT.getSizeInBits();
131 unsigned RSize = RVT.getSizeInBits();
133 // First get the sign bit of second operand.
134 SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
135 DAG.getConstant(RSize - 1,
136 TLI.getShiftAmountTy()));
137 SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
139 // Shift right or sign-extend it if the two operands have different types.
140 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
142 SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
143 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
144 SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
145 } else if (SizeDiff < 0) {
146 SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
147 SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
148 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
151 // Clear the sign bit of the first operand.
152 SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
153 DAG.getConstant(LSize - 1,
154 TLI.getShiftAmountTy()));
155 Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
156 LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
158 // Or the value with the sign bit.
159 return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
162 SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
163 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
164 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
165 GetSoftenedFloat(N->getOperand(1)) };
166 return MakeLibCall(GetFPLibCall(N->getValueType(0),
174 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
175 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
176 SDOperand Op = N->getOperand(0);
178 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
179 switch (Op.getValueType().getSimpleVT()) {
181 assert(false && "Unsupported FP_EXTEND!");
183 switch (N->getValueType(0).getSimpleVT()) {
185 assert(false && "Unsupported FP_EXTEND!");
187 LC = RTLIB::FPEXT_F32_F64;
191 return MakeLibCall(LC, NVT, &Op, 1, false);
194 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
195 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
196 SDOperand Op = N->getOperand(0);
198 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
199 switch (Op.getValueType().getSimpleVT()) {
201 assert(false && "Unsupported FP_ROUND!");
203 switch (N->getValueType(0).getSimpleVT()) {
205 assert(false && "Unsupported FP_ROUND!");
207 LC = RTLIB::FPROUND_F64_F32;
211 return MakeLibCall(LC, NVT, &Op, 1, false);
214 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
215 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
216 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
217 GetSoftenedFloat(N->getOperand(1)) };
218 return MakeLibCall(GetFPLibCall(N->getValueType(0),
226 SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
227 LoadSDNode *L = cast<LoadSDNode>(N);
228 MVT VT = N->getValueType(0);
229 MVT NVT = TLI.getTypeToTransformTo(VT);
231 if (L->getExtensionType() == ISD::NON_EXTLOAD)
232 return DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
233 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
234 L->getSrcValue(), L->getSrcValueOffset(), NVT,
235 L->isVolatile(), L->getAlignment());
237 // Do a non-extending load followed by FP_EXTEND.
238 SDOperand NL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
239 L->getMemoryVT(), L->getChain(),
240 L->getBasePtr(), L->getOffset(),
241 L->getSrcValue(), L->getSrcValueOffset(),
243 L->isVolatile(), L->getAlignment());
244 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NL));
247 SDOperand DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
248 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
249 MVT DestVT = N->getValueType(0);
250 SDOperand Op = N->getOperand(0);
252 if (Op.getValueType() == MVT::i32) {
253 // simple 32-bit [signed|unsigned] integer to float/double expansion
255 // Get the stack frame index of a 8 byte buffer.
256 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
258 // word offset constant for Hi/Lo address computation
260 DAG.getConstant(MVT(MVT::i32).getSizeInBits() / 8,
262 // set up Hi and Lo (into buffer) address based on endian
263 SDOperand Hi = StackSlot;
264 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, Offset);
265 if (TLI.isLittleEndian())
268 // if signed map to unsigned space
271 // constant used to invert sign bit (signed to unsigned mapping)
272 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
273 OpMapped = DAG.getNode(ISD::XOR, MVT::i32, Op, SignBit);
277 // store the lo of the constructed double - based on integer input
278 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
279 OpMapped, Lo, NULL, 0);
280 // initial hi portion of constructed double
281 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
282 // store the hi of the constructed double - biased exponent
283 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
284 // load the constructed double
285 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
286 // FP constant to bias correct the final result
287 SDOperand Bias = DAG.getConstantFP(isSigned ?
288 BitsToDouble(0x4330000080000000ULL)
289 : BitsToDouble(0x4330000000000000ULL),
292 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
295 // handle final rounding
296 if (DestVT == MVT::f64) {
299 } else if (DestVT.bitsLT(MVT::f64)) {
300 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
301 DAG.getIntPtrConstant(0));
302 } else if (DestVT.bitsGT(MVT::f64)) {
303 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
305 return BitConvertToInteger(Result);
307 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
308 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op);
310 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op), Op,
311 DAG.getConstant(0, Op.getValueType()),
313 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
314 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
315 SignSet, Four, Zero);
317 // If the sign bit of the integer is set, the large number will be treated
318 // as a negative number. To counteract this, the dynamic code adds an
319 // offset depending on the data type.
321 switch (Op.getValueType().getSimpleVT()) {
322 default: assert(0 && "Unsupported integer type!");
323 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
324 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
325 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
326 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
328 if (TLI.isLittleEndian()) FF <<= 32;
329 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
331 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
332 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
333 SDOperand FudgeInReg;
334 if (DestVT == MVT::f32)
335 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
336 PseudoSourceValue::getConstantPool(), 0);
338 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestVT,
339 DAG.getEntryNode(), CPIdx,
340 PseudoSourceValue::getConstantPool(), 0,
344 return BitConvertToInteger(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
348 //===----------------------------------------------------------------------===//
349 // Operand Float to Integer Conversion..
350 //===----------------------------------------------------------------------===//
352 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
353 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
357 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
358 == TargetLowering::Custom)
359 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
362 switch (N->getOpcode()) {
365 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
366 N->dump(&DAG); cerr << "\n";
368 assert(0 && "Do not know how to soften this operator's operand!");
371 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
373 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
374 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
375 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
376 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
380 // If the result is null, the sub-method took care of registering results etc.
381 if (!Res.Val) return false;
383 // If the result is N, the sub-method updated N in place. Check to see if any
384 // operands are new, and if so, mark them.
386 // Mark N as new and remark N and its operands. This allows us to correctly
387 // revisit N if it needs another step of promotion and allows us to visit
388 // any new operands to N.
393 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
394 "Invalid operand expansion");
396 ReplaceValueWith(SDOperand(N, 0), Res);
400 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
401 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
402 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
403 ISD::CondCode &CCCode) {
404 SDOperand LHSInt = GetSoftenedFloat(NewLHS);
405 SDOperand RHSInt = GetSoftenedFloat(NewRHS);
406 MVT VT = NewLHS.getValueType();
407 MVT NVT = LHSInt.getValueType();
409 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
411 // Expand into one or more soft-fp libcall(s).
412 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
416 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
420 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
424 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
428 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
432 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
436 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
439 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
444 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
447 // SETONE = SETOLT | SETOGT
448 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
451 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
454 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
457 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
460 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
463 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
465 default: assert(false && "Do not know how to soften this setcc!");
469 SDOperand Ops[2] = { LHSInt, RHSInt };
470 NewLHS = MakeLibCall(LC1, NVT, Ops, 2, false/*sign irrelevant*/);
471 NewRHS = DAG.getConstant(0, NVT);
472 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
473 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
475 DAG.getCondCode(TLI.getCmpLibcallCC(LC1)));
476 NewLHS = MakeLibCall(LC2, NVT, Ops, 2, false/*sign irrelevant*/);
477 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
478 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
479 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
480 NewRHS = SDOperand();
484 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
485 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
486 GetSoftenedFloat(N->getOperand(0)));
489 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
490 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
491 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
492 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
494 // If SoftenSetCCOperands returned a scalar, we need to compare the result
495 // against zero to select between true and false values.
496 if (NewRHS.Val == 0) {
497 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
501 // Update N to have the operands specified.
502 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
503 DAG.getCondCode(CCCode), NewLHS, NewRHS,
507 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
508 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
509 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
510 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
512 // If SoftenSetCCOperands returned a scalar, we need to compare the result
513 // against zero to select between true and false values.
514 if (NewRHS.Val == 0) {
515 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
519 // Update N to have the operands specified.
520 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
521 N->getOperand(2), N->getOperand(3),
522 DAG.getCondCode(CCCode));
525 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
526 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
527 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
528 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
530 // If SoftenSetCCOperands returned a scalar, use it.
531 if (NewRHS.Val == 0) {
532 assert(NewLHS.getValueType() == N->getValueType(0) &&
533 "Unexpected setcc expansion!");
537 // Otherwise, update N to have the operands specified.
538 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
539 DAG.getCondCode(CCCode));
542 SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
543 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
544 assert(OpNo == 1 && "Can only soften the stored value!");
545 StoreSDNode *ST = cast<StoreSDNode>(N);
546 SDOperand Val = ST->getValue();
548 if (ST->isTruncatingStore())
549 // Do an FP_ROUND followed by a non-truncating store.
550 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
551 Val, DAG.getIntPtrConstant(0)));
553 Val = GetSoftenedFloat(Val);
555 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
556 ST->getSrcValue(), ST->getSrcValueOffset(),
557 ST->isVolatile(), ST->getAlignment());
561 //===----------------------------------------------------------------------===//
562 // Float Result Expansion
563 //===----------------------------------------------------------------------===//
565 /// ExpandFloatResult - This method is called when the specified result of the
566 /// specified node is found to need expansion. At this point, the node may also
567 /// have invalid operands or may have other results that need promotion, we just
568 /// know that (at least) one result needs expansion.
569 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
570 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
572 Lo = Hi = SDOperand();
574 // See if the target wants to custom expand this node.
575 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
576 TargetLowering::Custom) {
577 // If the target wants to, allow it to lower this itself.
578 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
579 // Everything that once used N now uses P. We are guaranteed that the
580 // result value types of N and the result value types of P match.
581 ReplaceNodeWith(N, P);
586 switch (N->getOpcode()) {
589 cerr << "ExpandFloatResult #" << ResNo << ": ";
590 N->dump(&DAG); cerr << "\n";
592 assert(0 && "Do not know how to expand the result of this operator!");
595 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
596 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
597 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
598 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
600 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
601 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
602 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
603 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
605 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
606 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
607 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
608 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
609 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
610 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
611 case ISD::SINT_TO_FP:
612 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
615 // If Lo/Hi is null, the sub-method took care of registering results etc.
617 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
620 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
622 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
623 assert(NVT.getSizeInBits() == integerPartWidth &&
624 "Do not know how to expand this float constant!");
625 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
626 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
627 &C.getRawData()[1])), NVT);
628 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
629 &C.getRawData()[0])), NVT);
632 void DAGTypeLegalizer::ExpandFloatRes_FADD(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_FDIV(SDNode *N, SDOperand &Lo,
648 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
649 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
654 N->getValueType(0), Ops, 2,
656 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
657 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
660 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
662 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
663 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
668 N->getValueType(0), Ops, 2,
670 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
671 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
674 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
676 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
677 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
682 N->getValueType(0), Ops, 2,
684 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
685 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
688 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
690 if (ISD::isNormalLoad(N)) {
691 ExpandRes_NormalLoad(N, Lo, Hi);
695 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
696 LoadSDNode *LD = cast<LoadSDNode>(N);
697 SDOperand Chain = LD->getChain();
698 SDOperand Ptr = LD->getBasePtr();
700 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
701 assert(NVT.isByteSized() && "Expanded type not byte sized!");
702 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
704 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
705 LD->getSrcValue(), LD->getSrcValueOffset(),
707 LD->isVolatile(), LD->getAlignment());
709 // Remember the chain.
710 Chain = Lo.getValue(1);
712 // The high part is undefined.
713 Hi = DAG.getNode(ISD::UNDEF, NVT);
715 // Modified the chain - switch anything that used the old chain to use the
717 ReplaceValueWith(SDOperand(LD, 1), Chain);
720 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
722 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
723 MVT VT = N->getValueType(0);
724 MVT NVT = TLI.getTypeToTransformTo(VT);
725 SDOperand Src = N->getOperand(0);
726 MVT SrcVT = Src.getValueType();
728 // First do an SINT_TO_FP, whether the original was signed or unsigned.
729 if (SrcVT.bitsLE(MVT::i32)) {
730 // The integer can be represented exactly in an f64.
731 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
732 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
733 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
735 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
736 if (SrcVT.bitsLE(MVT::i64)) {
737 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
738 LC = RTLIB::SINTTOFP_I64_PPCF128;
739 } else if (SrcVT.bitsLE(MVT::i128)) {
740 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
741 LC = RTLIB::SINTTOFP_I128_PPCF128;
743 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
745 Hi = MakeLibCall(LC, VT, &Src, 1, true);
746 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
747 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
750 if (N->getOpcode() == ISD::SINT_TO_FP)
753 // Unsigned - fix up the SINT_TO_FP value just calculated.
754 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
755 SrcVT = Src.getValueType();
757 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
758 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
759 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
760 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
761 const uint64_t *Parts = 0;
763 switch (SrcVT.getSimpleVT()) {
765 assert(false && "Unsupported UINT_TO_FP!");
774 Lo = DAG.getNode(ISD::FADD, VT, Hi,
775 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
777 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
778 DAG.getCondCode(ISD::SETLT));
779 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
780 DAG.getConstant(1, TLI.getPointerTy()));
781 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
782 DAG.getConstant(0, TLI.getPointerTy()));
786 //===----------------------------------------------------------------------===//
787 // Float Operand Expansion
788 //===----------------------------------------------------------------------===//
790 /// ExpandFloatOperand - This method is called when the specified operand of the
791 /// specified node is found to need expansion. At this point, all of the result
792 /// types of the node are known to be legal, but other operands of the node may
793 /// need promotion or expansion as well as the specified one.
794 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
795 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
798 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
799 == TargetLowering::Custom)
800 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
803 switch (N->getOpcode()) {
806 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
807 N->dump(&DAG); cerr << "\n";
809 assert(0 && "Do not know how to expand this operator's operand!");
812 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
813 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
814 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
816 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
817 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
818 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
820 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
821 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
822 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
825 Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
830 // If the result is null, the sub-method took care of registering results etc.
831 if (!Res.Val) return false;
832 // If the result is N, the sub-method updated N in place. Check to see if any
833 // operands are new, and if so, mark them.
835 // Mark N as new and remark N and its operands. This allows us to correctly
836 // revisit N if it needs another step of expansion and allows us to visit
837 // any new operands to N.
842 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
843 "Invalid operand expansion");
845 ReplaceValueWith(SDOperand(N, 0), Res);
849 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
850 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
851 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
853 ISD::CondCode &CCCode) {
854 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
855 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
856 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
858 MVT VT = NewLHS.getValueType();
859 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
861 // FIXME: This generated code sucks. We want to generate
862 // FCMP crN, hi1, hi2
864 // FCMP crN, lo1, lo2
865 // The following can be improved, but not that much.
866 SDOperand Tmp1, Tmp2, Tmp3;
867 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
868 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
869 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
870 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
871 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
872 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
873 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
874 NewRHS = SDOperand(); // LHS is the result, not a compare.
877 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
878 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
879 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
880 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
882 // If ExpandSetCCOperands returned a scalar, we need to compare the result
883 // against zero to select between true and false values.
884 if (NewRHS.Val == 0) {
885 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
889 // Update N to have the operands specified.
890 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
891 DAG.getCondCode(CCCode), NewLHS, NewRHS,
895 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
896 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
897 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
898 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
900 // If ExpandSetCCOperands returned a scalar, we need to compare the result
901 // against zero to select between true and false values.
902 if (NewRHS.Val == 0) {
903 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
907 // Update N to have the operands specified.
908 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
909 N->getOperand(2), N->getOperand(3),
910 DAG.getCondCode(CCCode));
913 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
914 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
915 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
916 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
918 // If ExpandSetCCOperands returned a scalar, use it.
919 if (NewRHS.Val == 0) {
920 assert(NewLHS.getValueType() == N->getValueType(0) &&
921 "Unexpected setcc expansion!");
925 // Otherwise, update N to have the operands specified.
926 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
927 DAG.getCondCode(CCCode));
930 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
931 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
932 "Unsupported FP_TO_UINT!");
934 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
935 switch (N->getValueType(0).getSimpleVT()) {
937 assert(false && "Unsupported FP_TO_UINT!");
939 LC = RTLIB::FPTOUINT_PPCF128_I32;
942 LC = RTLIB::FPTOUINT_PPCF128_I64;
945 LC = RTLIB::FPTOUINT_PPCF128_I128;
949 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
952 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
953 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
954 "Unsupported FP_TO_SINT!");
956 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
957 switch (N->getValueType(0).getSimpleVT()) {
959 assert(false && "Unsupported FP_TO_SINT!");
961 LC = RTLIB::FPTOSINT_PPCF128_I32;
963 LC = RTLIB::FPTOSINT_PPCF128_I64;
966 LC = RTLIB::FPTOSINT_PPCF128_I64;
970 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
973 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
974 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
975 "Logic only correct for ppcf128!");
977 GetExpandedFloat(N->getOperand(0), Lo, Hi);
978 // Round it the rest of the way (e.g. to f32) if needed.
979 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
982 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
983 if (ISD::isNormalStore(N))
984 return ExpandOp_NormalStore(N, OpNo);
986 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
987 assert(OpNo == 1 && "Can only expand the stored value so far");
988 StoreSDNode *ST = cast<StoreSDNode>(N);
990 SDOperand Chain = ST->getChain();
991 SDOperand Ptr = ST->getBasePtr();
993 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
994 assert(NVT.isByteSized() && "Expanded type not byte sized!");
995 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
998 GetExpandedOp(ST->getValue(), Lo, Hi);
1000 return DAG.getTruncStore(Chain, Lo, Ptr,
1001 ST->getSrcValue(), ST->getSrcValueOffset(),
1003 ST->isVolatile(), ST->getAlignment());