1 //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "LegalizeTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
26
27 #define DEBUG_TYPE "legalize-types"
28
29 /// GetFPLibCall - Return the right libcall for the given floating point type.
GetFPLibCall(EVT VT,RTLIB::Libcall Call_F32,RTLIB::Libcall Call_F64,RTLIB::Libcall Call_F80,RTLIB::Libcall Call_F128,RTLIB::Libcall Call_PPCF128)30 static RTLIB::Libcall GetFPLibCall(EVT VT,
31 RTLIB::Libcall Call_F32,
32 RTLIB::Libcall Call_F64,
33 RTLIB::Libcall Call_F80,
34 RTLIB::Libcall Call_F128,
35 RTLIB::Libcall Call_PPCF128) {
36 return
37 VT == MVT::f32 ? Call_F32 :
38 VT == MVT::f64 ? Call_F64 :
39 VT == MVT::f80 ? Call_F80 :
40 VT == MVT::f128 ? Call_F128 :
41 VT == MVT::ppcf128 ? Call_PPCF128 :
42 RTLIB::UNKNOWN_LIBCALL;
43 }
44
45 //===----------------------------------------------------------------------===//
46 // Result Float to Integer Conversion.
47 //===----------------------------------------------------------------------===//
48
SoftenFloatResult(SDNode * N,unsigned ResNo)49 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
50 DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
51 dbgs() << "\n");
52 SDValue R = SDValue();
53
54 switch (N->getOpcode()) {
55 default:
56 #ifndef NDEBUG
57 dbgs() << "SoftenFloatResult #" << ResNo << ": ";
58 N->dump(&DAG); dbgs() << "\n";
59 #endif
60 llvm_unreachable("Do not know how to soften the result of this operator!");
61
62 case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
63 case ISD::BITCAST: R = SoftenFloatRes_BITCAST(N); break;
64 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
65 case ISD::ConstantFP:
66 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
67 break;
68 case ISD::EXTRACT_VECTOR_ELT:
69 R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
70 case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
71 case ISD::FMINNUM: R = SoftenFloatRes_FMINNUM(N); break;
72 case ISD::FMAXNUM: R = SoftenFloatRes_FMAXNUM(N); break;
73 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
74 case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
75 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
76 case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
77 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
78 case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
79 case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
80 case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
81 case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
82 case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
83 case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
84 case ISD::FMA: R = SoftenFloatRes_FMA(N); break;
85 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
86 case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
87 case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
88 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
89 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
90 case ISD::FP16_TO_FP: R = SoftenFloatRes_FP16_TO_FP(N); break;
91 case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
92 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
93 case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
94 case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
95 case ISD::FROUND: R = SoftenFloatRes_FROUND(N); break;
96 case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
97 case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
98 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
99 case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
100 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
101 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
102 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
103 case ISD::SINT_TO_FP:
104 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
105 case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
106 case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
107 }
108
109 // If R is null, the sub-method took care of registering the result.
110 if (R.getNode())
111 SetSoftenedFloat(SDValue(N, ResNo), R);
112 }
113
SoftenFloatRes_BITCAST(SDNode * N)114 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
115 return BitConvertToInteger(N->getOperand(0));
116 }
117
SoftenFloatRes_MERGE_VALUES(SDNode * N,unsigned ResNo)118 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
119 unsigned ResNo) {
120 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
121 return BitConvertToInteger(Op);
122 }
123
SoftenFloatRes_BUILD_PAIR(SDNode * N)124 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
125 // Convert the inputs to integers, and build a new pair out of them.
126 return DAG.getNode(ISD::BUILD_PAIR, SDLoc(N),
127 TLI.getTypeToTransformTo(*DAG.getContext(),
128 N->getValueType(0)),
129 BitConvertToInteger(N->getOperand(0)),
130 BitConvertToInteger(N->getOperand(1)));
131 }
132
SoftenFloatRes_ConstantFP(ConstantFPSDNode * N)133 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
134 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
135 TLI.getTypeToTransformTo(*DAG.getContext(),
136 N->getValueType(0)));
137 }
138
SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode * N)139 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
140 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
141 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
142 NewOp.getValueType().getVectorElementType(),
143 NewOp, N->getOperand(1));
144 }
145
SoftenFloatRes_FABS(SDNode * N)146 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
147 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
148 unsigned Size = NVT.getSizeInBits();
149
150 // Mask = ~(1 << (Size-1))
151 APInt API = APInt::getAllOnesValue(Size);
152 API.clearBit(Size-1);
153 SDValue Mask = DAG.getConstant(API, NVT);
154 SDValue Op = GetSoftenedFloat(N->getOperand(0));
155 return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
156 }
157
SoftenFloatRes_FMINNUM(SDNode * N)158 SDValue DAGTypeLegalizer::SoftenFloatRes_FMINNUM(SDNode *N) {
159 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
160 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
161 GetSoftenedFloat(N->getOperand(1)) };
162 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
163 RTLIB::FMIN_F32,
164 RTLIB::FMIN_F64,
165 RTLIB::FMIN_F80,
166 RTLIB::FMIN_F128,
167 RTLIB::FMIN_PPCF128),
168 NVT, Ops, 2, false, SDLoc(N)).first;
169 }
170
SoftenFloatRes_FMAXNUM(SDNode * N)171 SDValue DAGTypeLegalizer::SoftenFloatRes_FMAXNUM(SDNode *N) {
172 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
173 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
174 GetSoftenedFloat(N->getOperand(1)) };
175 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
176 RTLIB::FMAX_F32,
177 RTLIB::FMAX_F64,
178 RTLIB::FMAX_F80,
179 RTLIB::FMAX_F128,
180 RTLIB::FMAX_PPCF128),
181 NVT, Ops, 2, false, SDLoc(N)).first;
182 }
183
SoftenFloatRes_FADD(SDNode * N)184 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
185 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
186 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
187 GetSoftenedFloat(N->getOperand(1)) };
188 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
189 RTLIB::ADD_F32,
190 RTLIB::ADD_F64,
191 RTLIB::ADD_F80,
192 RTLIB::ADD_F128,
193 RTLIB::ADD_PPCF128),
194 NVT, Ops, 2, false, SDLoc(N)).first;
195 }
196
SoftenFloatRes_FCEIL(SDNode * N)197 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
198 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
199 SDValue Op = GetSoftenedFloat(N->getOperand(0));
200 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
201 RTLIB::CEIL_F32,
202 RTLIB::CEIL_F64,
203 RTLIB::CEIL_F80,
204 RTLIB::CEIL_F128,
205 RTLIB::CEIL_PPCF128),
206 NVT, &Op, 1, false, SDLoc(N)).first;
207 }
208
SoftenFloatRes_FCOPYSIGN(SDNode * N)209 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
210 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
211 SDValue RHS = BitConvertToInteger(N->getOperand(1));
212 SDLoc dl(N);
213
214 EVT LVT = LHS.getValueType();
215 EVT RVT = RHS.getValueType();
216
217 unsigned LSize = LVT.getSizeInBits();
218 unsigned RSize = RVT.getSizeInBits();
219
220 // First get the sign bit of second operand.
221 SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
222 DAG.getConstant(RSize - 1,
223 TLI.getShiftAmountTy(RVT)));
224 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
225
226 // Shift right or sign-extend it if the two operands have different types.
227 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
228 if (SizeDiff > 0) {
229 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
230 DAG.getConstant(SizeDiff,
231 TLI.getShiftAmountTy(SignBit.getValueType())));
232 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
233 } else if (SizeDiff < 0) {
234 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
235 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
236 DAG.getConstant(-SizeDiff,
237 TLI.getShiftAmountTy(SignBit.getValueType())));
238 }
239
240 // Clear the sign bit of the first operand.
241 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
242 DAG.getConstant(LSize - 1,
243 TLI.getShiftAmountTy(LVT)));
244 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
245 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
246
247 // Or the value with the sign bit.
248 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
249 }
250
SoftenFloatRes_FCOS(SDNode * N)251 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
252 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
253 SDValue Op = GetSoftenedFloat(N->getOperand(0));
254 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
255 RTLIB::COS_F32,
256 RTLIB::COS_F64,
257 RTLIB::COS_F80,
258 RTLIB::COS_F128,
259 RTLIB::COS_PPCF128),
260 NVT, &Op, 1, false, SDLoc(N)).first;
261 }
262
SoftenFloatRes_FDIV(SDNode * N)263 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
264 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
265 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
266 GetSoftenedFloat(N->getOperand(1)) };
267 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
268 RTLIB::DIV_F32,
269 RTLIB::DIV_F64,
270 RTLIB::DIV_F80,
271 RTLIB::DIV_F128,
272 RTLIB::DIV_PPCF128),
273 NVT, Ops, 2, false, SDLoc(N)).first;
274 }
275
SoftenFloatRes_FEXP(SDNode * N)276 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
277 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
278 SDValue Op = GetSoftenedFloat(N->getOperand(0));
279 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
280 RTLIB::EXP_F32,
281 RTLIB::EXP_F64,
282 RTLIB::EXP_F80,
283 RTLIB::EXP_F128,
284 RTLIB::EXP_PPCF128),
285 NVT, &Op, 1, false, SDLoc(N)).first;
286 }
287
SoftenFloatRes_FEXP2(SDNode * N)288 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
289 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
290 SDValue Op = GetSoftenedFloat(N->getOperand(0));
291 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
292 RTLIB::EXP2_F32,
293 RTLIB::EXP2_F64,
294 RTLIB::EXP2_F80,
295 RTLIB::EXP2_F128,
296 RTLIB::EXP2_PPCF128),
297 NVT, &Op, 1, false, SDLoc(N)).first;
298 }
299
SoftenFloatRes_FFLOOR(SDNode * N)300 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
301 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
302 SDValue Op = GetSoftenedFloat(N->getOperand(0));
303 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
304 RTLIB::FLOOR_F32,
305 RTLIB::FLOOR_F64,
306 RTLIB::FLOOR_F80,
307 RTLIB::FLOOR_F128,
308 RTLIB::FLOOR_PPCF128),
309 NVT, &Op, 1, false, SDLoc(N)).first;
310 }
311
SoftenFloatRes_FLOG(SDNode * N)312 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
313 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
314 SDValue Op = GetSoftenedFloat(N->getOperand(0));
315 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
316 RTLIB::LOG_F32,
317 RTLIB::LOG_F64,
318 RTLIB::LOG_F80,
319 RTLIB::LOG_F128,
320 RTLIB::LOG_PPCF128),
321 NVT, &Op, 1, false, SDLoc(N)).first;
322 }
323
SoftenFloatRes_FLOG2(SDNode * N)324 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
325 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
326 SDValue Op = GetSoftenedFloat(N->getOperand(0));
327 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
328 RTLIB::LOG2_F32,
329 RTLIB::LOG2_F64,
330 RTLIB::LOG2_F80,
331 RTLIB::LOG2_F128,
332 RTLIB::LOG2_PPCF128),
333 NVT, &Op, 1, false, SDLoc(N)).first;
334 }
335
SoftenFloatRes_FLOG10(SDNode * N)336 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
337 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
338 SDValue Op = GetSoftenedFloat(N->getOperand(0));
339 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
340 RTLIB::LOG10_F32,
341 RTLIB::LOG10_F64,
342 RTLIB::LOG10_F80,
343 RTLIB::LOG10_F128,
344 RTLIB::LOG10_PPCF128),
345 NVT, &Op, 1, false, SDLoc(N)).first;
346 }
347
SoftenFloatRes_FMA(SDNode * N)348 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
349 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
350 SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
351 GetSoftenedFloat(N->getOperand(1)),
352 GetSoftenedFloat(N->getOperand(2)) };
353 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
354 RTLIB::FMA_F32,
355 RTLIB::FMA_F64,
356 RTLIB::FMA_F80,
357 RTLIB::FMA_F128,
358 RTLIB::FMA_PPCF128),
359 NVT, Ops, 3, false, SDLoc(N)).first;
360 }
361
SoftenFloatRes_FMUL(SDNode * N)362 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
363 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
364 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
365 GetSoftenedFloat(N->getOperand(1)) };
366 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
367 RTLIB::MUL_F32,
368 RTLIB::MUL_F64,
369 RTLIB::MUL_F80,
370 RTLIB::MUL_F128,
371 RTLIB::MUL_PPCF128),
372 NVT, Ops, 2, false, SDLoc(N)).first;
373 }
374
SoftenFloatRes_FNEARBYINT(SDNode * N)375 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
376 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
377 SDValue Op = GetSoftenedFloat(N->getOperand(0));
378 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
379 RTLIB::NEARBYINT_F32,
380 RTLIB::NEARBYINT_F64,
381 RTLIB::NEARBYINT_F80,
382 RTLIB::NEARBYINT_F128,
383 RTLIB::NEARBYINT_PPCF128),
384 NVT, &Op, 1, false, SDLoc(N)).first;
385 }
386
SoftenFloatRes_FNEG(SDNode * N)387 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
388 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
389 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
390 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
391 GetSoftenedFloat(N->getOperand(0)) };
392 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
393 RTLIB::SUB_F32,
394 RTLIB::SUB_F64,
395 RTLIB::SUB_F80,
396 RTLIB::SUB_F128,
397 RTLIB::SUB_PPCF128),
398 NVT, Ops, 2, false, SDLoc(N)).first;
399 }
400
SoftenFloatRes_FP_EXTEND(SDNode * N)401 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
402 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
403 SDValue Op = N->getOperand(0);
404
405 // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
406 // entirely possible for both f16 and f32 to be legal, so use the fully
407 // hard-float FP_EXTEND rather than FP16_TO_FP.
408 if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
409 Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
410 if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
411 SoftenFloatResult(Op.getNode(), 0);
412 }
413
414 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
415 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
416 Op = GetSoftenedFloat(Op);
417 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
418 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
419 }
420
421 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
422 // nodes?
SoftenFloatRes_FP16_TO_FP(SDNode * N)423 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
424 EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
425 SDValue Op = N->getOperand(0);
426 SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, &Op, 1,
427 false, SDLoc(N)).first;
428 if (N->getValueType(0) == MVT::f32)
429 return Res32;
430
431 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
432 RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
433 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
434 return TLI.makeLibCall(DAG, LC, NVT, &Res32, 1, false, SDLoc(N)).first;
435 }
436
SoftenFloatRes_FP_ROUND(SDNode * N)437 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
438 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
439 SDValue Op = N->getOperand(0);
440 if (N->getValueType(0) == MVT::f16) {
441 // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
442 // storage-only type get a chance to select things.
443 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
444 }
445
446 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
447 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
448 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
449 }
450
SoftenFloatRes_FPOW(SDNode * N)451 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
452 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
453 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
454 GetSoftenedFloat(N->getOperand(1)) };
455 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
456 RTLIB::POW_F32,
457 RTLIB::POW_F64,
458 RTLIB::POW_F80,
459 RTLIB::POW_F128,
460 RTLIB::POW_PPCF128),
461 NVT, Ops, 2, false, SDLoc(N)).first;
462 }
463
SoftenFloatRes_FPOWI(SDNode * N)464 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
465 assert(N->getOperand(1).getValueType() == MVT::i32 &&
466 "Unsupported power type!");
467 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
468 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
469 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
470 RTLIB::POWI_F32,
471 RTLIB::POWI_F64,
472 RTLIB::POWI_F80,
473 RTLIB::POWI_F128,
474 RTLIB::POWI_PPCF128),
475 NVT, Ops, 2, false, SDLoc(N)).first;
476 }
477
SoftenFloatRes_FREM(SDNode * N)478 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
479 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
480 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
481 GetSoftenedFloat(N->getOperand(1)) };
482 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
483 RTLIB::REM_F32,
484 RTLIB::REM_F64,
485 RTLIB::REM_F80,
486 RTLIB::REM_F128,
487 RTLIB::REM_PPCF128),
488 NVT, Ops, 2, false, SDLoc(N)).first;
489 }
490
SoftenFloatRes_FRINT(SDNode * N)491 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
492 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
493 SDValue Op = GetSoftenedFloat(N->getOperand(0));
494 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
495 RTLIB::RINT_F32,
496 RTLIB::RINT_F64,
497 RTLIB::RINT_F80,
498 RTLIB::RINT_F128,
499 RTLIB::RINT_PPCF128),
500 NVT, &Op, 1, false, SDLoc(N)).first;
501 }
502
SoftenFloatRes_FROUND(SDNode * N)503 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
504 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
505 SDValue Op = GetSoftenedFloat(N->getOperand(0));
506 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
507 RTLIB::ROUND_F32,
508 RTLIB::ROUND_F64,
509 RTLIB::ROUND_F80,
510 RTLIB::ROUND_F128,
511 RTLIB::ROUND_PPCF128),
512 NVT, &Op, 1, false, SDLoc(N)).first;
513 }
514
SoftenFloatRes_FSIN(SDNode * N)515 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
516 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
517 SDValue Op = GetSoftenedFloat(N->getOperand(0));
518 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
519 RTLIB::SIN_F32,
520 RTLIB::SIN_F64,
521 RTLIB::SIN_F80,
522 RTLIB::SIN_F128,
523 RTLIB::SIN_PPCF128),
524 NVT, &Op, 1, false, SDLoc(N)).first;
525 }
526
SoftenFloatRes_FSQRT(SDNode * N)527 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
528 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
529 SDValue Op = GetSoftenedFloat(N->getOperand(0));
530 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
531 RTLIB::SQRT_F32,
532 RTLIB::SQRT_F64,
533 RTLIB::SQRT_F80,
534 RTLIB::SQRT_F128,
535 RTLIB::SQRT_PPCF128),
536 NVT, &Op, 1, false, SDLoc(N)).first;
537 }
538
SoftenFloatRes_FSUB(SDNode * N)539 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
540 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
541 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
542 GetSoftenedFloat(N->getOperand(1)) };
543 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
544 RTLIB::SUB_F32,
545 RTLIB::SUB_F64,
546 RTLIB::SUB_F80,
547 RTLIB::SUB_F128,
548 RTLIB::SUB_PPCF128),
549 NVT, Ops, 2, false, SDLoc(N)).first;
550 }
551
SoftenFloatRes_FTRUNC(SDNode * N)552 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
553 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
554 if (N->getValueType(0) == MVT::f16)
555 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
556
557 SDValue Op = GetSoftenedFloat(N->getOperand(0));
558 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
559 RTLIB::TRUNC_F32,
560 RTLIB::TRUNC_F64,
561 RTLIB::TRUNC_F80,
562 RTLIB::TRUNC_F128,
563 RTLIB::TRUNC_PPCF128),
564 NVT, &Op, 1, false, SDLoc(N)).first;
565 }
566
SoftenFloatRes_LOAD(SDNode * N)567 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
568 LoadSDNode *L = cast<LoadSDNode>(N);
569 EVT VT = N->getValueType(0);
570 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
571 SDLoc dl(N);
572
573 SDValue NewL;
574 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
575 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
576 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
577 L->getPointerInfo(), NVT, L->isVolatile(),
578 L->isNonTemporal(), false, L->getAlignment(),
579 L->getAAInfo());
580 // Legalized the chain result - switch anything that used the old chain to
581 // use the new one.
582 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
583 return NewL;
584 }
585
586 // Do a non-extending load followed by FP_EXTEND.
587 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
588 L->getMemoryVT(), dl, L->getChain(),
589 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
590 L->getMemoryVT(), L->isVolatile(),
591 L->isNonTemporal(), false, L->getAlignment(),
592 L->getAAInfo());
593 // Legalized the chain result - switch anything that used the old chain to
594 // use the new one.
595 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
596 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
597 }
598
SoftenFloatRes_SELECT(SDNode * N)599 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
600 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
601 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
602 return DAG.getSelect(SDLoc(N),
603 LHS.getValueType(), N->getOperand(0), LHS, RHS);
604 }
605
SoftenFloatRes_SELECT_CC(SDNode * N)606 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
607 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
608 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
609 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
610 LHS.getValueType(), N->getOperand(0),
611 N->getOperand(1), LHS, RHS, N->getOperand(4));
612 }
613
SoftenFloatRes_UNDEF(SDNode * N)614 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
615 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
616 N->getValueType(0)));
617 }
618
SoftenFloatRes_VAARG(SDNode * N)619 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
620 SDValue Chain = N->getOperand(0); // Get the chain.
621 SDValue Ptr = N->getOperand(1); // Get the pointer.
622 EVT VT = N->getValueType(0);
623 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
624 SDLoc dl(N);
625
626 SDValue NewVAARG;
627 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
628 N->getConstantOperandVal(3));
629
630 // Legalized the chain result - switch anything that used the old chain to
631 // use the new one.
632 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
633 return NewVAARG;
634 }
635
SoftenFloatRes_XINT_TO_FP(SDNode * N)636 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
637 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
638 EVT SVT = N->getOperand(0).getValueType();
639 EVT RVT = N->getValueType(0);
640 EVT NVT = EVT();
641 SDLoc dl(N);
642
643 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
644 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
645 // match. Look for an appropriate libcall.
646 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
647 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
648 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
649 NVT = (MVT::SimpleValueType)t;
650 // The source needs to big enough to hold the operand.
651 if (NVT.bitsGE(SVT))
652 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
653 }
654 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
655
656 // Sign/zero extend the argument if the libcall takes a larger type.
657 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
658 NVT, N->getOperand(0));
659 return TLI.makeLibCall(DAG, LC,
660 TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
661 &Op, 1, Signed, dl).first;
662 }
663
664
665 //===----------------------------------------------------------------------===//
666 // Operand Float to Integer Conversion..
667 //===----------------------------------------------------------------------===//
668
SoftenFloatOperand(SDNode * N,unsigned OpNo)669 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
670 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
671 dbgs() << "\n");
672 SDValue Res = SDValue();
673
674 switch (N->getOpcode()) {
675 default:
676 #ifndef NDEBUG
677 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
678 N->dump(&DAG); dbgs() << "\n";
679 #endif
680 llvm_unreachable("Do not know how to soften this operator's operand!");
681
682 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
683 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
684 case ISD::FP_EXTEND: Res = SoftenFloatOp_FP_EXTEND(N); break;
685 case ISD::FP_TO_FP16: // Same as FP_ROUND for softening purposes
686 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
687 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
688 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
689 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
690 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
691 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
692 }
693
694 // If the result is null, the sub-method took care of registering results etc.
695 if (!Res.getNode()) return false;
696
697 // If the result is N, the sub-method updated N in place. Tell the legalizer
698 // core about this.
699 if (Res.getNode() == N)
700 return true;
701
702 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
703 "Invalid operand expansion");
704
705 ReplaceValueWith(SDValue(N, 0), Res);
706 return false;
707 }
708
SoftenFloatOp_BITCAST(SDNode * N)709 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
710 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
711 GetSoftenedFloat(N->getOperand(0)));
712 }
713
SoftenFloatOp_FP_EXTEND(SDNode * N)714 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
715 // If we get here, the result must be legal but the source illegal.
716 EVT SVT = N->getOperand(0).getValueType();
717 EVT RVT = N->getValueType(0);
718 SDValue Op = GetSoftenedFloat(N->getOperand(0));
719
720 if (SVT == MVT::f16)
721 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
722
723 RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
724 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
725
726 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
727 }
728
729
SoftenFloatOp_FP_ROUND(SDNode * N)730 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
731 // We actually deal with the partially-softened FP_TO_FP16 node too, which
732 // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
733 assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
734
735 EVT SVT = N->getOperand(0).getValueType();
736 EVT RVT = N->getValueType(0);
737 EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
738
739 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
740 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
741
742 SDValue Op = GetSoftenedFloat(N->getOperand(0));
743 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
744 }
745
SoftenFloatOp_BR_CC(SDNode * N)746 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
747 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
748 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
749
750 EVT VT = NewLHS.getValueType();
751 NewLHS = GetSoftenedFloat(NewLHS);
752 NewRHS = GetSoftenedFloat(NewRHS);
753 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
754
755 // If softenSetCCOperands returned a scalar, we need to compare the result
756 // against zero to select between true and false values.
757 if (!NewRHS.getNode()) {
758 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
759 CCCode = ISD::SETNE;
760 }
761
762 // Update N to have the operands specified.
763 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
764 DAG.getCondCode(CCCode), NewLHS, NewRHS,
765 N->getOperand(4)),
766 0);
767 }
768
SoftenFloatOp_FP_TO_SINT(SDNode * N)769 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
770 EVT RVT = N->getValueType(0);
771 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
772 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
773 SDValue Op = GetSoftenedFloat(N->getOperand(0));
774 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
775 }
776
SoftenFloatOp_FP_TO_UINT(SDNode * N)777 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
778 EVT RVT = N->getValueType(0);
779 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
780 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
781 SDValue Op = GetSoftenedFloat(N->getOperand(0));
782 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
783 }
784
SoftenFloatOp_SELECT_CC(SDNode * N)785 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
786 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
787 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
788
789 EVT VT = NewLHS.getValueType();
790 NewLHS = GetSoftenedFloat(NewLHS);
791 NewRHS = GetSoftenedFloat(NewRHS);
792 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
793
794 // If softenSetCCOperands returned a scalar, we need to compare the result
795 // against zero to select between true and false values.
796 if (!NewRHS.getNode()) {
797 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
798 CCCode = ISD::SETNE;
799 }
800
801 // Update N to have the operands specified.
802 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
803 N->getOperand(2), N->getOperand(3),
804 DAG.getCondCode(CCCode)),
805 0);
806 }
807
SoftenFloatOp_SETCC(SDNode * N)808 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
809 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
810 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
811
812 EVT VT = NewLHS.getValueType();
813 NewLHS = GetSoftenedFloat(NewLHS);
814 NewRHS = GetSoftenedFloat(NewRHS);
815 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
816
817 // If softenSetCCOperands returned a scalar, use it.
818 if (!NewRHS.getNode()) {
819 assert(NewLHS.getValueType() == N->getValueType(0) &&
820 "Unexpected setcc expansion!");
821 return NewLHS;
822 }
823
824 // Otherwise, update N to have the operands specified.
825 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
826 DAG.getCondCode(CCCode)),
827 0);
828 }
829
SoftenFloatOp_STORE(SDNode * N,unsigned OpNo)830 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
831 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
832 assert(OpNo == 1 && "Can only soften the stored value!");
833 StoreSDNode *ST = cast<StoreSDNode>(N);
834 SDValue Val = ST->getValue();
835 SDLoc dl(N);
836
837 if (ST->isTruncatingStore())
838 // Do an FP_ROUND followed by a non-truncating store.
839 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
840 Val, DAG.getIntPtrConstant(0)));
841 else
842 Val = GetSoftenedFloat(Val);
843
844 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
845 ST->getMemOperand());
846 }
847
848
849 //===----------------------------------------------------------------------===//
850 // Float Result Expansion
851 //===----------------------------------------------------------------------===//
852
853 /// ExpandFloatResult - This method is called when the specified result of the
854 /// specified node is found to need expansion. At this point, the node may also
855 /// have invalid operands or may have other results that need promotion, we just
856 /// know that (at least) one result needs expansion.
ExpandFloatResult(SDNode * N,unsigned ResNo)857 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
858 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
859 SDValue Lo, Hi;
860 Lo = Hi = SDValue();
861
862 // See if the target wants to custom expand this node.
863 if (CustomLowerNode(N, N->getValueType(ResNo), true))
864 return;
865
866 switch (N->getOpcode()) {
867 default:
868 #ifndef NDEBUG
869 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
870 N->dump(&DAG); dbgs() << "\n";
871 #endif
872 llvm_unreachable("Do not know how to expand the result of this operator!");
873
874 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
875 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
876 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
877
878 case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
879 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
880 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
881 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
882 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
883 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
884
885 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
886 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
887 case ISD::FMINNUM: ExpandFloatRes_FMINNUM(N, Lo, Hi); break;
888 case ISD::FMAXNUM: ExpandFloatRes_FMAXNUM(N, Lo, Hi); break;
889 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
890 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
891 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
892 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
893 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
894 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
895 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
896 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
897 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
898 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
899 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
900 case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
901 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
902 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
903 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
904 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
905 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
906 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
907 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
908 case ISD::FROUND: ExpandFloatRes_FROUND(N, Lo, Hi); break;
909 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
910 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
911 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
912 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
913 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
914 case ISD::SINT_TO_FP:
915 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
916 case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
917 }
918
919 // If Lo/Hi is null, the sub-method took care of registering results etc.
920 if (Lo.getNode())
921 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
922 }
923
ExpandFloatRes_ConstantFP(SDNode * N,SDValue & Lo,SDValue & Hi)924 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
925 SDValue &Hi) {
926 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
927 assert(NVT.getSizeInBits() == integerPartWidth &&
928 "Do not know how to expand this float constant!");
929 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
930 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
931 APInt(integerPartWidth, C.getRawData()[1])),
932 NVT);
933 Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
934 APInt(integerPartWidth, C.getRawData()[0])),
935 NVT);
936 }
937
ExpandFloatRes_FABS(SDNode * N,SDValue & Lo,SDValue & Hi)938 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
939 SDValue &Hi) {
940 assert(N->getValueType(0) == MVT::ppcf128 &&
941 "Logic only correct for ppcf128!");
942 SDLoc dl(N);
943 SDValue Tmp;
944 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
945 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
946 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
947 Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
948 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
949 ISD::SETEQ);
950 }
951
ExpandFloatRes_FMINNUM(SDNode * N,SDValue & Lo,SDValue & Hi)952 void DAGTypeLegalizer::ExpandFloatRes_FMINNUM(SDNode *N, SDValue &Lo,
953 SDValue &Hi) {
954 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
955 RTLIB::FMIN_F32, RTLIB::FMIN_F64,
956 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
957 RTLIB::FMIN_PPCF128),
958 N, false);
959 GetPairElements(Call, Lo, Hi);
960 }
961
ExpandFloatRes_FMAXNUM(SDNode * N,SDValue & Lo,SDValue & Hi)962 void DAGTypeLegalizer::ExpandFloatRes_FMAXNUM(SDNode *N, SDValue &Lo,
963 SDValue &Hi) {
964 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
965 RTLIB::FMAX_F32, RTLIB::FMAX_F64,
966 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
967 RTLIB::FMAX_PPCF128),
968 N, false);
969 GetPairElements(Call, Lo, Hi);
970 }
971
ExpandFloatRes_FADD(SDNode * N,SDValue & Lo,SDValue & Hi)972 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
973 SDValue &Hi) {
974 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
975 RTLIB::ADD_F32, RTLIB::ADD_F64,
976 RTLIB::ADD_F80, RTLIB::ADD_F128,
977 RTLIB::ADD_PPCF128),
978 N, false);
979 GetPairElements(Call, Lo, Hi);
980 }
981
ExpandFloatRes_FCEIL(SDNode * N,SDValue & Lo,SDValue & Hi)982 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
983 SDValue &Lo, SDValue &Hi) {
984 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
985 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
986 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
987 RTLIB::CEIL_PPCF128),
988 N, false);
989 GetPairElements(Call, Lo, Hi);
990 }
991
ExpandFloatRes_FCOPYSIGN(SDNode * N,SDValue & Lo,SDValue & Hi)992 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
993 SDValue &Lo, SDValue &Hi) {
994 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
995 RTLIB::COPYSIGN_F32,
996 RTLIB::COPYSIGN_F64,
997 RTLIB::COPYSIGN_F80,
998 RTLIB::COPYSIGN_F128,
999 RTLIB::COPYSIGN_PPCF128),
1000 N, false);
1001 GetPairElements(Call, Lo, Hi);
1002 }
1003
ExpandFloatRes_FCOS(SDNode * N,SDValue & Lo,SDValue & Hi)1004 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
1005 SDValue &Lo, SDValue &Hi) {
1006 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1007 RTLIB::COS_F32, RTLIB::COS_F64,
1008 RTLIB::COS_F80, RTLIB::COS_F128,
1009 RTLIB::COS_PPCF128),
1010 N, false);
1011 GetPairElements(Call, Lo, Hi);
1012 }
1013
ExpandFloatRes_FDIV(SDNode * N,SDValue & Lo,SDValue & Hi)1014 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
1015 SDValue &Hi) {
1016 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1017 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1018 RTLIB::DIV_F32,
1019 RTLIB::DIV_F64,
1020 RTLIB::DIV_F80,
1021 RTLIB::DIV_F128,
1022 RTLIB::DIV_PPCF128),
1023 N->getValueType(0), Ops, 2, false,
1024 SDLoc(N)).first;
1025 GetPairElements(Call, Lo, Hi);
1026 }
1027
ExpandFloatRes_FEXP(SDNode * N,SDValue & Lo,SDValue & Hi)1028 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
1029 SDValue &Lo, SDValue &Hi) {
1030 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1031 RTLIB::EXP_F32, RTLIB::EXP_F64,
1032 RTLIB::EXP_F80, RTLIB::EXP_F128,
1033 RTLIB::EXP_PPCF128),
1034 N, false);
1035 GetPairElements(Call, Lo, Hi);
1036 }
1037
ExpandFloatRes_FEXP2(SDNode * N,SDValue & Lo,SDValue & Hi)1038 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
1039 SDValue &Lo, SDValue &Hi) {
1040 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1041 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
1042 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
1043 RTLIB::EXP2_PPCF128),
1044 N, false);
1045 GetPairElements(Call, Lo, Hi);
1046 }
1047
ExpandFloatRes_FFLOOR(SDNode * N,SDValue & Lo,SDValue & Hi)1048 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
1049 SDValue &Lo, SDValue &Hi) {
1050 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1051 RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
1052 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
1053 RTLIB::FLOOR_PPCF128),
1054 N, false);
1055 GetPairElements(Call, Lo, Hi);
1056 }
1057
ExpandFloatRes_FLOG(SDNode * N,SDValue & Lo,SDValue & Hi)1058 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1059 SDValue &Lo, SDValue &Hi) {
1060 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1061 RTLIB::LOG_F32, RTLIB::LOG_F64,
1062 RTLIB::LOG_F80, RTLIB::LOG_F128,
1063 RTLIB::LOG_PPCF128),
1064 N, false);
1065 GetPairElements(Call, Lo, Hi);
1066 }
1067
ExpandFloatRes_FLOG2(SDNode * N,SDValue & Lo,SDValue & Hi)1068 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1069 SDValue &Lo, SDValue &Hi) {
1070 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1071 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1072 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1073 RTLIB::LOG2_PPCF128),
1074 N, false);
1075 GetPairElements(Call, Lo, Hi);
1076 }
1077
ExpandFloatRes_FLOG10(SDNode * N,SDValue & Lo,SDValue & Hi)1078 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1079 SDValue &Lo, SDValue &Hi) {
1080 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1081 RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1082 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1083 RTLIB::LOG10_PPCF128),
1084 N, false);
1085 GetPairElements(Call, Lo, Hi);
1086 }
1087
ExpandFloatRes_FMA(SDNode * N,SDValue & Lo,SDValue & Hi)1088 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1089 SDValue &Hi) {
1090 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1091 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1092 RTLIB::FMA_F32,
1093 RTLIB::FMA_F64,
1094 RTLIB::FMA_F80,
1095 RTLIB::FMA_F128,
1096 RTLIB::FMA_PPCF128),
1097 N->getValueType(0), Ops, 3, false,
1098 SDLoc(N)).first;
1099 GetPairElements(Call, Lo, Hi);
1100 }
1101
ExpandFloatRes_FMUL(SDNode * N,SDValue & Lo,SDValue & Hi)1102 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1103 SDValue &Hi) {
1104 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1105 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1106 RTLIB::MUL_F32,
1107 RTLIB::MUL_F64,
1108 RTLIB::MUL_F80,
1109 RTLIB::MUL_F128,
1110 RTLIB::MUL_PPCF128),
1111 N->getValueType(0), Ops, 2, false,
1112 SDLoc(N)).first;
1113 GetPairElements(Call, Lo, Hi);
1114 }
1115
ExpandFloatRes_FNEARBYINT(SDNode * N,SDValue & Lo,SDValue & Hi)1116 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1117 SDValue &Lo, SDValue &Hi) {
1118 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1119 RTLIB::NEARBYINT_F32,
1120 RTLIB::NEARBYINT_F64,
1121 RTLIB::NEARBYINT_F80,
1122 RTLIB::NEARBYINT_F128,
1123 RTLIB::NEARBYINT_PPCF128),
1124 N, false);
1125 GetPairElements(Call, Lo, Hi);
1126 }
1127
ExpandFloatRes_FNEG(SDNode * N,SDValue & Lo,SDValue & Hi)1128 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1129 SDValue &Hi) {
1130 SDLoc dl(N);
1131 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1132 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1133 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1134 }
1135
ExpandFloatRes_FP_EXTEND(SDNode * N,SDValue & Lo,SDValue & Hi)1136 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1137 SDValue &Hi) {
1138 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1139 Hi = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), NVT, N->getOperand(0));
1140 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1141 APInt(NVT.getSizeInBits(), 0)), NVT);
1142 }
1143
ExpandFloatRes_FPOW(SDNode * N,SDValue & Lo,SDValue & Hi)1144 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1145 SDValue &Lo, SDValue &Hi) {
1146 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1147 RTLIB::POW_F32, RTLIB::POW_F64,
1148 RTLIB::POW_F80, RTLIB::POW_F128,
1149 RTLIB::POW_PPCF128),
1150 N, false);
1151 GetPairElements(Call, Lo, Hi);
1152 }
1153
ExpandFloatRes_FPOWI(SDNode * N,SDValue & Lo,SDValue & Hi)1154 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1155 SDValue &Lo, SDValue &Hi) {
1156 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1157 RTLIB::POWI_F32, RTLIB::POWI_F64,
1158 RTLIB::POWI_F80, RTLIB::POWI_F128,
1159 RTLIB::POWI_PPCF128),
1160 N, false);
1161 GetPairElements(Call, Lo, Hi);
1162 }
1163
ExpandFloatRes_FREM(SDNode * N,SDValue & Lo,SDValue & Hi)1164 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1165 SDValue &Lo, SDValue &Hi) {
1166 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1167 RTLIB::REM_F32, RTLIB::REM_F64,
1168 RTLIB::REM_F80, RTLIB::REM_F128,
1169 RTLIB::REM_PPCF128),
1170 N, false);
1171 GetPairElements(Call, Lo, Hi);
1172 }
1173
ExpandFloatRes_FRINT(SDNode * N,SDValue & Lo,SDValue & Hi)1174 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1175 SDValue &Lo, SDValue &Hi) {
1176 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1177 RTLIB::RINT_F32, RTLIB::RINT_F64,
1178 RTLIB::RINT_F80, RTLIB::RINT_F128,
1179 RTLIB::RINT_PPCF128),
1180 N, false);
1181 GetPairElements(Call, Lo, Hi);
1182 }
1183
ExpandFloatRes_FROUND(SDNode * N,SDValue & Lo,SDValue & Hi)1184 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1185 SDValue &Lo, SDValue &Hi) {
1186 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1187 RTLIB::ROUND_F32,
1188 RTLIB::ROUND_F64,
1189 RTLIB::ROUND_F80,
1190 RTLIB::ROUND_F128,
1191 RTLIB::ROUND_PPCF128),
1192 N, false);
1193 GetPairElements(Call, Lo, Hi);
1194 }
1195
ExpandFloatRes_FSIN(SDNode * N,SDValue & Lo,SDValue & Hi)1196 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1197 SDValue &Lo, SDValue &Hi) {
1198 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1199 RTLIB::SIN_F32, RTLIB::SIN_F64,
1200 RTLIB::SIN_F80, RTLIB::SIN_F128,
1201 RTLIB::SIN_PPCF128),
1202 N, false);
1203 GetPairElements(Call, Lo, Hi);
1204 }
1205
ExpandFloatRes_FSQRT(SDNode * N,SDValue & Lo,SDValue & Hi)1206 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1207 SDValue &Lo, SDValue &Hi) {
1208 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1209 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1210 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1211 RTLIB::SQRT_PPCF128),
1212 N, false);
1213 GetPairElements(Call, Lo, Hi);
1214 }
1215
ExpandFloatRes_FSUB(SDNode * N,SDValue & Lo,SDValue & Hi)1216 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1217 SDValue &Hi) {
1218 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1219 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1220 RTLIB::SUB_F32,
1221 RTLIB::SUB_F64,
1222 RTLIB::SUB_F80,
1223 RTLIB::SUB_F128,
1224 RTLIB::SUB_PPCF128),
1225 N->getValueType(0), Ops, 2, false,
1226 SDLoc(N)).first;
1227 GetPairElements(Call, Lo, Hi);
1228 }
1229
ExpandFloatRes_FTRUNC(SDNode * N,SDValue & Lo,SDValue & Hi)1230 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1231 SDValue &Lo, SDValue &Hi) {
1232 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1233 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1234 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1235 RTLIB::TRUNC_PPCF128),
1236 N, false);
1237 GetPairElements(Call, Lo, Hi);
1238 }
1239
ExpandFloatRes_LOAD(SDNode * N,SDValue & Lo,SDValue & Hi)1240 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1241 SDValue &Hi) {
1242 if (ISD::isNormalLoad(N)) {
1243 ExpandRes_NormalLoad(N, Lo, Hi);
1244 return;
1245 }
1246
1247 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1248 LoadSDNode *LD = cast<LoadSDNode>(N);
1249 SDValue Chain = LD->getChain();
1250 SDValue Ptr = LD->getBasePtr();
1251 SDLoc dl(N);
1252
1253 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1254 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1255 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1256
1257 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1258 LD->getMemoryVT(), LD->getMemOperand());
1259
1260 // Remember the chain.
1261 Chain = Hi.getValue(1);
1262
1263 // The low part is zero.
1264 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1265 APInt(NVT.getSizeInBits(), 0)), NVT);
1266
1267 // Modified the chain - switch anything that used the old chain to use the
1268 // new one.
1269 ReplaceValueWith(SDValue(LD, 1), Chain);
1270 }
1271
ExpandFloatRes_XINT_TO_FP(SDNode * N,SDValue & Lo,SDValue & Hi)1272 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1273 SDValue &Hi) {
1274 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1275 EVT VT = N->getValueType(0);
1276 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1277 SDValue Src = N->getOperand(0);
1278 EVT SrcVT = Src.getValueType();
1279 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1280 SDLoc dl(N);
1281
1282 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1283 // When promoting partial word types to i32 we must honor the signedness,
1284 // though.
1285 if (SrcVT.bitsLE(MVT::i32)) {
1286 // The integer can be represented exactly in an f64.
1287 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1288 MVT::i32, Src);
1289 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1290 APInt(NVT.getSizeInBits(), 0)), NVT);
1291 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1292 } else {
1293 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1294 if (SrcVT.bitsLE(MVT::i64)) {
1295 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1296 MVT::i64, Src);
1297 LC = RTLIB::SINTTOFP_I64_PPCF128;
1298 } else if (SrcVT.bitsLE(MVT::i128)) {
1299 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1300 LC = RTLIB::SINTTOFP_I128_PPCF128;
1301 }
1302 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1303
1304 Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl).first;
1305 GetPairElements(Hi, Lo, Hi);
1306 }
1307
1308 if (isSigned)
1309 return;
1310
1311 // Unsigned - fix up the SINT_TO_FP value just calculated.
1312 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1313 SrcVT = Src.getValueType();
1314
1315 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1316 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1317 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1318 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1319 ArrayRef<uint64_t> Parts;
1320
1321 switch (SrcVT.getSimpleVT().SimpleTy) {
1322 default:
1323 llvm_unreachable("Unsupported UINT_TO_FP!");
1324 case MVT::i32:
1325 Parts = TwoE32;
1326 break;
1327 case MVT::i64:
1328 Parts = TwoE64;
1329 break;
1330 case MVT::i128:
1331 Parts = TwoE128;
1332 break;
1333 }
1334
1335 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1336 DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1337 APInt(128, Parts)),
1338 MVT::ppcf128));
1339 Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, SrcVT),
1340 Lo, Hi, ISD::SETLT);
1341 GetPairElements(Lo, Lo, Hi);
1342 }
1343
1344
1345 //===----------------------------------------------------------------------===//
1346 // Float Operand Expansion
1347 //===----------------------------------------------------------------------===//
1348
1349 /// ExpandFloatOperand - This method is called when the specified operand of the
1350 /// specified node is found to need expansion. At this point, all of the result
1351 /// types of the node are known to be legal, but other operands of the node may
1352 /// need promotion or expansion as well as the specified one.
ExpandFloatOperand(SDNode * N,unsigned OpNo)1353 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1354 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1355 SDValue Res = SDValue();
1356
1357 // See if the target wants to custom expand this node.
1358 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1359 return false;
1360
1361 switch (N->getOpcode()) {
1362 default:
1363 #ifndef NDEBUG
1364 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1365 N->dump(&DAG); dbgs() << "\n";
1366 #endif
1367 llvm_unreachable("Do not know how to expand this operator's operand!");
1368
1369 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1370 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1371 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1372
1373 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1374 case ISD::FCOPYSIGN: Res = ExpandFloatOp_FCOPYSIGN(N); break;
1375 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1376 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1377 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1378 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1379 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1380 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1381 OpNo); break;
1382 }
1383
1384 // If the result is null, the sub-method took care of registering results etc.
1385 if (!Res.getNode()) return false;
1386
1387 // If the result is N, the sub-method updated N in place. Tell the legalizer
1388 // core about this.
1389 if (Res.getNode() == N)
1390 return true;
1391
1392 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1393 "Invalid operand expansion");
1394
1395 ReplaceValueWith(SDValue(N, 0), Res);
1396 return false;
1397 }
1398
1399 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1400 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
FloatExpandSetCCOperands(SDValue & NewLHS,SDValue & NewRHS,ISD::CondCode & CCCode,SDLoc dl)1401 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1402 SDValue &NewRHS,
1403 ISD::CondCode &CCCode,
1404 SDLoc dl) {
1405 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1406 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1407 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1408
1409 assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1410
1411 // FIXME: This generated code sucks. We want to generate
1412 // FCMPU crN, hi1, hi2
1413 // BNE crN, L:
1414 // FCMPU crN, lo1, lo2
1415 // The following can be improved, but not that much.
1416 SDValue Tmp1, Tmp2, Tmp3;
1417 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1418 LHSHi, RHSHi, ISD::SETOEQ);
1419 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1420 LHSLo, RHSLo, CCCode);
1421 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1422 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1423 LHSHi, RHSHi, ISD::SETUNE);
1424 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1425 LHSHi, RHSHi, CCCode);
1426 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1427 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1428 NewRHS = SDValue(); // LHS is the result, not a compare.
1429 }
1430
ExpandFloatOp_BR_CC(SDNode * N)1431 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1432 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1433 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1434 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1435
1436 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1437 // against zero to select between true and false values.
1438 if (!NewRHS.getNode()) {
1439 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1440 CCCode = ISD::SETNE;
1441 }
1442
1443 // Update N to have the operands specified.
1444 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1445 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1446 N->getOperand(4)), 0);
1447 }
1448
ExpandFloatOp_FCOPYSIGN(SDNode * N)1449 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1450 assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1451 "Logic only correct for ppcf128!");
1452 SDValue Lo, Hi;
1453 GetExpandedFloat(N->getOperand(1), Lo, Hi);
1454 // The ppcf128 value is providing only the sign; take it from the
1455 // higher-order double (which must have the larger magnitude).
1456 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1457 N->getValueType(0), N->getOperand(0), Hi);
1458 }
1459
ExpandFloatOp_FP_ROUND(SDNode * N)1460 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1461 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1462 "Logic only correct for ppcf128!");
1463 SDValue Lo, Hi;
1464 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1465 // Round it the rest of the way (e.g. to f32) if needed.
1466 return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1467 N->getValueType(0), Hi, N->getOperand(1));
1468 }
1469
ExpandFloatOp_FP_TO_SINT(SDNode * N)1470 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1471 EVT RVT = N->getValueType(0);
1472 SDLoc dl(N);
1473
1474 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1475 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1476 if (RVT == MVT::i32) {
1477 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1478 "Logic only correct for ppcf128!");
1479 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1480 N->getOperand(0), DAG.getValueType(MVT::f64));
1481 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1482 DAG.getIntPtrConstant(1));
1483 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1484 }
1485
1486 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1487 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1488 return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl).first;
1489 }
1490
ExpandFloatOp_FP_TO_UINT(SDNode * N)1491 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1492 EVT RVT = N->getValueType(0);
1493 SDLoc dl(N);
1494
1495 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1496 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1497 if (RVT == MVT::i32) {
1498 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1499 "Logic only correct for ppcf128!");
1500 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1501 APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1502 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1503 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1504 // FIXME: generated code sucks.
1505 return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1506 DAG.getNode(ISD::ADD, dl, MVT::i32,
1507 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1508 DAG.getNode(ISD::FSUB, dl,
1509 MVT::ppcf128,
1510 N->getOperand(0),
1511 Tmp)),
1512 DAG.getConstant(0x80000000, MVT::i32)),
1513 DAG.getNode(ISD::FP_TO_SINT, dl,
1514 MVT::i32, N->getOperand(0)),
1515 ISD::SETGE);
1516 }
1517
1518 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1519 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1520 return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1521 false, dl).first;
1522 }
1523
ExpandFloatOp_SELECT_CC(SDNode * N)1524 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1525 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1526 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1527 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1528
1529 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1530 // against zero to select between true and false values.
1531 if (!NewRHS.getNode()) {
1532 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1533 CCCode = ISD::SETNE;
1534 }
1535
1536 // Update N to have the operands specified.
1537 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1538 N->getOperand(2), N->getOperand(3),
1539 DAG.getCondCode(CCCode)), 0);
1540 }
1541
ExpandFloatOp_SETCC(SDNode * N)1542 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1543 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1544 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1545 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1546
1547 // If ExpandSetCCOperands returned a scalar, use it.
1548 if (!NewRHS.getNode()) {
1549 assert(NewLHS.getValueType() == N->getValueType(0) &&
1550 "Unexpected setcc expansion!");
1551 return NewLHS;
1552 }
1553
1554 // Otherwise, update N to have the operands specified.
1555 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1556 DAG.getCondCode(CCCode)), 0);
1557 }
1558
ExpandFloatOp_STORE(SDNode * N,unsigned OpNo)1559 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1560 if (ISD::isNormalStore(N))
1561 return ExpandOp_NormalStore(N, OpNo);
1562
1563 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1564 assert(OpNo == 1 && "Can only expand the stored value so far");
1565 StoreSDNode *ST = cast<StoreSDNode>(N);
1566
1567 SDValue Chain = ST->getChain();
1568 SDValue Ptr = ST->getBasePtr();
1569
1570 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1571 ST->getValue().getValueType());
1572 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1573 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1574 (void)NVT;
1575
1576 SDValue Lo, Hi;
1577 GetExpandedOp(ST->getValue(), Lo, Hi);
1578
1579 return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1580 ST->getMemoryVT(), ST->getMemOperand());
1581 }
1582
1583 //===----------------------------------------------------------------------===//
1584 // Float Operand Promotion
1585 //===----------------------------------------------------------------------===//
1586 //
1587
GetPromotionOpcode(EVT OpVT,EVT RetVT)1588 static ISD::NodeType GetPromotionOpcode(EVT OpVT, EVT RetVT) {
1589 if (OpVT == MVT::f16) {
1590 return ISD::FP16_TO_FP;
1591 } else if (RetVT == MVT::f16) {
1592 return ISD::FP_TO_FP16;
1593 }
1594
1595 report_fatal_error("Attempt at an invalid promotion-related conversion");
1596 }
1597
PromoteFloatOperand(SDNode * N,unsigned OpNo)1598 bool DAGTypeLegalizer::PromoteFloatOperand(SDNode *N, unsigned OpNo) {
1599 SDValue R = SDValue();
1600
1601 // Nodes that use a promotion-requiring floating point operand, but doesn't
1602 // produce a promotion-requiring floating point result, need to be legalized
1603 // to use the promoted float operand. Nodes that produce at least one
1604 // promotion-requiring floating point result have their operands legalized as
1605 // a part of PromoteFloatResult.
1606 switch (N->getOpcode()) {
1607 default:
1608 llvm_unreachable("Do not know how to promote this operator's operand!");
1609
1610 case ISD::BITCAST: R = PromoteFloatOp_BITCAST(N, OpNo); break;
1611 case ISD::FCOPYSIGN: R = PromoteFloatOp_FCOPYSIGN(N, OpNo); break;
1612 case ISD::FP_TO_SINT:
1613 case ISD::FP_TO_UINT: R = PromoteFloatOp_FP_TO_XINT(N, OpNo); break;
1614 case ISD::FP_EXTEND: R = PromoteFloatOp_FP_EXTEND(N, OpNo); break;
1615 case ISD::SELECT_CC: R = PromoteFloatOp_SELECT_CC(N, OpNo); break;
1616 case ISD::SETCC: R = PromoteFloatOp_SETCC(N, OpNo); break;
1617 case ISD::STORE: R = PromoteFloatOp_STORE(N, OpNo); break;
1618 }
1619
1620 if (R.getNode())
1621 ReplaceValueWith(SDValue(N, 0), R);
1622 return false;
1623 }
1624
PromoteFloatOp_BITCAST(SDNode * N,unsigned OpNo)1625 SDValue DAGTypeLegalizer::PromoteFloatOp_BITCAST(SDNode *N, unsigned OpNo) {
1626 SDValue Op = N->getOperand(0);
1627 EVT OpVT = Op->getValueType(0);
1628
1629 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
1630 assert (IVT == N->getValueType(0) && "Bitcast to type of different size");
1631
1632 SDValue Promoted = GetPromotedFloat(N->getOperand(0));
1633 EVT PromotedVT = Promoted->getValueType(0);
1634
1635 // Convert the promoted float value to the desired IVT.
1636 return DAG.getNode(GetPromotionOpcode(PromotedVT, OpVT), SDLoc(N), IVT,
1637 Promoted);
1638 }
1639
1640 // Promote Operand 1 of FCOPYSIGN. Operand 0 ought to be handled by
1641 // PromoteFloatRes_FCOPYSIGN.
PromoteFloatOp_FCOPYSIGN(SDNode * N,unsigned OpNo)1642 SDValue DAGTypeLegalizer::PromoteFloatOp_FCOPYSIGN(SDNode *N, unsigned OpNo) {
1643 assert (OpNo == 1 && "Only Operand 1 must need promotion here");
1644 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1645
1646 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1647 N->getOperand(0), Op1);
1648 }
1649
1650 // Convert the promoted float value to the desired integer type
PromoteFloatOp_FP_TO_XINT(SDNode * N,unsigned OpNo)1651 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_TO_XINT(SDNode *N, unsigned OpNo) {
1652 SDValue Op = GetPromotedFloat(N->getOperand(0));
1653 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), Op);
1654 }
1655
PromoteFloatOp_FP_EXTEND(SDNode * N,unsigned OpNo)1656 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_EXTEND(SDNode *N, unsigned OpNo) {
1657 SDValue Op = GetPromotedFloat(N->getOperand(0));
1658 EVT VT = N->getValueType(0);
1659
1660 // Desired VT is same as promoted type. Use promoted float directly.
1661 if (VT == Op->getValueType(0))
1662 return Op;
1663
1664 // Else, extend the promoted float value to the desired VT.
1665 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Op);
1666 }
1667
1668 // Promote the float operands used for comparison. The true- and false-
1669 // operands have the same type as the result and are promoted, if needed, by
1670 // PromoteFloatRes_SELECT_CC
PromoteFloatOp_SELECT_CC(SDNode * N,unsigned OpNo)1671 SDValue DAGTypeLegalizer::PromoteFloatOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1672 SDValue LHS = GetPromotedFloat(N->getOperand(0));
1673 SDValue RHS = GetPromotedFloat(N->getOperand(1));
1674
1675 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1676 LHS, RHS, N->getOperand(2), N->getOperand(3),
1677 N->getOperand(4));
1678 }
1679
1680 // Construct a SETCC that compares the promoted values and sets the conditional
1681 // code.
PromoteFloatOp_SETCC(SDNode * N,unsigned OpNo)1682 SDValue DAGTypeLegalizer::PromoteFloatOp_SETCC(SDNode *N, unsigned OpNo) {
1683 EVT VT = N->getValueType(0);
1684 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1685 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1686 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1687 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1688
1689 return DAG.getSetCC(SDLoc(N), NVT, Op0, Op1, CCCode);
1690
1691 }
1692
1693 // Lower the promoted Float down to the integer value of same size and construct
1694 // a STORE of the integer value.
PromoteFloatOp_STORE(SDNode * N,unsigned OpNo)1695 SDValue DAGTypeLegalizer::PromoteFloatOp_STORE(SDNode *N, unsigned OpNo) {
1696 StoreSDNode *ST = cast<StoreSDNode>(N);
1697 SDValue Val = ST->getValue();
1698 SDLoc DL(N);
1699
1700 SDValue Promoted = GetPromotedFloat(Val);
1701 EVT VT = ST->getOperand(1)->getValueType(0);
1702 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1703
1704 SDValue NewVal;
1705 NewVal = DAG.getNode(GetPromotionOpcode(Promoted.getValueType(), VT), DL,
1706 IVT, Promoted);
1707
1708 return DAG.getStore(ST->getChain(), DL, NewVal, ST->getBasePtr(),
1709 ST->getMemOperand());
1710 }
1711
1712 //===----------------------------------------------------------------------===//
1713 // Float Result Promotion
1714 //===----------------------------------------------------------------------===//
1715
PromoteFloatResult(SDNode * N,unsigned ResNo)1716 void DAGTypeLegalizer::PromoteFloatResult(SDNode *N, unsigned ResNo) {
1717 SDValue R = SDValue();
1718
1719 switch (N->getOpcode()) {
1720 // These opcodes cannot appear if promotion of FP16 is done in the backend
1721 // instead of Clang
1722 case ISD::FP16_TO_FP:
1723 case ISD::FP_TO_FP16:
1724 default:
1725 llvm_unreachable("Do not know how to promote this operator's result!");
1726
1727 case ISD::BITCAST: R = PromoteFloatRes_BITCAST(N); break;
1728 case ISD::ConstantFP: R = PromoteFloatRes_ConstantFP(N); break;
1729 case ISD::EXTRACT_VECTOR_ELT:
1730 R = PromoteFloatRes_EXTRACT_VECTOR_ELT(N); break;
1731 case ISD::FCOPYSIGN: R = PromoteFloatRes_FCOPYSIGN(N); break;
1732
1733 // Unary FP Operations
1734 case ISD::FABS:
1735 case ISD::FCEIL:
1736 case ISD::FCOS:
1737 case ISD::FEXP:
1738 case ISD::FEXP2:
1739 case ISD::FFLOOR:
1740 case ISD::FLOG:
1741 case ISD::FLOG2:
1742 case ISD::FLOG10:
1743 case ISD::FNEARBYINT:
1744 case ISD::FNEG:
1745 case ISD::FRINT:
1746 case ISD::FROUND:
1747 case ISD::FSIN:
1748 case ISD::FSQRT:
1749 case ISD::FTRUNC: R = PromoteFloatRes_UnaryOp(N); break;
1750
1751 // Binary FP Operations
1752 case ISD::FADD:
1753 case ISD::FDIV:
1754 case ISD::FMAXNUM:
1755 case ISD::FMINNUM:
1756 case ISD::FMUL:
1757 case ISD::FPOW:
1758 case ISD::FREM:
1759 case ISD::FSUB: R = PromoteFloatRes_BinOp(N); break;
1760
1761 case ISD::FMA: // FMA is same as FMAD
1762 case ISD::FMAD: R = PromoteFloatRes_FMAD(N); break;
1763
1764 case ISD::FPOWI: R = PromoteFloatRes_FPOWI(N); break;
1765
1766 case ISD::FP_ROUND: R = PromoteFloatRes_FP_ROUND(N); break;
1767 case ISD::LOAD: R = PromoteFloatRes_LOAD(N); break;
1768 case ISD::SELECT: R = PromoteFloatRes_SELECT(N); break;
1769 case ISD::SELECT_CC: R = PromoteFloatRes_SELECT_CC(N); break;
1770
1771 case ISD::SINT_TO_FP:
1772 case ISD::UINT_TO_FP: R = PromoteFloatRes_XINT_TO_FP(N); break;
1773 case ISD::UNDEF: R = PromoteFloatRes_UNDEF(N); break;
1774
1775 }
1776
1777 if (R.getNode())
1778 SetPromotedFloat(SDValue(N, ResNo), R);
1779 }
1780
1781 // Bitcast from i16 to f16: convert the i16 to a f32 value instead.
1782 // At this point, it is not possible to determine if the bitcast value is
1783 // eventually stored to memory or promoted to f32 or promoted to a floating
1784 // point at a higher precision. Some of these cases are handled by FP_EXTEND,
1785 // STORE promotion handlers.
PromoteFloatRes_BITCAST(SDNode * N)1786 SDValue DAGTypeLegalizer::PromoteFloatRes_BITCAST(SDNode *N) {
1787 EVT VT = N->getValueType(0);
1788 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1789 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT,
1790 N->getOperand(0));
1791 }
1792
PromoteFloatRes_ConstantFP(SDNode * N)1793 SDValue DAGTypeLegalizer::PromoteFloatRes_ConstantFP(SDNode *N) {
1794 ConstantFPSDNode *CFPNode = cast<ConstantFPSDNode>(N);
1795 EVT VT = N->getValueType(0);
1796
1797 // Get the (bit-cast) APInt of the APFloat and build an integer constant
1798 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1799 SDValue C = DAG.getConstant(CFPNode->getValueAPF().bitcastToAPInt(),
1800 IVT);
1801
1802 // Convert the Constant to the desired FP type
1803 // FIXME We might be able to do the conversion during compilation and get rid
1804 // of it from the object code
1805 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1806 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, C);
1807 }
1808
1809 // If the Index operand is a constant, try to redirect the extract operation to
1810 // the correct legalized vector. If not, bit-convert the input vector to
1811 // equivalent integer vector. Extract the element as an (bit-cast) integer
1812 // value and convert it to the promoted type.
PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode * N)1813 SDValue DAGTypeLegalizer::PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
1814 SDLoc DL(N);
1815
1816 // If the index is constant, try to extract the value from the legalized
1817 // vector type.
1818 if (isa<ConstantSDNode>(N->getOperand(1))) {
1819 SDValue Vec = N->getOperand(0);
1820 SDValue Idx = N->getOperand(1);
1821 EVT VecVT = Vec->getValueType(0);
1822 EVT EltVT = VecVT.getVectorElementType();
1823
1824 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1825
1826 switch (getTypeAction(VecVT)) {
1827 default: break;
1828 case TargetLowering::TypeScalarizeVector: {
1829 SDValue Res = GetScalarizedVector(N->getOperand(0));
1830 ReplaceValueWith(SDValue(N, 0), Res);
1831 return SDValue();
1832 }
1833 case TargetLowering::TypeWidenVector: {
1834 Vec = GetWidenedVector(Vec);
1835 SDValue Res = DAG.getNode(N->getOpcode(), DL, EltVT, Vec, Idx);
1836 ReplaceValueWith(SDValue(N, 0), Res);
1837 return SDValue();
1838 }
1839 case TargetLowering::TypeSplitVector: {
1840 SDValue Lo, Hi;
1841 GetSplitVector(Vec, Lo, Hi);
1842
1843 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1844 SDValue Res;
1845 if (IdxVal < LoElts)
1846 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Lo, Idx);
1847 else
1848 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Hi,
1849 DAG.getConstant(IdxVal - LoElts,
1850 Idx.getValueType()));
1851 ReplaceValueWith(SDValue(N, 0), Res);
1852 return SDValue();
1853 }
1854
1855 }
1856 }
1857
1858 // Bit-convert the input vector to the equivalent integer vector
1859 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
1860 EVT IVT = NewOp.getValueType().getVectorElementType();
1861
1862 // Extract the element as an (bit-cast) integer value
1863 SDValue NewVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IVT,
1864 NewOp, N->getOperand(1));
1865
1866 // Convert the element to the desired FP type
1867 EVT VT = N->getValueType(0);
1868 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1869 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, NewVal);
1870 }
1871
1872 // FCOPYSIGN(X, Y) returns the value of X with the sign of Y. If the result
1873 // needs promotion, so does the argument X. Note that Y, if needed, will be
1874 // handled during operand promotion.
PromoteFloatRes_FCOPYSIGN(SDNode * N)1875 SDValue DAGTypeLegalizer::PromoteFloatRes_FCOPYSIGN(SDNode *N) {
1876 EVT VT = N->getValueType(0);
1877 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1878 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1879
1880 SDValue Op1 = N->getOperand(1);
1881
1882 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1883 }
1884
1885 // Unary operation where the result and the operand have PromoteFloat type
1886 // action. Construct a new SDNode with the promoted float value of the old
1887 // operand.
PromoteFloatRes_UnaryOp(SDNode * N)1888 SDValue DAGTypeLegalizer::PromoteFloatRes_UnaryOp(SDNode *N) {
1889 EVT VT = N->getValueType(0);
1890 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1891 SDValue Op = GetPromotedFloat(N->getOperand(0));
1892
1893 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op);
1894 }
1895
1896 // Binary operations where the result and both operands have PromoteFloat type
1897 // action. Construct a new SDNode with the promoted float values of the old
1898 // operands.
PromoteFloatRes_BinOp(SDNode * N)1899 SDValue DAGTypeLegalizer::PromoteFloatRes_BinOp(SDNode *N) {
1900 EVT VT = N->getValueType(0);
1901 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1902 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1903 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1904
1905 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1906 }
1907
PromoteFloatRes_FMAD(SDNode * N)1908 SDValue DAGTypeLegalizer::PromoteFloatRes_FMAD(SDNode *N) {
1909 EVT VT = N->getValueType(0);
1910 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1911 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1912 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1913 SDValue Op2 = GetPromotedFloat(N->getOperand(2));
1914
1915 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, Op2);
1916 }
1917
1918 // Promote the Float (first) operand and retain the Integer (second) operand
PromoteFloatRes_FPOWI(SDNode * N)1919 SDValue DAGTypeLegalizer::PromoteFloatRes_FPOWI(SDNode *N) {
1920 EVT VT = N->getValueType(0);
1921 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1922 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1923 SDValue Op1 = N->getOperand(1);
1924
1925 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1926 }
1927
1928 // Explicit operation to reduce precision. Reduce the value to half precision
1929 // and promote it back to the legal type.
PromoteFloatRes_FP_ROUND(SDNode * N)1930 SDValue DAGTypeLegalizer::PromoteFloatRes_FP_ROUND(SDNode *N) {
1931 SDLoc DL(N);
1932
1933 SDValue Op = N->getOperand(0);
1934 EVT VT = N->getValueType(0);
1935 EVT OpVT = Op->getValueType(0);
1936 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1937 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1938
1939 // Round promoted float to desired precision
1940 SDValue Round = DAG.getNode(GetPromotionOpcode(OpVT, VT), DL, IVT, Op);
1941 // Promote it back to the legal output type
1942 return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, Round);
1943 }
1944
PromoteFloatRes_LOAD(SDNode * N)1945 SDValue DAGTypeLegalizer::PromoteFloatRes_LOAD(SDNode *N) {
1946 LoadSDNode *L = cast<LoadSDNode>(N);
1947 EVT VT = N->getValueType(0);
1948
1949 // Load the value as an integer value with the same number of bits
1950 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1951 SDValue newL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
1952 IVT, SDLoc(N), L->getChain(), L->getBasePtr(),
1953 L->getOffset(), L->getPointerInfo(), IVT, L->isVolatile(),
1954 L->isNonTemporal(), false, L->getAlignment(),
1955 L->getAAInfo());
1956 // Legalize the chain result by replacing uses of the old value chain with the
1957 // new one
1958 ReplaceValueWith(SDValue(N, 1), newL.getValue(1));
1959
1960 // Convert the integer value to the desired FP type
1961 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1962 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, newL);
1963 }
1964
1965 // Construct a new SELECT node with the promoted true- and false- values.
PromoteFloatRes_SELECT(SDNode * N)1966 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT(SDNode *N) {
1967 SDValue TrueVal = GetPromotedFloat(N->getOperand(1));
1968 SDValue FalseVal = GetPromotedFloat(N->getOperand(2));
1969
1970 return DAG.getNode(ISD::SELECT, SDLoc(N), TrueVal->getValueType(0),
1971 N->getOperand(0), TrueVal, FalseVal);
1972 }
1973
1974 // Construct a new SELECT_CC node with the promoted true- and false- values.
1975 // The operands used for comparison are promoted by PromoteFloatOp_SELECT_CC.
PromoteFloatRes_SELECT_CC(SDNode * N)1976 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT_CC(SDNode *N) {
1977 SDValue TrueVal = GetPromotedFloat(N->getOperand(2));
1978 SDValue FalseVal = GetPromotedFloat(N->getOperand(3));
1979
1980 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1981 N->getOperand(0), N->getOperand(1), TrueVal, FalseVal,
1982 N->getOperand(4));
1983 }
1984
1985 // Construct a SDNode that transforms the SINT or UINT operand to the promoted
1986 // float type.
PromoteFloatRes_XINT_TO_FP(SDNode * N)1987 SDValue DAGTypeLegalizer::PromoteFloatRes_XINT_TO_FP(SDNode *N) {
1988 EVT VT = N->getValueType(0);
1989 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1990 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, N->getOperand(0));
1991 }
1992
PromoteFloatRes_UNDEF(SDNode * N)1993 SDValue DAGTypeLegalizer::PromoteFloatRes_UNDEF(SDNode *N) {
1994 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
1995 N->getValueType(0)));
1996 }
1997
1998