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 // FIXME: Custom lowering for float-to-int?
53 // See if the target wants to custom convert this node to an integer.
54 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) ==
55 TargetLowering::Custom) {
56 // If the target wants to, allow it to lower this itself.
57 if (SDNode *P = TLI.FloatToIntOperationResult(N, DAG)) {
58 // Everything that once used N now uses P. We are guaranteed that the
59 // result value types of N and the result value types of P match.
60 ReplaceNodeWith(N, P);
66 switch (N->getOpcode()) {
69 cerr << "SoftenFloatResult #" << ResNo << ": ";
70 N->dump(&DAG); cerr << "\n";
72 assert(0 && "Do not know how to convert the result of this operator!");
75 case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
76 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
78 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
80 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
81 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
83 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
85 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
86 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
87 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
90 // If R is null, the sub-method took care of registering the result.
92 SetSoftenedFloat(SDOperand(N, ResNo), R);
95 SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
96 return BitConvertToInteger(N->getOperand(0));
99 SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
100 // Convert the inputs to integers, and build a new pair out of them.
101 return DAG.getNode(ISD::BUILD_PAIR,
102 TLI.getTypeToTransformTo(N->getValueType(0)),
103 BitConvertToInteger(N->getOperand(0)),
104 BitConvertToInteger(N->getOperand(1)));
107 SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
108 return DAG.getConstant(N->getValueAPF().convertToAPInt(),
109 TLI.getTypeToTransformTo(N->getValueType(0)));
112 SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
113 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
114 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
115 GetSoftenedFloat(N->getOperand(1)) };
116 return MakeLibCall(GetFPLibCall(N->getValueType(0),
121 NVT, Ops, 2, false/*sign irrelevant*/);
124 SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
125 SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
126 SDOperand RHS = BitConvertToInteger(N->getOperand(1));
128 MVT LVT = LHS.getValueType();
129 MVT RVT = RHS.getValueType();
131 unsigned LSize = LVT.getSizeInBits();
132 unsigned RSize = RVT.getSizeInBits();
134 // First get the sign bit of second operand.
135 SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
136 DAG.getConstant(RSize - 1,
137 TLI.getShiftAmountTy()));
138 SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
140 // Shift right or sign-extend it if the two operands have different types.
141 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
143 SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
144 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
145 SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
146 } else if (SizeDiff < 0) {
147 SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
148 SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
149 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
152 // Clear the sign bit of the first operand.
153 SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
154 DAG.getConstant(LSize - 1,
155 TLI.getShiftAmountTy()));
156 Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
157 LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
159 // Or the value with the sign bit.
160 return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
163 SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
164 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
165 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
166 GetSoftenedFloat(N->getOperand(1)) };
167 return MakeLibCall(GetFPLibCall(N->getValueType(0),
172 NVT, Ops, 2, false/*sign irrelevant*/);
175 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
176 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
177 SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
178 GetSoftenedFloat(N->getOperand(1)) };
179 return MakeLibCall(GetFPLibCall(N->getValueType(0),
184 NVT, Ops, 2, false/*sign irrelevant*/);
187 SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
188 LoadSDNode *L = cast<LoadSDNode>(N);
189 MVT VT = N->getValueType(0);
190 MVT NVT = TLI.getTypeToTransformTo(VT);
192 if (L->getExtensionType() == ISD::NON_EXTLOAD)
193 return DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
194 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
195 L->getSrcValue(), L->getSrcValueOffset(), NVT,
196 L->isVolatile(), L->getAlignment());
198 // Do a non-extending load followed by FP_EXTEND.
199 SDOperand NL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
200 L->getMemoryVT(), L->getChain(),
201 L->getBasePtr(), L->getOffset(),
202 L->getSrcValue(), L->getSrcValueOffset(),
204 L->isVolatile(), L->getAlignment());
205 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NL));
208 SDOperand DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
209 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
210 MVT DestVT = N->getValueType(0);
211 SDOperand Op = N->getOperand(0);
213 if (Op.getValueType() == MVT::i32) {
214 // simple 32-bit [signed|unsigned] integer to float/double expansion
216 // Get the stack frame index of a 8 byte buffer.
217 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
219 // word offset constant for Hi/Lo address computation
221 DAG.getConstant(MVT(MVT::i32).getSizeInBits() / 8,
223 // set up Hi and Lo (into buffer) address based on endian
224 SDOperand Hi = StackSlot;
225 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, Offset);
226 if (TLI.isLittleEndian())
229 // if signed map to unsigned space
232 // constant used to invert sign bit (signed to unsigned mapping)
233 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
234 OpMapped = DAG.getNode(ISD::XOR, MVT::i32, Op, SignBit);
238 // store the lo of the constructed double - based on integer input
239 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
240 OpMapped, Lo, NULL, 0);
241 // initial hi portion of constructed double
242 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
243 // store the hi of the constructed double - biased exponent
244 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
245 // load the constructed double
246 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
247 // FP constant to bias correct the final result
248 SDOperand Bias = DAG.getConstantFP(isSigned ?
249 BitsToDouble(0x4330000080000000ULL)
250 : BitsToDouble(0x4330000000000000ULL),
253 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
256 // handle final rounding
257 if (DestVT == MVT::f64) {
260 } else if (DestVT.bitsLT(MVT::f64)) {
261 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
262 DAG.getIntPtrConstant(0));
263 } else if (DestVT.bitsGT(MVT::f64)) {
264 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
266 return BitConvertToInteger(Result);
268 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
269 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op);
271 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op), Op,
272 DAG.getConstant(0, Op.getValueType()),
274 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
275 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
276 SignSet, Four, Zero);
278 // If the sign bit of the integer is set, the large number will be treated
279 // as a negative number. To counteract this, the dynamic code adds an
280 // offset depending on the data type.
282 switch (Op.getValueType().getSimpleVT()) {
283 default: assert(0 && "Unsupported integer type!");
284 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
285 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
286 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
287 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
289 if (TLI.isLittleEndian()) FF <<= 32;
290 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
292 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
293 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
294 SDOperand FudgeInReg;
295 if (DestVT == MVT::f32)
296 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
297 PseudoSourceValue::getConstantPool(), 0);
299 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestVT,
300 DAG.getEntryNode(), CPIdx,
301 PseudoSourceValue::getConstantPool(), 0,
305 return BitConvertToInteger(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
309 //===----------------------------------------------------------------------===//
310 // Operand Float to Integer Conversion..
311 //===----------------------------------------------------------------------===//
313 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
314 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
318 // FIXME: Custom lowering for float-to-int?
320 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
321 == TargetLowering::Custom)
322 Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
326 switch (N->getOpcode()) {
329 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
330 N->dump(&DAG); cerr << "\n";
332 assert(0 && "Do not know how to convert this operator's operand!");
335 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
337 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
338 case ISD::SELECT_CC: Res = SoftenFloatOp_BR_CC(N); break;
339 case ISD::SETCC: Res = SoftenFloatOp_BR_CC(N); break;
343 // If the result is null, the sub-method took care of registering results etc.
344 if (!Res.Val) return false;
346 // If the result is N, the sub-method updated N in place. Check to see if any
347 // operands are new, and if so, mark them.
349 // Mark N as new and remark N and its operands. This allows us to correctly
350 // revisit N if it needs another step of promotion and allows us to visit
351 // any new operands to N.
356 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
357 "Invalid operand expansion");
359 ReplaceValueWith(SDOperand(N, 0), Res);
363 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
364 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
365 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
366 ISD::CondCode &CCCode) {
367 SDOperand LHSInt = GetSoftenedFloat(NewLHS);
368 SDOperand RHSInt = GetSoftenedFloat(NewRHS);
369 MVT VT = NewLHS.getValueType();
370 MVT NVT = LHSInt.getValueType();
372 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
374 // Expand into one or more soft-fp libcall(s).
375 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
379 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
383 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
387 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
391 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
395 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
399 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
402 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
407 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
410 // SETONE = SETOLT | SETOGT
411 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
414 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
417 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
420 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
423 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
426 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
428 default: assert(false && "Do not know how to soften this setcc!");
432 SDOperand Ops[2] = { LHSInt, RHSInt };
433 NewLHS = MakeLibCall(LC1, NVT, Ops, 2, false/*sign irrelevant*/);
434 NewRHS = DAG.getConstant(0, NVT);
435 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
436 SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
438 DAG.getCondCode(TLI.getCmpLibcallCC(LC1)));
439 NewLHS = MakeLibCall(LC2, NVT, Ops, 2, false/*sign irrelevant*/);
440 NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
441 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
442 NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
443 NewRHS = SDOperand();
447 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
448 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
449 GetSoftenedFloat(N->getOperand(0)));
452 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
453 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
454 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
455 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
457 // If SoftenSetCCOperands returned a scalar, we need to compare the result
458 // against zero to select between true and false values.
459 if (NewRHS.Val == 0) {
460 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
464 // Update N to have the operands specified.
465 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
466 DAG.getCondCode(CCCode), NewLHS, NewRHS,
470 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
471 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
472 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
473 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
475 // If SoftenSetCCOperands returned a scalar, we need to compare the result
476 // against zero to select between true and false values.
477 if (NewRHS.Val == 0) {
478 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
482 // Update N to have the operands specified.
483 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
484 N->getOperand(2), N->getOperand(3),
485 DAG.getCondCode(CCCode));
488 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
489 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
490 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
491 SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
493 // If SoftenSetCCOperands returned a scalar, use it.
494 if (NewRHS.Val == 0) {
495 assert(NewLHS.getValueType() == N->getValueType(0) &&
496 "Unexpected setcc expansion!");
500 // Otherwise, update N to have the operands specified.
501 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
502 DAG.getCondCode(CCCode));
506 //===----------------------------------------------------------------------===//
507 // Float Result Expansion
508 //===----------------------------------------------------------------------===//
510 /// ExpandFloatResult - This method is called when the specified result of the
511 /// specified node is found to need expansion. At this point, the node may also
512 /// have invalid operands or may have other results that need promotion, we just
513 /// know that (at least) one result needs expansion.
514 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
515 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
517 Lo = Hi = SDOperand();
519 // See if the target wants to custom expand this node.
520 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) ==
521 TargetLowering::Custom) {
522 // If the target wants to, allow it to lower this itself.
523 if (SDNode *P = TLI.ExpandOperationResult(N, DAG)) {
524 // Everything that once used N now uses P. We are guaranteed that the
525 // result value types of N and the result value types of P match.
526 ReplaceNodeWith(N, P);
531 switch (N->getOpcode()) {
534 cerr << "ExpandFloatResult #" << ResNo << ": ";
535 N->dump(&DAG); cerr << "\n";
537 assert(0 && "Do not know how to expand the result of this operator!");
540 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
541 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
542 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
543 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
545 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
546 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
547 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
548 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
550 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
553 // If Lo/Hi is null, the sub-method took care of registering results etc.
555 SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
558 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
560 if (ISD::isNormalLoad(N)) {
561 ExpandRes_NormalLoad(N, Lo, Hi);
565 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
566 LoadSDNode *LD = cast<LoadSDNode>(N);
567 SDOperand Chain = LD->getChain();
568 SDOperand Ptr = LD->getBasePtr();
570 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
571 assert(NVT.isByteSized() && "Expanded type not byte sized!");
572 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
574 Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
575 LD->getSrcValue(), LD->getSrcValueOffset(),
577 LD->isVolatile(), LD->getAlignment());
579 // Remember the chain.
580 Chain = Lo.getValue(1);
582 // The high part is undefined.
583 Hi = DAG.getNode(ISD::UNDEF, NVT);
585 // Modified the chain - switch anything that used the old chain to use the
587 ReplaceValueWith(SDOperand(LD, 1), Chain);
591 //===----------------------------------------------------------------------===//
592 // Float Operand Expansion
593 //===----------------------------------------------------------------------===//
595 /// ExpandFloatOperand - This method is called when the specified operand of the
596 /// specified node is found to need expansion. At this point, all of the result
597 /// types of the node are known to be legal, but other operands of the node may
598 /// need promotion or expansion as well as the specified one.
599 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
600 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
603 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
604 == TargetLowering::Custom)
605 Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
608 switch (N->getOpcode()) {
611 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
612 N->dump(&DAG); cerr << "\n";
614 assert(0 && "Do not know how to expand this operator's operand!");
617 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
618 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
619 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
621 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
622 case ISD::SELECT_CC: Res = ExpandFloatOp_BR_CC(N); break;
623 case ISD::SETCC: Res = ExpandFloatOp_BR_CC(N); break;
626 Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
631 // If the result is null, the sub-method took care of registering results etc.
632 if (!Res.Val) return false;
633 // If the result is N, the sub-method updated N in place. Check to see if any
634 // operands are new, and if so, mark them.
636 // Mark N as new and remark N and its operands. This allows us to correctly
637 // revisit N if it needs another step of expansion and allows us to visit
638 // any new operands to N.
643 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
644 "Invalid operand expansion");
646 ReplaceValueWith(SDOperand(N, 0), Res);
650 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
651 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
652 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
654 ISD::CondCode &CCCode) {
655 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
656 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
657 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
659 MVT VT = NewLHS.getValueType();
660 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
662 // FIXME: This generated code sucks. We want to generate
663 // FCMP crN, hi1, hi2
665 // FCMP crN, lo1, lo2
666 // The following can be improved, but not that much.
667 SDOperand Tmp1, Tmp2, Tmp3;
668 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
669 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
670 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
671 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
672 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
673 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
674 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
675 NewRHS = SDOperand(); // LHS is the result, not a compare.
678 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
679 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
680 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
681 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
683 // If ExpandSetCCOperands returned a scalar, we need to compare the result
684 // against zero to select between true and false values.
685 if (NewRHS.Val == 0) {
686 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
690 // Update N to have the operands specified.
691 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
692 DAG.getCondCode(CCCode), NewLHS, NewRHS,
696 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
697 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
698 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
699 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
701 // If ExpandSetCCOperands returned a scalar, we need to compare the result
702 // against zero to select between true and false values.
703 if (NewRHS.Val == 0) {
704 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
708 // Update N to have the operands specified.
709 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
710 N->getOperand(2), N->getOperand(3),
711 DAG.getCondCode(CCCode));
714 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
715 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
716 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
717 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
719 // If ExpandSetCCOperands returned a scalar, use it.
720 if (NewRHS.Val == 0) {
721 assert(NewLHS.getValueType() == N->getValueType(0) &&
722 "Unexpected setcc expansion!");
726 // Otherwise, update N to have the operands specified.
727 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
728 DAG.getCondCode(CCCode));
731 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
732 if (ISD::isNormalStore(N))
733 return ExpandOp_NormalStore(N, OpNo);
735 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
736 assert(OpNo == 1 && "Can only expand the stored value so far");
737 StoreSDNode *ST = cast<StoreSDNode>(N);
739 SDOperand Chain = ST->getChain();
740 SDOperand Ptr = ST->getBasePtr();
742 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
743 assert(NVT.isByteSized() && "Expanded type not byte sized!");
744 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
747 GetExpandedOp(ST->getValue(), Lo, Hi);
749 return DAG.getTruncStore(Chain, Lo, Ptr,
750 ST->getSrcValue(), ST->getSrcValueOffset(),
752 ST->isVolatile(), ST->getAlignment());