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 switch (N->getOpcode()) {
54 cerr << "SoftenFloatResult #" << ResNo << ": ";
55 N->dump(&DAG); cerr << "\n";
57 assert(0 && "Do not know how to soften the result of this operator!");
60 case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
61 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
63 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
65 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
66 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
67 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
68 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
69 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
70 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
72 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
74 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
75 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
76 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
79 // If R is null, the sub-method took care of registering the result.
81 SetSoftenedFloat(SDOperand(N, ResNo), R);
84 SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
85 return BitConvertToInteger(N->getOperand(0));
88 SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
89 // Convert the inputs to integers, and build a new pair out of them.
90 return DAG.getNode(ISD::BUILD_PAIR,
91 TLI.getTypeToTransformTo(N->getValueType(0)),
92 BitConvertToInteger(N->getOperand(0)),
93 BitConvertToInteger(N->getOperand(1)));
96 SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
97 return DAG.getConstant(N->getValueAPF().convertToAPInt(),
98 TLI.getTypeToTransformTo(N->getValueType(0)));
101 SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
102 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
103 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
104 GetSoftenedFloat(N->getOperand(1)) };
105 return MakeLibCall(GetFPLibCall(N->getValueType(0),
113 SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
114 SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
115 SDOperand RHS = BitConvertToInteger(N->getOperand(1));
117 MVT LVT = LHS.getValueType();
118 MVT RVT = RHS.getValueType();
120 unsigned LSize = LVT.getSizeInBits();
121 unsigned RSize = RVT.getSizeInBits();
123 // First get the sign bit of second operand.
124 SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
125 DAG.getConstant(RSize - 1,
126 TLI.getShiftAmountTy()));
127 SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
129 // Shift right or sign-extend it if the two operands have different types.
130 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
132 SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
133 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
134 SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
135 } else if (SizeDiff < 0) {
136 SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
137 SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
138 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
141 // Clear the sign bit of the first operand.
142 SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
143 DAG.getConstant(LSize - 1,
144 TLI.getShiftAmountTy()));
145 Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
146 LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
148 // Or the value with the sign bit.
149 return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
152 SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
153 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
154 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
155 GetSoftenedFloat(N->getOperand(1)) };
156 return MakeLibCall(GetFPLibCall(N->getValueType(0),
164 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
165 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
166 SDOperand Op = N->getOperand(0);
168 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
169 switch (Op.getValueType().getSimpleVT()) {
171 assert(false && "Unsupported FP_EXTEND!");
173 switch (N->getValueType(0).getSimpleVT()) {
175 assert(false && "Unsupported FP_EXTEND!");
177 LC = RTLIB::FPEXT_F32_F64;
181 return MakeLibCall(LC, NVT, &Op, 1, false);
184 SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
185 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
186 SDOperand Op = N->getOperand(0);
188 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
189 switch (Op.getValueType().getSimpleVT()) {
191 assert(false && "Unsupported FP_ROUND!");
193 switch (N->getValueType(0).getSimpleVT()) {
195 assert(false && "Unsupported FP_ROUND!");
197 LC = RTLIB::FPROUND_F64_F32;
201 return MakeLibCall(LC, NVT, &Op, 1, false);
204 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
205 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
206 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
207 GetSoftenedFloat(N->getOperand(1)) };
208 return MakeLibCall(GetFPLibCall(N->getValueType(0),
216 SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
217 LoadSDNode *L = cast<LoadSDNode>(N);
218 MVT VT = N->getValueType(0);
219 MVT NVT = TLI.getTypeToTransformTo(VT);
221 if (L->getExtensionType() == ISD::NON_EXTLOAD)
222 return DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
223 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
224 L->getSrcValue(), L->getSrcValueOffset(), NVT,
225 L->isVolatile(), L->getAlignment());
227 // Do a non-extending load followed by FP_EXTEND.
228 SDOperand NL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
229 L->getMemoryVT(), L->getChain(),
230 L->getBasePtr(), L->getOffset(),
231 L->getSrcValue(), L->getSrcValueOffset(),
233 L->isVolatile(), L->getAlignment());
234 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NL));
237 SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
238 SDOperand LHS = GetSoftenedFloat(N->getOperand(1));
239 SDOperand RHS = GetSoftenedFloat(N->getOperand(2));
240 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
243 SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
244 SDOperand LHS = GetSoftenedFloat(N->getOperand(2));
245 SDOperand RHS = GetSoftenedFloat(N->getOperand(3));
246 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
247 N->getOperand(1), LHS, RHS, N->getOperand(4));
250 SDOperand DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
251 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
252 MVT DestVT = N->getValueType(0);
253 SDOperand Op = N->getOperand(0);
255 if (Op.getValueType() == MVT::i32) {
256 // simple 32-bit [signed|unsigned] integer to float/double expansion
258 // Get the stack frame index of a 8 byte buffer.
259 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
261 // word offset constant for Hi/Lo address computation
263 DAG.getConstant(MVT(MVT::i32).getSizeInBits() / 8,
265 // set up Hi and Lo (into buffer) address based on endian
266 SDOperand Hi = StackSlot;
267 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, Offset);
268 if (TLI.isLittleEndian())
271 // if signed map to unsigned space
274 // constant used to invert sign bit (signed to unsigned mapping)
275 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
276 OpMapped = DAG.getNode(ISD::XOR, MVT::i32, Op, SignBit);
280 // store the lo of the constructed double - based on integer input
281 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
282 OpMapped, Lo, NULL, 0);
283 // initial hi portion of constructed double
284 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
285 // store the hi of the constructed double - biased exponent
286 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
287 // load the constructed double
288 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
289 // FP constant to bias correct the final result
290 SDOperand Bias = DAG.getConstantFP(isSigned ?
291 BitsToDouble(0x4330000080000000ULL)
292 : BitsToDouble(0x4330000000000000ULL),
295 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
298 // handle final rounding
299 if (DestVT == MVT::f64) {
302 } else if (DestVT.bitsLT(MVT::f64)) {
303 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
304 DAG.getIntPtrConstant(0));
305 } else if (DestVT.bitsGT(MVT::f64)) {
306 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
308 return BitConvertToInteger(Result);
310 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
311 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op);
313 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op), Op,
314 DAG.getConstant(0, Op.getValueType()),
316 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
317 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
318 SignSet, Four, Zero);
320 // If the sign bit of the integer is set, the large number will be treated
321 // as a negative number. To counteract this, the dynamic code adds an
322 // offset depending on the data type.
324 switch (Op.getValueType().getSimpleVT()) {
325 default: assert(0 && "Unsupported integer type!");
326 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
327 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
328 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
329 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
331 if (TLI.isLittleEndian()) FF <<= 32;
332 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
334 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
335 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
336 SDOperand FudgeInReg;
337 if (DestVT == MVT::f32)
338 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
339 PseudoSourceValue::getConstantPool(), 0);
341 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestVT,
342 DAG.getEntryNode(), CPIdx,
343 PseudoSourceValue::getConstantPool(), 0,
347 return BitConvertToInteger(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
351 //===----------------------------------------------------------------------===//
352 // Operand Float to Integer Conversion..
353 //===----------------------------------------------------------------------===//
355 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
356 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
358 SDOperand Res = SDOperand();
360 switch (N->getOpcode()) {
363 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
364 N->dump(&DAG); cerr << "\n";
366 assert(0 && "Do not know how to soften this operator's operand!");
369 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
371 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
372 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
373 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
374 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
377 // If the result is null, the sub-method took care of registering results etc.
378 if (!Res.Val) return false;
380 // If the result is N, the sub-method updated N in place. Check to see if any
381 // operands are new, and if so, mark them.
383 // Mark N as new and remark N and its operands. This allows us to correctly
384 // revisit N if it needs another step of promotion and allows us to visit
385 // any new operands to N.
390 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
391 "Invalid operand expansion");
393 ReplaceValueWith(SDOperand(N, 0), Res);
397 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
398 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
399 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
400 ISD::CondCode &CCCode) {
401 SDOperand LHSInt = GetSoftenedFloat(NewLHS);
402 SDOperand RHSInt = GetSoftenedFloat(NewRHS);
403 MVT VT = NewLHS.getValueType();
405 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
407 // Expand into one or more soft-fp libcall(s).
408 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
412 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
416 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
420 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
424 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
428 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
432 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
435 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
438 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
441 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
444 // SETONE = SETOLT | SETOGT
445 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
448 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
451 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
454 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
457 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
460 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
462 default: assert(false && "Do not know how to soften this setcc!");
466 SDOperand Ops[2] = { LHSInt, RHSInt };
467 NewLHS = MakeLibCall(LC1, MVT::i32, Ops, 2, false/*sign irrelevant*/);
468 NewRHS = DAG.getConstant(0, MVT::i32);
469 CCCode = TLI.getCmpLibcallCC(LC1);
470 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
471 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
472 NewLHS, NewRHS, DAG.getCondCode(CCCode));
473 NewLHS = MakeLibCall(LC2, MVT::i32, Ops, 2, false/*sign irrelevant*/);
474 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
475 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
476 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
477 NewRHS = SDOperand();
481 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
482 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
483 GetSoftenedFloat(N->getOperand(0)));
486 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
487 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
488 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
489 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
491 // If SoftenSetCCOperands returned a scalar, we need to compare the result
492 // against zero to select between true and false values.
493 if (NewRHS.Val == 0) {
494 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
498 // Update N to have the operands specified.
499 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
500 DAG.getCondCode(CCCode), NewLHS, NewRHS,
504 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
505 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
506 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
507 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
509 // If SoftenSetCCOperands returned a scalar, we need to compare the result
510 // against zero to select between true and false values.
511 if (NewRHS.Val == 0) {
512 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
516 // Update N to have the operands specified.
517 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
518 N->getOperand(2), N->getOperand(3),
519 DAG.getCondCode(CCCode));
522 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
523 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
524 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
525 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
527 // If SoftenSetCCOperands returned a scalar, use it.
528 if (NewRHS.Val == 0) {
529 assert(NewLHS.getValueType() == N->getValueType(0) &&
530 "Unexpected setcc expansion!");
534 // Otherwise, update N to have the operands specified.
535 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
536 DAG.getCondCode(CCCode));
539 SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
540 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
541 assert(OpNo == 1 && "Can only soften the stored value!");
542 StoreSDNode *ST = cast<StoreSDNode>(N);
543 SDOperand Val = ST->getValue();
545 if (ST->isTruncatingStore())
546 // Do an FP_ROUND followed by a non-truncating store.
547 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
548 Val, DAG.getIntPtrConstant(0)));
550 Val = GetSoftenedFloat(Val);
552 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
553 ST->getSrcValue(), ST->getSrcValueOffset(),
554 ST->isVolatile(), ST->getAlignment());
558 //===----------------------------------------------------------------------===//
559 // Float Result Expansion
560 //===----------------------------------------------------------------------===//
562 /// ExpandFloatResult - This method is called when the specified result of the
563 /// specified node is found to need expansion. At this point, the node may also
564 /// have invalid operands or may have other results that need promotion, we just
565 /// know that (at least) one result needs expansion.
566 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
567 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
569 Lo = Hi = SDOperand();
571 // See if the target wants to custom expand this node.
572 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
573 TargetLowering::Custom) {
574 // If the target wants to, allow it to lower this itself.
575 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
576 // Everything that once used N now uses P. We are guaranteed that the
577 // result value types of N and the result value types of P match.
578 ReplaceNodeWith(N, P);
583 switch (N->getOpcode()) {
586 cerr << "ExpandFloatResult #" << ResNo << ": ";
587 N->dump(&DAG); cerr << "\n";
589 assert(0 && "Do not know how to expand the result of this operator!");
592 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
593 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
594 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
595 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
597 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
598 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
599 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
600 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
602 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
603 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
604 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
605 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
606 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
607 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
608 case ISD::SINT_TO_FP:
609 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
612 // If Lo/Hi is null, the sub-method took care of registering results etc.
614 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
617 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
619 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
620 assert(NVT.getSizeInBits() == integerPartWidth &&
621 "Do not know how to expand this float constant!");
622 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
623 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
624 &C.getRawData()[1])), NVT);
625 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
626 &C.getRawData()[0])), NVT);
629 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
631 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
632 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
637 N->getValueType(0), Ops, 2,
639 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
640 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
643 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
645 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
646 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
651 N->getValueType(0), Ops, 2,
653 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
654 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
657 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
659 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
660 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
665 N->getValueType(0), Ops, 2,
667 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
668 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
671 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
673 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
674 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
679 N->getValueType(0), Ops, 2,
681 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
682 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
685 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
687 if (ISD::isNormalLoad(N)) {
688 ExpandRes_NormalLoad(N, Lo, Hi);
692 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
693 LoadSDNode *LD = cast<LoadSDNode>(N);
694 SDOperand Chain = LD->getChain();
695 SDOperand Ptr = LD->getBasePtr();
697 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
698 assert(NVT.isByteSized() && "Expanded type not byte sized!");
699 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
701 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
702 LD->getSrcValue(), LD->getSrcValueOffset(),
704 LD->isVolatile(), LD->getAlignment());
706 // Remember the chain.
707 Chain = Lo.getValue(1);
709 // The high part is undefined.
710 Hi = DAG.getNode(ISD::UNDEF, NVT);
712 // Modified the chain - switch anything that used the old chain to use the
714 ReplaceValueWith(SDOperand(LD, 1), Chain);
717 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
719 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
720 MVT VT = N->getValueType(0);
721 MVT NVT = TLI.getTypeToTransformTo(VT);
722 SDOperand Src = N->getOperand(0);
723 MVT SrcVT = Src.getValueType();
725 // First do an SINT_TO_FP, whether the original was signed or unsigned.
726 if (SrcVT.bitsLE(MVT::i32)) {
727 // The integer can be represented exactly in an f64.
728 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
729 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
730 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
732 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
733 if (SrcVT.bitsLE(MVT::i64)) {
734 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
735 LC = RTLIB::SINTTOFP_I64_PPCF128;
736 } else if (SrcVT.bitsLE(MVT::i128)) {
737 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
738 LC = RTLIB::SINTTOFP_I128_PPCF128;
740 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
742 Hi = MakeLibCall(LC, VT, &Src, 1, true);
743 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
744 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
747 if (N->getOpcode() == ISD::SINT_TO_FP)
750 // Unsigned - fix up the SINT_TO_FP value just calculated.
751 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
752 SrcVT = Src.getValueType();
754 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
755 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
756 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
757 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
758 const uint64_t *Parts = 0;
760 switch (SrcVT.getSimpleVT()) {
762 assert(false && "Unsupported UINT_TO_FP!");
771 Lo = DAG.getNode(ISD::FADD, VT, Hi,
772 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
774 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
775 DAG.getCondCode(ISD::SETLT));
776 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
777 DAG.getConstant(1, TLI.getPointerTy()));
778 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
779 DAG.getConstant(0, TLI.getPointerTy()));
783 //===----------------------------------------------------------------------===//
784 // Float Operand Expansion
785 //===----------------------------------------------------------------------===//
787 /// ExpandFloatOperand - This method is called when the specified operand of the
788 /// specified node is found to need expansion. At this point, all of the result
789 /// types of the node are known to be legal, but other operands of the node may
790 /// need promotion or expansion as well as the specified one.
791 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
792 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
793 SDOperand Res = SDOperand();
795 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
796 == TargetLowering::Custom)
797 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
800 switch (N->getOpcode()) {
803 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
804 N->dump(&DAG); cerr << "\n";
806 assert(0 && "Do not know how to expand this operator's operand!");
809 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
810 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
811 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
813 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
814 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
815 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
817 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
818 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
819 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
822 Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
827 // If the result is null, the sub-method took care of registering results etc.
828 if (!Res.Val) return false;
829 // If the result is N, the sub-method updated N in place. Check to see if any
830 // operands are new, and if so, mark them.
832 // Mark N as new and remark N and its operands. This allows us to correctly
833 // revisit N if it needs another step of expansion and allows us to visit
834 // any new operands to N.
839 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
840 "Invalid operand expansion");
842 ReplaceValueWith(SDOperand(N, 0), Res);
846 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
847 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
848 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
850 ISD::CondCode &CCCode) {
851 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
852 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
853 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
855 MVT VT = NewLHS.getValueType();
856 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
858 // FIXME: This generated code sucks. We want to generate
859 // FCMP crN, hi1, hi2
861 // FCMP crN, lo1, lo2
862 // The following can be improved, but not that much.
863 SDOperand Tmp1, Tmp2, Tmp3;
864 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
865 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
866 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
867 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
868 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
869 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
870 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
871 NewRHS = SDOperand(); // LHS is the result, not a compare.
874 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
875 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
876 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
877 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
879 // If ExpandSetCCOperands returned a scalar, we need to compare the result
880 // against zero to select between true and false values.
881 if (NewRHS.Val == 0) {
882 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
886 // Update N to have the operands specified.
887 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
888 DAG.getCondCode(CCCode), NewLHS, NewRHS,
892 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
893 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
894 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
895 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
897 // If ExpandSetCCOperands returned a scalar, we need to compare the result
898 // against zero to select between true and false values.
899 if (NewRHS.Val == 0) {
900 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
904 // Update N to have the operands specified.
905 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
906 N->getOperand(2), N->getOperand(3),
907 DAG.getCondCode(CCCode));
910 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
911 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
912 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
913 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
915 // If ExpandSetCCOperands returned a scalar, use it.
916 if (NewRHS.Val == 0) {
917 assert(NewLHS.getValueType() == N->getValueType(0) &&
918 "Unexpected setcc expansion!");
922 // Otherwise, update N to have the operands specified.
923 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
924 DAG.getCondCode(CCCode));
927 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
928 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
929 "Unsupported FP_TO_UINT!");
931 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
932 switch (N->getValueType(0).getSimpleVT()) {
934 assert(false && "Unsupported FP_TO_UINT!");
936 LC = RTLIB::FPTOUINT_PPCF128_I32;
939 LC = RTLIB::FPTOUINT_PPCF128_I64;
942 LC = RTLIB::FPTOUINT_PPCF128_I128;
946 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
949 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
950 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
951 "Unsupported FP_TO_SINT!");
953 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
954 switch (N->getValueType(0).getSimpleVT()) {
956 assert(false && "Unsupported FP_TO_SINT!");
958 LC = RTLIB::FPTOSINT_PPCF128_I32;
960 LC = RTLIB::FPTOSINT_PPCF128_I64;
963 LC = RTLIB::FPTOSINT_PPCF128_I64;
967 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
970 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
971 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
972 "Logic only correct for ppcf128!");
974 GetExpandedFloat(N->getOperand(0), Lo, Hi);
975 // Round it the rest of the way (e.g. to f32) if needed.
976 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
979 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
980 if (ISD::isNormalStore(N))
981 return ExpandOp_NormalStore(N, OpNo);
983 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
984 assert(OpNo == 1 && "Can only expand the stored value so far");
985 StoreSDNode *ST = cast<StoreSDNode>(N);
987 SDOperand Chain = ST->getChain();
988 SDOperand Ptr = ST->getBasePtr();
990 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
991 assert(NVT.isByteSized() && "Expanded type not byte sized!");
992 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
995 GetExpandedOp(ST->getValue(), Lo, Hi);
997 return DAG.getTruncStore(Chain, Lo, Ptr,
998 ST->getSrcValue(), ST->getSrcValueOffset(),
1000 ST->isVolatile(), ST->getAlignment());