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/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
27 /// GetFPLibCall - Return the right libcall for the given floating point type.
28 static RTLIB::Libcall GetFPLibCall(EVT VT,
29 RTLIB::Libcall Call_F32,
30 RTLIB::Libcall Call_F64,
31 RTLIB::Libcall Call_F80,
32 RTLIB::Libcall Call_F128,
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::f128 ? Call_F128 :
39 VT == MVT::ppcf128 ? Call_PPCF128 :
40 RTLIB::UNKNOWN_LIBCALL;
43 //===----------------------------------------------------------------------===//
44 // Result Float to Integer Conversion.
45 //===----------------------------------------------------------------------===//
47 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
48 DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
50 SDValue R = SDValue();
52 switch (N->getOpcode()) {
55 dbgs() << "SoftenFloatResult #" << ResNo << ": ";
56 N->dump(&DAG); dbgs() << "\n";
58 llvm_unreachable("Do not know how to soften the result of this operator!");
60 case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
61 case ISD::BITCAST: R = SoftenFloatRes_BITCAST(N); break;
62 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
64 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
66 case ISD::EXTRACT_VECTOR_ELT:
67 R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
68 case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
69 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
70 case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
71 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
72 case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
73 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
74 case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
75 case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
76 case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
77 case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
78 case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
79 case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
80 case ISD::FMA: R = SoftenFloatRes_FMA(N); break;
81 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
82 case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
83 case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
84 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
85 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
86 case ISD::FP16_TO_FP32:R = SoftenFloatRes_FP16_TO_FP32(N); break;
87 case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
88 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
89 case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
90 case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
91 case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
92 case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
93 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
94 case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
95 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
96 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
97 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
99 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
100 case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
101 case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
104 // If R is null, the sub-method took care of registering the result.
106 SetSoftenedFloat(SDValue(N, ResNo), R);
109 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
110 return BitConvertToInteger(N->getOperand(0));
113 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
115 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
116 return BitConvertToInteger(Op);
119 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
120 // Convert the inputs to integers, and build a new pair out of them.
121 return DAG.getNode(ISD::BUILD_PAIR, N->getDebugLoc(),
122 TLI.getTypeToTransformTo(*DAG.getContext(),
124 BitConvertToInteger(N->getOperand(0)),
125 BitConvertToInteger(N->getOperand(1)));
128 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
129 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
130 TLI.getTypeToTransformTo(*DAG.getContext(),
131 N->getValueType(0)));
134 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
135 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
136 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(),
137 NewOp.getValueType().getVectorElementType(),
138 NewOp, N->getOperand(1));
141 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
142 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
143 unsigned Size = NVT.getSizeInBits();
145 // Mask = ~(1 << (Size-1))
146 APInt API = APInt::getAllOnesValue(Size);
147 API.clearBit(Size-1);
148 SDValue Mask = DAG.getConstant(API, NVT);
149 SDValue Op = GetSoftenedFloat(N->getOperand(0));
150 return DAG.getNode(ISD::AND, N->getDebugLoc(), NVT, Op, Mask);
153 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
154 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
155 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
156 GetSoftenedFloat(N->getOperand(1)) };
157 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
163 NVT, Ops, 2, false, N->getDebugLoc());
166 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
167 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
168 SDValue Op = GetSoftenedFloat(N->getOperand(0));
169 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
174 RTLIB::CEIL_PPCF128),
175 NVT, &Op, 1, false, N->getDebugLoc());
178 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
179 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
180 SDValue RHS = BitConvertToInteger(N->getOperand(1));
181 DebugLoc dl = N->getDebugLoc();
183 EVT LVT = LHS.getValueType();
184 EVT RVT = RHS.getValueType();
186 unsigned LSize = LVT.getSizeInBits();
187 unsigned RSize = RVT.getSizeInBits();
189 // First get the sign bit of second operand.
190 SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
191 DAG.getConstant(RSize - 1,
192 TLI.getShiftAmountTy(RVT)));
193 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
195 // Shift right or sign-extend it if the two operands have different types.
196 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
198 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
199 DAG.getConstant(SizeDiff,
200 TLI.getShiftAmountTy(SignBit.getValueType())));
201 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
202 } else if (SizeDiff < 0) {
203 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
204 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
205 DAG.getConstant(-SizeDiff,
206 TLI.getShiftAmountTy(SignBit.getValueType())));
209 // Clear the sign bit of the first operand.
210 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
211 DAG.getConstant(LSize - 1,
212 TLI.getShiftAmountTy(LVT)));
213 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
214 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
216 // Or the value with the sign bit.
217 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
220 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
221 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
222 SDValue Op = GetSoftenedFloat(N->getOperand(0));
223 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
229 NVT, &Op, 1, false, N->getDebugLoc());
232 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
233 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
234 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
235 GetSoftenedFloat(N->getOperand(1)) };
236 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
242 NVT, Ops, 2, false, N->getDebugLoc());
245 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
246 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
247 SDValue Op = GetSoftenedFloat(N->getOperand(0));
248 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
254 NVT, &Op, 1, false, N->getDebugLoc());
257 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
258 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
259 SDValue Op = GetSoftenedFloat(N->getOperand(0));
260 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
265 RTLIB::EXP2_PPCF128),
266 NVT, &Op, 1, false, N->getDebugLoc());
269 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
270 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
271 SDValue Op = GetSoftenedFloat(N->getOperand(0));
272 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
277 RTLIB::FLOOR_PPCF128),
278 NVT, &Op, 1, false, N->getDebugLoc());
281 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
282 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
283 SDValue Op = GetSoftenedFloat(N->getOperand(0));
284 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
290 NVT, &Op, 1, false, N->getDebugLoc());
293 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
294 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
295 SDValue Op = GetSoftenedFloat(N->getOperand(0));
296 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
301 RTLIB::LOG2_PPCF128),
302 NVT, &Op, 1, false, N->getDebugLoc());
305 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
306 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
307 SDValue Op = GetSoftenedFloat(N->getOperand(0));
308 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
313 RTLIB::LOG10_PPCF128),
314 NVT, &Op, 1, false, N->getDebugLoc());
317 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
318 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
319 SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
320 GetSoftenedFloat(N->getOperand(1)),
321 GetSoftenedFloat(N->getOperand(2)) };
322 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
328 NVT, Ops, 3, false, N->getDebugLoc());
331 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
332 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
333 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
334 GetSoftenedFloat(N->getOperand(1)) };
335 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
341 NVT, Ops, 2, false, N->getDebugLoc());
344 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
345 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
346 SDValue Op = GetSoftenedFloat(N->getOperand(0));
347 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
348 RTLIB::NEARBYINT_F32,
349 RTLIB::NEARBYINT_F64,
350 RTLIB::NEARBYINT_F80,
351 RTLIB::NEARBYINT_F128,
352 RTLIB::NEARBYINT_PPCF128),
353 NVT, &Op, 1, false, N->getDebugLoc());
356 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
357 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
358 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
359 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
360 GetSoftenedFloat(N->getOperand(0)) };
361 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
367 NVT, Ops, 2, false, N->getDebugLoc());
370 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
371 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
372 SDValue Op = N->getOperand(0);
373 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
374 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
375 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, N->getDebugLoc());
378 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
380 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP32(SDNode *N) {
381 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
382 SDValue Op = N->getOperand(0);
383 return TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, NVT, &Op, 1, false,
387 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
388 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
389 SDValue Op = N->getOperand(0);
390 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
391 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
392 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, N->getDebugLoc());
395 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
396 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
397 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
398 GetSoftenedFloat(N->getOperand(1)) };
399 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
405 NVT, Ops, 2, false, N->getDebugLoc());
408 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
409 assert(N->getOperand(1).getValueType() == MVT::i32 &&
410 "Unsupported power type!");
411 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
412 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
413 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
418 RTLIB::POWI_PPCF128),
419 NVT, Ops, 2, false, N->getDebugLoc());
422 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
423 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
424 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
425 GetSoftenedFloat(N->getOperand(1)) };
426 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
432 NVT, Ops, 2, false, N->getDebugLoc());
435 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
436 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
437 SDValue Op = GetSoftenedFloat(N->getOperand(0));
438 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
443 RTLIB::RINT_PPCF128),
444 NVT, &Op, 1, false, N->getDebugLoc());
447 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
448 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
449 SDValue Op = GetSoftenedFloat(N->getOperand(0));
450 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
456 NVT, &Op, 1, false, N->getDebugLoc());
459 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
460 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
461 SDValue Op = GetSoftenedFloat(N->getOperand(0));
462 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
467 RTLIB::SQRT_PPCF128),
468 NVT, &Op, 1, false, N->getDebugLoc());
471 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
472 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
473 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
474 GetSoftenedFloat(N->getOperand(1)) };
475 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
481 NVT, Ops, 2, false, N->getDebugLoc());
484 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
485 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
486 SDValue Op = GetSoftenedFloat(N->getOperand(0));
487 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
492 RTLIB::TRUNC_PPCF128),
493 NVT, &Op, 1, false, N->getDebugLoc());
496 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
497 LoadSDNode *L = cast<LoadSDNode>(N);
498 EVT VT = N->getValueType(0);
499 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
500 DebugLoc dl = N->getDebugLoc();
503 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
504 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
505 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
506 L->getPointerInfo(), NVT, L->isVolatile(),
507 L->isNonTemporal(), false, L->getAlignment());
508 // Legalized the chain result - switch anything that used the old chain to
510 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
514 // Do a non-extending load followed by FP_EXTEND.
515 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
516 L->getMemoryVT(), dl, L->getChain(),
517 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
518 L->getMemoryVT(), L->isVolatile(),
519 L->isNonTemporal(), false, L->getAlignment());
520 // Legalized the chain result - switch anything that used the old chain to
522 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
523 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
526 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
527 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
528 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
529 return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
530 LHS.getValueType(), N->getOperand(0),LHS,RHS);
533 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
534 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
535 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
536 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
537 LHS.getValueType(), N->getOperand(0),
538 N->getOperand(1), LHS, RHS, N->getOperand(4));
541 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
542 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
543 N->getValueType(0)));
546 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
547 SDValue Chain = N->getOperand(0); // Get the chain.
548 SDValue Ptr = N->getOperand(1); // Get the pointer.
549 EVT VT = N->getValueType(0);
550 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
551 DebugLoc dl = N->getDebugLoc();
554 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
555 N->getConstantOperandVal(3));
557 // Legalized the chain result - switch anything that used the old chain to
559 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
563 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
564 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
565 EVT SVT = N->getOperand(0).getValueType();
566 EVT RVT = N->getValueType(0);
568 DebugLoc dl = N->getDebugLoc();
570 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
571 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
572 // match. Look for an appropriate libcall.
573 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
574 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
575 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
576 NVT = (MVT::SimpleValueType)t;
577 // The source needs to big enough to hold the operand.
579 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
581 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
583 // Sign/zero extend the argument if the libcall takes a larger type.
584 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
585 NVT, N->getOperand(0));
586 return TLI.makeLibCall(DAG, LC,
587 TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
592 //===----------------------------------------------------------------------===//
593 // Operand Float to Integer Conversion..
594 //===----------------------------------------------------------------------===//
596 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
597 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
599 SDValue Res = SDValue();
601 switch (N->getOpcode()) {
604 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
605 N->dump(&DAG); dbgs() << "\n";
607 llvm_unreachable("Do not know how to soften this operator's operand!");
609 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
610 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
611 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
612 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
613 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
614 case ISD::FP32_TO_FP16:Res = SoftenFloatOp_FP32_TO_FP16(N); break;
615 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
616 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
617 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
620 // If the result is null, the sub-method took care of registering results etc.
621 if (!Res.getNode()) return false;
623 // If the result is N, the sub-method updated N in place. Tell the legalizer
625 if (Res.getNode() == N)
628 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
629 "Invalid operand expansion");
631 ReplaceValueWith(SDValue(N, 0), Res);
635 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
636 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getValueType(0),
637 GetSoftenedFloat(N->getOperand(0)));
640 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
641 EVT SVT = N->getOperand(0).getValueType();
642 EVT RVT = N->getValueType(0);
644 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
645 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
647 SDValue Op = GetSoftenedFloat(N->getOperand(0));
648 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
651 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
652 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
653 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
655 EVT VT = NewLHS.getValueType();
656 NewLHS = GetSoftenedFloat(NewLHS);
657 NewRHS = GetSoftenedFloat(NewRHS);
658 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
660 // If softenSetCCOperands returned a scalar, we need to compare the result
661 // against zero to select between true and false values.
662 if (NewRHS.getNode() == 0) {
663 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
667 // Update N to have the operands specified.
668 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
669 DAG.getCondCode(CCCode), NewLHS, NewRHS,
674 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
675 EVT RVT = N->getValueType(0);
676 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
677 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
678 SDValue Op = GetSoftenedFloat(N->getOperand(0));
679 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
682 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
683 EVT RVT = N->getValueType(0);
684 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
685 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
686 SDValue Op = GetSoftenedFloat(N->getOperand(0));
687 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
690 SDValue DAGTypeLegalizer::SoftenFloatOp_FP32_TO_FP16(SDNode *N) {
691 EVT RVT = N->getValueType(0);
692 RTLIB::Libcall LC = RTLIB::FPROUND_F32_F16;
693 SDValue Op = GetSoftenedFloat(N->getOperand(0));
694 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
697 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
698 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
699 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
701 EVT VT = NewLHS.getValueType();
702 NewLHS = GetSoftenedFloat(NewLHS);
703 NewRHS = GetSoftenedFloat(NewRHS);
704 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
706 // If softenSetCCOperands returned a scalar, we need to compare the result
707 // against zero to select between true and false values.
708 if (NewRHS.getNode() == 0) {
709 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
713 // Update N to have the operands specified.
714 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
715 N->getOperand(2), N->getOperand(3),
716 DAG.getCondCode(CCCode)),
720 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
721 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
722 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
724 EVT VT = NewLHS.getValueType();
725 NewLHS = GetSoftenedFloat(NewLHS);
726 NewRHS = GetSoftenedFloat(NewRHS);
727 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
729 // If softenSetCCOperands returned a scalar, use it.
730 if (NewRHS.getNode() == 0) {
731 assert(NewLHS.getValueType() == N->getValueType(0) &&
732 "Unexpected setcc expansion!");
736 // Otherwise, update N to have the operands specified.
737 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
738 DAG.getCondCode(CCCode)),
742 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
743 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
744 assert(OpNo == 1 && "Can only soften the stored value!");
745 StoreSDNode *ST = cast<StoreSDNode>(N);
746 SDValue Val = ST->getValue();
747 DebugLoc dl = N->getDebugLoc();
749 if (ST->isTruncatingStore())
750 // Do an FP_ROUND followed by a non-truncating store.
751 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
752 Val, DAG.getIntPtrConstant(0)));
754 Val = GetSoftenedFloat(Val);
756 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
757 ST->getPointerInfo(),
758 ST->isVolatile(), ST->isNonTemporal(),
763 //===----------------------------------------------------------------------===//
764 // Float Result Expansion
765 //===----------------------------------------------------------------------===//
767 /// ExpandFloatResult - This method is called when the specified result of the
768 /// specified node is found to need expansion. At this point, the node may also
769 /// have invalid operands or may have other results that need promotion, we just
770 /// know that (at least) one result needs expansion.
771 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
772 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
776 // See if the target wants to custom expand this node.
777 if (CustomLowerNode(N, N->getValueType(ResNo), true))
780 switch (N->getOpcode()) {
783 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
784 N->dump(&DAG); dbgs() << "\n";
786 llvm_unreachable("Do not know how to expand the result of this operator!");
788 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
789 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
790 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
792 case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
793 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
794 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
795 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
796 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
797 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
799 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
800 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
801 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
802 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
803 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
804 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
805 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
806 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
807 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
808 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
809 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
810 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
811 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
812 case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
813 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
814 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
815 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
816 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
817 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
818 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
819 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
820 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
821 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
822 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
823 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
824 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
825 case ISD::SINT_TO_FP:
826 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
829 // If Lo/Hi is null, the sub-method took care of registering results etc.
831 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
834 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
836 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
837 assert(NVT.getSizeInBits() == integerPartWidth &&
838 "Do not know how to expand this float constant!");
839 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
840 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
841 APInt(integerPartWidth, C.getRawData()[1])),
843 Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
844 APInt(integerPartWidth, C.getRawData()[0])),
848 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
850 assert(N->getValueType(0) == MVT::ppcf128 &&
851 "Logic only correct for ppcf128!");
852 DebugLoc dl = N->getDebugLoc();
854 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
855 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
856 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
857 Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
858 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
859 DAG.getCondCode(ISD::SETEQ));
862 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
864 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
865 RTLIB::ADD_F32, RTLIB::ADD_F64,
866 RTLIB::ADD_F80, RTLIB::ADD_F128,
869 GetPairElements(Call, Lo, Hi);
872 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
873 SDValue &Lo, SDValue &Hi) {
874 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
875 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
876 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
877 RTLIB::CEIL_PPCF128),
879 GetPairElements(Call, Lo, Hi);
882 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
883 SDValue &Lo, SDValue &Hi) {
884 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
888 RTLIB::COPYSIGN_F128,
889 RTLIB::COPYSIGN_PPCF128),
891 GetPairElements(Call, Lo, Hi);
894 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
895 SDValue &Lo, SDValue &Hi) {
896 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
897 RTLIB::COS_F32, RTLIB::COS_F64,
898 RTLIB::COS_F80, RTLIB::COS_F128,
901 GetPairElements(Call, Lo, Hi);
904 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
906 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
907 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
913 N->getValueType(0), Ops, 2, false,
915 GetPairElements(Call, Lo, Hi);
918 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
919 SDValue &Lo, SDValue &Hi) {
920 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
921 RTLIB::EXP_F32, RTLIB::EXP_F64,
922 RTLIB::EXP_F80, RTLIB::EXP_F128,
925 GetPairElements(Call, Lo, Hi);
928 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
929 SDValue &Lo, SDValue &Hi) {
930 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
931 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
932 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
933 RTLIB::EXP2_PPCF128),
935 GetPairElements(Call, Lo, Hi);
938 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
939 SDValue &Lo, SDValue &Hi) {
940 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
941 RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
942 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
943 RTLIB::FLOOR_PPCF128),
945 GetPairElements(Call, Lo, Hi);
948 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
949 SDValue &Lo, SDValue &Hi) {
950 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
951 RTLIB::LOG_F32, RTLIB::LOG_F64,
952 RTLIB::LOG_F80, RTLIB::LOG_F128,
955 GetPairElements(Call, Lo, Hi);
958 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
959 SDValue &Lo, SDValue &Hi) {
960 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
961 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
962 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
963 RTLIB::LOG2_PPCF128),
965 GetPairElements(Call, Lo, Hi);
968 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
969 SDValue &Lo, SDValue &Hi) {
970 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
971 RTLIB::LOG10_F32, RTLIB::LOG10_F64,
972 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
973 RTLIB::LOG10_PPCF128),
975 GetPairElements(Call, Lo, Hi);
978 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
980 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
981 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
987 N->getValueType(0), Ops, 3, false,
989 GetPairElements(Call, Lo, Hi);
992 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
994 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
995 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1000 RTLIB::MUL_PPCF128),
1001 N->getValueType(0), Ops, 2, false,
1003 GetPairElements(Call, Lo, Hi);
1006 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1007 SDValue &Lo, SDValue &Hi) {
1008 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1009 RTLIB::NEARBYINT_F32,
1010 RTLIB::NEARBYINT_F64,
1011 RTLIB::NEARBYINT_F80,
1012 RTLIB::NEARBYINT_F128,
1013 RTLIB::NEARBYINT_PPCF128),
1015 GetPairElements(Call, Lo, Hi);
1018 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1020 DebugLoc dl = N->getDebugLoc();
1021 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1022 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1023 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1026 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1028 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1029 Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
1030 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1031 APInt(NVT.getSizeInBits(), 0)), NVT);
1034 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1035 SDValue &Lo, SDValue &Hi) {
1036 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1037 RTLIB::POW_F32, RTLIB::POW_F64,
1038 RTLIB::POW_F80, RTLIB::POW_F128,
1039 RTLIB::POW_PPCF128),
1041 GetPairElements(Call, Lo, Hi);
1044 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1045 SDValue &Lo, SDValue &Hi) {
1046 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1047 RTLIB::POWI_F32, RTLIB::POWI_F64,
1048 RTLIB::POWI_F80, RTLIB::POWI_F128,
1049 RTLIB::POWI_PPCF128),
1051 GetPairElements(Call, Lo, Hi);
1054 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1055 SDValue &Lo, SDValue &Hi) {
1056 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1057 RTLIB::RINT_F32, RTLIB::RINT_F64,
1058 RTLIB::RINT_F80, RTLIB::RINT_F128,
1059 RTLIB::RINT_PPCF128),
1061 GetPairElements(Call, Lo, Hi);
1064 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1065 SDValue &Lo, SDValue &Hi) {
1066 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1067 RTLIB::SIN_F32, RTLIB::SIN_F64,
1068 RTLIB::SIN_F80, RTLIB::SIN_F128,
1069 RTLIB::SIN_PPCF128),
1071 GetPairElements(Call, Lo, Hi);
1074 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1075 SDValue &Lo, SDValue &Hi) {
1076 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1077 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1078 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1079 RTLIB::SQRT_PPCF128),
1081 GetPairElements(Call, Lo, Hi);
1084 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1086 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1087 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1092 RTLIB::SUB_PPCF128),
1093 N->getValueType(0), Ops, 2, false,
1095 GetPairElements(Call, Lo, Hi);
1098 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1099 SDValue &Lo, SDValue &Hi) {
1100 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1101 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1102 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1103 RTLIB::TRUNC_PPCF128),
1105 GetPairElements(Call, Lo, Hi);
1108 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1110 if (ISD::isNormalLoad(N)) {
1111 ExpandRes_NormalLoad(N, Lo, Hi);
1115 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1116 LoadSDNode *LD = cast<LoadSDNode>(N);
1117 SDValue Chain = LD->getChain();
1118 SDValue Ptr = LD->getBasePtr();
1119 DebugLoc dl = N->getDebugLoc();
1121 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1122 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1123 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1125 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1126 LD->getPointerInfo(), LD->getMemoryVT(), LD->isVolatile(),
1127 LD->isNonTemporal(), LD->getAlignment());
1129 // Remember the chain.
1130 Chain = Hi.getValue(1);
1132 // The low part is zero.
1133 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1134 APInt(NVT.getSizeInBits(), 0)), NVT);
1136 // Modified the chain - switch anything that used the old chain to use the
1138 ReplaceValueWith(SDValue(LD, 1), Chain);
1141 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1143 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1144 EVT VT = N->getValueType(0);
1145 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1146 SDValue Src = N->getOperand(0);
1147 EVT SrcVT = Src.getValueType();
1148 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1149 DebugLoc dl = N->getDebugLoc();
1151 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1152 // When promoting partial word types to i32 we must honor the signedness,
1154 if (SrcVT.bitsLE(MVT::i32)) {
1155 // The integer can be represented exactly in an f64.
1156 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1158 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1159 APInt(NVT.getSizeInBits(), 0)), NVT);
1160 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1162 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1163 if (SrcVT.bitsLE(MVT::i64)) {
1164 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1166 LC = RTLIB::SINTTOFP_I64_PPCF128;
1167 } else if (SrcVT.bitsLE(MVT::i128)) {
1168 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1169 LC = RTLIB::SINTTOFP_I128_PPCF128;
1171 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1173 Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl);
1174 GetPairElements(Hi, Lo, Hi);
1180 // Unsigned - fix up the SINT_TO_FP value just calculated.
1181 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1182 SrcVT = Src.getValueType();
1184 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1185 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1186 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1187 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1188 ArrayRef<uint64_t> Parts;
1190 switch (SrcVT.getSimpleVT().SimpleTy) {
1192 llvm_unreachable("Unsupported UINT_TO_FP!");
1204 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1205 DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1208 Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
1209 Lo, Hi, DAG.getCondCode(ISD::SETLT));
1210 GetPairElements(Lo, Lo, Hi);
1214 //===----------------------------------------------------------------------===//
1215 // Float Operand Expansion
1216 //===----------------------------------------------------------------------===//
1218 /// ExpandFloatOperand - This method is called when the specified operand of the
1219 /// specified node is found to need expansion. At this point, all of the result
1220 /// types of the node are known to be legal, but other operands of the node may
1221 /// need promotion or expansion as well as the specified one.
1222 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1223 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1224 SDValue Res = SDValue();
1226 // See if the target wants to custom expand this node.
1227 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1230 switch (N->getOpcode()) {
1233 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1234 N->dump(&DAG); dbgs() << "\n";
1236 llvm_unreachable("Do not know how to expand this operator's operand!");
1238 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1239 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1240 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1242 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1243 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1244 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1245 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1246 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1247 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1248 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1252 // If the result is null, the sub-method took care of registering results etc.
1253 if (!Res.getNode()) return false;
1255 // If the result is N, the sub-method updated N in place. Tell the legalizer
1257 if (Res.getNode() == N)
1260 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1261 "Invalid operand expansion");
1263 ReplaceValueWith(SDValue(N, 0), Res);
1267 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1268 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1269 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1271 ISD::CondCode &CCCode,
1273 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1274 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1275 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1277 assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1279 // FIXME: This generated code sucks. We want to generate
1280 // FCMPU crN, hi1, hi2
1282 // FCMPU crN, lo1, lo2
1283 // The following can be improved, but not that much.
1284 SDValue Tmp1, Tmp2, Tmp3;
1285 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1286 LHSHi, RHSHi, ISD::SETOEQ);
1287 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
1288 LHSLo, RHSLo, CCCode);
1289 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1290 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1291 LHSHi, RHSHi, ISD::SETUNE);
1292 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1293 LHSHi, RHSHi, CCCode);
1294 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1295 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1296 NewRHS = SDValue(); // LHS is the result, not a compare.
1299 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1300 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1301 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1302 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1304 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1305 // against zero to select between true and false values.
1306 if (NewRHS.getNode() == 0) {
1307 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1308 CCCode = ISD::SETNE;
1311 // Update N to have the operands specified.
1312 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1313 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1314 N->getOperand(4)), 0);
1317 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1318 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1319 "Logic only correct for ppcf128!");
1321 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1322 // Round it the rest of the way (e.g. to f32) if needed.
1323 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
1324 N->getValueType(0), Hi, N->getOperand(1));
1327 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1328 EVT RVT = N->getValueType(0);
1329 DebugLoc dl = N->getDebugLoc();
1331 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1332 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1333 if (RVT == MVT::i32) {
1334 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1335 "Logic only correct for ppcf128!");
1336 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1337 N->getOperand(0), DAG.getValueType(MVT::f64));
1338 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1339 DAG.getIntPtrConstant(1));
1340 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1343 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1344 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1345 return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl);
1348 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1349 EVT RVT = N->getValueType(0);
1350 DebugLoc dl = N->getDebugLoc();
1352 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1353 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1354 if (RVT == MVT::i32) {
1355 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1356 "Logic only correct for ppcf128!");
1357 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1358 APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1359 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1360 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1361 // FIXME: generated code sucks.
1362 return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
1363 DAG.getNode(ISD::ADD, dl, MVT::i32,
1364 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1365 DAG.getNode(ISD::FSUB, dl,
1369 DAG.getConstant(0x80000000, MVT::i32)),
1370 DAG.getNode(ISD::FP_TO_SINT, dl,
1371 MVT::i32, N->getOperand(0)),
1372 DAG.getCondCode(ISD::SETGE));
1375 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1376 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1377 return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1381 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1382 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1383 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1384 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1386 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1387 // against zero to select between true and false values.
1388 if (NewRHS.getNode() == 0) {
1389 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1390 CCCode = ISD::SETNE;
1393 // Update N to have the operands specified.
1394 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1395 N->getOperand(2), N->getOperand(3),
1396 DAG.getCondCode(CCCode)), 0);
1399 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1400 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1401 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1402 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1404 // If ExpandSetCCOperands returned a scalar, use it.
1405 if (NewRHS.getNode() == 0) {
1406 assert(NewLHS.getValueType() == N->getValueType(0) &&
1407 "Unexpected setcc expansion!");
1411 // Otherwise, update N to have the operands specified.
1412 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1413 DAG.getCondCode(CCCode)), 0);
1416 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1417 if (ISD::isNormalStore(N))
1418 return ExpandOp_NormalStore(N, OpNo);
1420 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1421 assert(OpNo == 1 && "Can only expand the stored value so far");
1422 StoreSDNode *ST = cast<StoreSDNode>(N);
1424 SDValue Chain = ST->getChain();
1425 SDValue Ptr = ST->getBasePtr();
1427 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1428 ST->getValue().getValueType());
1429 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1430 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1434 GetExpandedOp(ST->getValue(), Lo, Hi);
1436 return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
1437 ST->getPointerInfo(),
1438 ST->getMemoryVT(), ST->isVolatile(),
1439 ST->isNonTemporal(), ST->getAlignment());