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;
442 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
445 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
448 // SETONE = SETOLT | SETOGT
449 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
452 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
455 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
458 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
461 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
464 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
466 default: assert(false && "Do not know how to soften this setcc!");
470 SDOperand Ops[2] = { LHSInt, RHSInt };
471 NewLHS = MakeLibCall(LC1, NVT, Ops, 2, false/*sign irrelevant*/);
472 NewRHS = DAG.getConstant(0, NVT);
473 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
474 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
476 DAG.getCondCode(TLI.getCmpLibcallCC(LC1)));
477 NewLHS = MakeLibCall(LC2, NVT, Ops, 2, false/*sign irrelevant*/);
478 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
479 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
480 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
481 NewRHS = SDOperand();
485 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
486 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
487 GetSoftenedFloat(N->getOperand(0)));
490 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
491 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
492 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
493 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
495 // If SoftenSetCCOperands returned a scalar, we need to compare the result
496 // against zero to select between true and false values.
497 if (NewRHS.Val == 0) {
498 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
502 // Update N to have the operands specified.
503 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
504 DAG.getCondCode(CCCode), NewLHS, NewRHS,
508 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
509 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
510 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
511 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
513 // If SoftenSetCCOperands returned a scalar, we need to compare the result
514 // against zero to select between true and false values.
515 if (NewRHS.Val == 0) {
516 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
520 // Update N to have the operands specified.
521 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
522 N->getOperand(2), N->getOperand(3),
523 DAG.getCondCode(CCCode));
526 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
527 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
528 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
529 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
531 // If SoftenSetCCOperands returned a scalar, use it.
532 if (NewRHS.Val == 0) {
533 assert(NewLHS.getValueType() == N->getValueType(0) &&
534 "Unexpected setcc expansion!");
538 // Otherwise, update N to have the operands specified.
539 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
540 DAG.getCondCode(CCCode));
543 SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
544 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
545 assert(OpNo == 1 && "Can only soften the stored value!");
546 StoreSDNode *ST = cast<StoreSDNode>(N);
547 SDOperand Val = ST->getValue();
549 if (ST->isTruncatingStore())
550 // Do an FP_ROUND followed by a non-truncating store.
551 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
552 Val, DAG.getIntPtrConstant(0)));
554 Val = GetSoftenedFloat(Val);
556 return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
557 ST->getSrcValue(), ST->getSrcValueOffset(),
558 ST->isVolatile(), ST->getAlignment());
562 //===----------------------------------------------------------------------===//
563 // Float Result Expansion
564 //===----------------------------------------------------------------------===//
566 /// ExpandFloatResult - This method is called when the specified result of the
567 /// specified node is found to need expansion. At this point, the node may also
568 /// have invalid operands or may have other results that need promotion, we just
569 /// know that (at least) one result needs expansion.
570 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
571 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
573 Lo = Hi = SDOperand();
575 // See if the target wants to custom expand this node.
576 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
577 TargetLowering::Custom) {
578 // If the target wants to, allow it to lower this itself.
579 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
580 // Everything that once used N now uses P. We are guaranteed that the
581 // result value types of N and the result value types of P match.
582 ReplaceNodeWith(N, P);
587 switch (N->getOpcode()) {
590 cerr << "ExpandFloatResult #" << ResNo << ": ";
591 N->dump(&DAG); cerr << "\n";
593 assert(0 && "Do not know how to expand the result of this operator!");
596 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
597 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
598 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
599 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
601 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
602 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
603 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
604 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
606 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
607 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
608 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
609 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
610 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
611 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
612 case ISD::SINT_TO_FP:
613 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
616 // If Lo/Hi is null, the sub-method took care of registering results etc.
618 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
621 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
623 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
624 assert(NVT.getSizeInBits() == integerPartWidth &&
625 "Do not know how to expand this float constant!");
626 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
627 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
628 &C.getRawData()[1])), NVT);
629 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
630 &C.getRawData()[0])), NVT);
633 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
635 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
636 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
641 N->getValueType(0), Ops, 2,
643 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
644 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
647 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
649 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
650 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
655 N->getValueType(0), Ops, 2,
657 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
658 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
661 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
663 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
664 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
669 N->getValueType(0), Ops, 2,
671 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
672 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
675 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
677 SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
678 SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
683 N->getValueType(0), Ops, 2,
685 assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
686 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
689 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
691 if (ISD::isNormalLoad(N)) {
692 ExpandRes_NormalLoad(N, Lo, Hi);
696 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
697 LoadSDNode *LD = cast<LoadSDNode>(N);
698 SDOperand Chain = LD->getChain();
699 SDOperand Ptr = LD->getBasePtr();
701 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
702 assert(NVT.isByteSized() && "Expanded type not byte sized!");
703 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
705 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
706 LD->getSrcValue(), LD->getSrcValueOffset(),
708 LD->isVolatile(), LD->getAlignment());
710 // Remember the chain.
711 Chain = Lo.getValue(1);
713 // The high part is undefined.
714 Hi = DAG.getNode(ISD::UNDEF, NVT);
716 // Modified the chain - switch anything that used the old chain to use the
718 ReplaceValueWith(SDOperand(LD, 1), Chain);
721 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
723 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
724 MVT VT = N->getValueType(0);
725 MVT NVT = TLI.getTypeToTransformTo(VT);
726 SDOperand Src = N->getOperand(0);
727 MVT SrcVT = Src.getValueType();
729 // First do an SINT_TO_FP, whether the original was signed or unsigned.
730 if (SrcVT.bitsLE(MVT::i32)) {
731 // The integer can be represented exactly in an f64.
732 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
733 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
734 Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
736 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
737 if (SrcVT.bitsLE(MVT::i64)) {
738 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
739 LC = RTLIB::SINTTOFP_I64_PPCF128;
740 } else if (SrcVT.bitsLE(MVT::i128)) {
741 Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
742 LC = RTLIB::SINTTOFP_I128_PPCF128;
744 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
746 Hi = MakeLibCall(LC, VT, &Src, 1, true);
747 assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
748 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
751 if (N->getOpcode() == ISD::SINT_TO_FP)
754 // Unsigned - fix up the SINT_TO_FP value just calculated.
755 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
756 SrcVT = Src.getValueType();
758 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
759 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
760 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
761 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
762 const uint64_t *Parts = 0;
764 switch (SrcVT.getSimpleVT()) {
766 assert(false && "Unsupported UINT_TO_FP!");
775 Lo = DAG.getNode(ISD::FADD, VT, Hi,
776 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
778 Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
779 DAG.getCondCode(ISD::SETLT));
780 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
781 DAG.getConstant(1, TLI.getPointerTy()));
782 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
783 DAG.getConstant(0, TLI.getPointerTy()));
787 //===----------------------------------------------------------------------===//
788 // Float Operand Expansion
789 //===----------------------------------------------------------------------===//
791 /// ExpandFloatOperand - This method is called when the specified operand of the
792 /// specified node is found to need expansion. At this point, all of the result
793 /// types of the node are known to be legal, but other operands of the node may
794 /// need promotion or expansion as well as the specified one.
795 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
796 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
799 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
800 == TargetLowering::Custom)
801 Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
804 switch (N->getOpcode()) {
807 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
808 N->dump(&DAG); cerr << "\n";
810 assert(0 && "Do not know how to expand this operator's operand!");
813 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
814 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
815 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
817 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
818 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
819 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
821 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
822 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
823 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
826 Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
831 // If the result is null, the sub-method took care of registering results etc.
832 if (!Res.Val) return false;
833 // If the result is N, the sub-method updated N in place. Check to see if any
834 // operands are new, and if so, mark them.
836 // Mark N as new and remark N and its operands. This allows us to correctly
837 // revisit N if it needs another step of expansion and allows us to visit
838 // any new operands to N.
843 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
844 "Invalid operand expansion");
846 ReplaceValueWith(SDOperand(N, 0), Res);
850 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
851 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
852 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
854 ISD::CondCode &CCCode) {
855 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
856 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
857 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
859 MVT VT = NewLHS.getValueType();
860 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
862 // FIXME: This generated code sucks. We want to generate
863 // FCMP crN, hi1, hi2
865 // FCMP crN, lo1, lo2
866 // The following can be improved, but not that much.
867 SDOperand Tmp1, Tmp2, Tmp3;
868 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
869 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
870 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
871 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
872 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
873 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
874 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
875 NewRHS = SDOperand(); // LHS is the result, not a compare.
878 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
879 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
880 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
881 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
883 // If ExpandSetCCOperands returned a scalar, we need to compare the result
884 // against zero to select between true and false values.
885 if (NewRHS.Val == 0) {
886 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
890 // Update N to have the operands specified.
891 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
892 DAG.getCondCode(CCCode), NewLHS, NewRHS,
896 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
897 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
898 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
899 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
901 // If ExpandSetCCOperands returned a scalar, we need to compare the result
902 // against zero to select between true and false values.
903 if (NewRHS.Val == 0) {
904 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
908 // Update N to have the operands specified.
909 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
910 N->getOperand(2), N->getOperand(3),
911 DAG.getCondCode(CCCode));
914 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
915 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
916 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
917 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
919 // If ExpandSetCCOperands returned a scalar, use it.
920 if (NewRHS.Val == 0) {
921 assert(NewLHS.getValueType() == N->getValueType(0) &&
922 "Unexpected setcc expansion!");
926 // Otherwise, update N to have the operands specified.
927 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
928 DAG.getCondCode(CCCode));
931 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
932 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
933 "Unsupported FP_TO_UINT!");
935 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
936 switch (N->getValueType(0).getSimpleVT()) {
938 assert(false && "Unsupported FP_TO_UINT!");
940 LC = RTLIB::FPTOUINT_PPCF128_I32;
943 LC = RTLIB::FPTOUINT_PPCF128_I64;
946 LC = RTLIB::FPTOUINT_PPCF128_I128;
950 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
953 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
954 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
955 "Unsupported FP_TO_SINT!");
957 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
958 switch (N->getValueType(0).getSimpleVT()) {
960 assert(false && "Unsupported FP_TO_SINT!");
962 LC = RTLIB::FPTOSINT_PPCF128_I32;
964 LC = RTLIB::FPTOSINT_PPCF128_I64;
967 LC = RTLIB::FPTOSINT_PPCF128_I64;
971 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
974 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
975 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
976 "Logic only correct for ppcf128!");
978 GetExpandedFloat(N->getOperand(0), Lo, Hi);
979 // Round it the rest of the way (e.g. to f32) if needed.
980 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
983 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
984 if (ISD::isNormalStore(N))
985 return ExpandOp_NormalStore(N, OpNo);
987 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
988 assert(OpNo == 1 && "Can only expand the stored value so far");
989 StoreSDNode *ST = cast<StoreSDNode>(N);
991 SDOperand Chain = ST->getChain();
992 SDOperand Ptr = ST->getBasePtr();
994 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
995 assert(NVT.isByteSized() && "Expanded type not byte sized!");
996 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
999 GetExpandedOp(ST->getValue(), Lo, Hi);
1001 return DAG.getTruncStore(Chain, Lo, Ptr,
1002 ST->getSrcValue(), ST->getSrcValueOffset(),
1004 ST->isVolatile(), ST->getAlignment());