LLVM  10.0.0svn
LegalizeFloatTypes.cpp
Go to the documentation of this file.
1 //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements float type expansion and softening for LegalizeTypes.
10 // Softening is the act of turning a computation in an illegal floating point
11 // type into a computation in an integer type of the same size; also known as
12 // "soft float". For example, turning f32 arithmetic into operations using i32.
13 // The resulting integer value is the same as what you would get by performing
14 // the floating point operation and bitcasting the result to the integer type.
15 // Expansion is the act of changing a computation in an illegal type to be a
16 // computation in two identical registers of a smaller type. For example,
17 // implementing ppcf128 arithmetic in two f64 registers.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #include "LegalizeTypes.h"
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "legalize-types"
27 
28 /// GetFPLibCall - Return the right libcall for the given floating point type.
30  RTLIB::Libcall Call_F32,
31  RTLIB::Libcall Call_F64,
32  RTLIB::Libcall Call_F80,
33  RTLIB::Libcall Call_F128,
34  RTLIB::Libcall Call_PPCF128) {
35  return
36  VT == MVT::f32 ? Call_F32 :
37  VT == MVT::f64 ? Call_F64 :
38  VT == MVT::f80 ? Call_F80 :
39  VT == MVT::f128 ? Call_F128 :
40  VT == MVT::ppcf128 ? Call_PPCF128 :
41  RTLIB::UNKNOWN_LIBCALL;
42 }
43 
44 //===----------------------------------------------------------------------===//
45 // Convert Float Results to Integer
46 //===----------------------------------------------------------------------===//
47 
48 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
49  LLVM_DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
50  dbgs() << "\n");
51  SDValue R = SDValue();
52 
53  switch (N->getOpcode()) {
54  default:
55 #ifndef NDEBUG
56  dbgs() << "SoftenFloatResult #" << ResNo << ": ";
57  N->dump(&DAG); dbgs() << "\n";
58 #endif
59  llvm_unreachable("Do not know how to soften the result of this operator!");
60 
61  case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
62  case ISD::BITCAST: R = SoftenFloatRes_BITCAST(N); break;
63  case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
64  case ISD::ConstantFP: R = SoftenFloatRes_ConstantFP(N); break;
66  R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N, ResNo); break;
67  case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
68  case ISD::FMINNUM: R = SoftenFloatRes_FMINNUM(N); break;
69  case ISD::FMAXNUM: R = SoftenFloatRes_FMAXNUM(N); break;
70  case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
71  case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
72  case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
73  case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
74  case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
75  case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
76  case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
77  case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
78  case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
79  case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
80  case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
81  case ISD::FMA: R = SoftenFloatRes_FMA(N); break;
82  case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
83  case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
84  case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
85  case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
86  case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
87  case ISD::FP16_TO_FP: R = SoftenFloatRes_FP16_TO_FP(N); break;
88  case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
89  case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
90  case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
91  case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
92  case ISD::FROUND: R = SoftenFloatRes_FROUND(N); break;
93  case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
94  case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
95  case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
96  case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
97  case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
98  case ISD::ATOMIC_SWAP: R = BitcastToInt_ATOMIC_SWAP(N); break;
99  case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
100  case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
101  case ISD::SINT_TO_FP:
102  case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
103  case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
104  case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
105  }
106 
107  // If R is null, the sub-method took care of registering the result.
108  if (R.getNode()) {
109  assert(R.getNode() != N);
110  SetSoftenedFloat(SDValue(N, ResNo), R);
111  }
112 }
113 
114 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
115  return BitConvertToInteger(N->getOperand(0));
116 }
117 
118 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
119  unsigned ResNo) {
120  SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
121  return BitConvertToInteger(Op);
122 }
123 
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 
133 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(SDNode *N) {
134  ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
135  // In ppcf128, the high 64 bits are always first in memory regardless
136  // of Endianness. LLVM's APFloat representation is not Endian sensitive,
137  // and so always converts into a 128-bit APInt in a non-Endian-sensitive
138  // way. However, APInt's are serialized in an Endian-sensitive fashion,
139  // so on big-Endian targets, the two doubles are output in the wrong
140  // order. Fix this by manually flipping the order of the high 64 bits
141  // and the low 64 bits here.
142  if (DAG.getDataLayout().isBigEndian() &&
144  uint64_t words[2] = { CN->getValueAPF().bitcastToAPInt().getRawData()[1],
145  CN->getValueAPF().bitcastToAPInt().getRawData()[0] };
146  APInt Val(128, words);
147  return DAG.getConstant(Val, SDLoc(CN),
148  TLI.getTypeToTransformTo(*DAG.getContext(),
149  CN->getValueType(0)));
150  } else {
151  return DAG.getConstant(CN->getValueAPF().bitcastToAPInt(), SDLoc(CN),
152  TLI.getTypeToTransformTo(*DAG.getContext(),
153  CN->getValueType(0)));
154  }
155 }
156 
157 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N, unsigned ResNo) {
158  SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
159  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
161  NewOp, N->getOperand(1));
162 }
163 
164 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
165  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
166  unsigned Size = NVT.getSizeInBits();
167 
168  // Mask = ~(1 << (Size-1))
169  APInt API = APInt::getAllOnesValue(Size);
170  API.clearBit(Size - 1);
171  SDValue Mask = DAG.getConstant(API, SDLoc(N), NVT);
172  SDValue Op = GetSoftenedFloat(N->getOperand(0));
173  return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
174 }
175 
176 SDValue DAGTypeLegalizer::SoftenFloatRes_FMINNUM(SDNode *N) {
177  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
178  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
179  GetSoftenedFloat(N->getOperand(1)) };
181  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
182  N->getOperand(1).getValueType() };
183  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
184  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
185  RTLIB::FMIN_F32,
186  RTLIB::FMIN_F64,
187  RTLIB::FMIN_F80,
188  RTLIB::FMIN_F128,
189  RTLIB::FMIN_PPCF128),
190  NVT, Ops, CallOptions, SDLoc(N)).first;
191 }
192 
193 SDValue DAGTypeLegalizer::SoftenFloatRes_FMAXNUM(SDNode *N) {
194  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
195  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
196  GetSoftenedFloat(N->getOperand(1)) };
198  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
199  N->getOperand(1).getValueType() };
200  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
201  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
202  RTLIB::FMAX_F32,
203  RTLIB::FMAX_F64,
204  RTLIB::FMAX_F80,
205  RTLIB::FMAX_F128,
206  RTLIB::FMAX_PPCF128),
207  NVT, Ops, CallOptions, SDLoc(N)).first;
208 }
209 
210 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
211  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
212  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
213  GetSoftenedFloat(N->getOperand(1)) };
215  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
216  N->getOperand(1).getValueType() };
217  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
218  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
219  RTLIB::ADD_F32,
220  RTLIB::ADD_F64,
221  RTLIB::ADD_F80,
222  RTLIB::ADD_F128,
223  RTLIB::ADD_PPCF128),
224  NVT, Ops, CallOptions, SDLoc(N)).first;
225 }
226 
227 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
228  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
229  SDValue Op = GetSoftenedFloat(N->getOperand(0));
231  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
232  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
233  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
234  RTLIB::CEIL_F32,
235  RTLIB::CEIL_F64,
236  RTLIB::CEIL_F80,
237  RTLIB::CEIL_F128,
238  RTLIB::CEIL_PPCF128),
239  NVT, Op, CallOptions, SDLoc(N)).first;
240 }
241 
242 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
243  SDValue LHS = GetSoftenedFloat(N->getOperand(0));
244  SDValue RHS = BitConvertToInteger(N->getOperand(1));
245  SDLoc dl(N);
246 
247  EVT LVT = LHS.getValueType();
248  EVT RVT = RHS.getValueType();
249 
250  unsigned LSize = LVT.getSizeInBits();
251  unsigned RSize = RVT.getSizeInBits();
252 
253  // First get the sign bit of second operand.
254  SDValue SignBit = DAG.getNode(
255  ISD::SHL, dl, RVT, DAG.getConstant(1, dl, RVT),
256  DAG.getConstant(RSize - 1, dl,
257  TLI.getShiftAmountTy(RVT, DAG.getDataLayout())));
258  SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
259 
260  // Shift right or sign-extend it if the two operands have different types.
261  int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
262  if (SizeDiff > 0) {
263  SignBit =
264  DAG.getNode(ISD::SRL, dl, RVT, SignBit,
265  DAG.getConstant(SizeDiff, dl,
266  TLI.getShiftAmountTy(SignBit.getValueType(),
267  DAG.getDataLayout())));
268  SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
269  } else if (SizeDiff < 0) {
270  SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
271  SignBit =
272  DAG.getNode(ISD::SHL, dl, LVT, SignBit,
273  DAG.getConstant(-SizeDiff, dl,
274  TLI.getShiftAmountTy(SignBit.getValueType(),
275  DAG.getDataLayout())));
276  }
277 
278  // Clear the sign bit of the first operand.
279  SDValue Mask = DAG.getNode(
280  ISD::SHL, dl, LVT, DAG.getConstant(1, dl, LVT),
281  DAG.getConstant(LSize - 1, dl,
282  TLI.getShiftAmountTy(LVT, DAG.getDataLayout())));
283  Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, dl, LVT));
284  LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
285 
286  // Or the value with the sign bit.
287  return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
288 }
289 
290 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
291  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
292  SDValue Op = GetSoftenedFloat(N->getOperand(0));
294  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
295  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
296  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
297  RTLIB::COS_F32,
298  RTLIB::COS_F64,
299  RTLIB::COS_F80,
300  RTLIB::COS_F128,
301  RTLIB::COS_PPCF128),
302  NVT, Op, CallOptions, SDLoc(N)).first;
303 }
304 
305 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
306  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
307  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
308  GetSoftenedFloat(N->getOperand(1)) };
310  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
311  N->getOperand(1).getValueType() };
312  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
313  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
314  RTLIB::DIV_F32,
315  RTLIB::DIV_F64,
316  RTLIB::DIV_F80,
317  RTLIB::DIV_F128,
318  RTLIB::DIV_PPCF128),
319  NVT, Ops, CallOptions, SDLoc(N)).first;
320 }
321 
322 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
323  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
324  SDValue Op = GetSoftenedFloat(N->getOperand(0));
326  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
327  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
328  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
329  RTLIB::EXP_F32,
330  RTLIB::EXP_F64,
331  RTLIB::EXP_F80,
332  RTLIB::EXP_F128,
333  RTLIB::EXP_PPCF128),
334  NVT, Op, CallOptions, SDLoc(N)).first;
335 }
336 
337 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
338  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
339  SDValue Op = GetSoftenedFloat(N->getOperand(0));
341  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
342  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
343  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
344  RTLIB::EXP2_F32,
345  RTLIB::EXP2_F64,
346  RTLIB::EXP2_F80,
347  RTLIB::EXP2_F128,
348  RTLIB::EXP2_PPCF128),
349  NVT, Op, CallOptions, SDLoc(N)).first;
350 }
351 
352 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
353  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
354  SDValue Op = GetSoftenedFloat(N->getOperand(0));
356  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
357  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
358  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
359  RTLIB::FLOOR_F32,
360  RTLIB::FLOOR_F64,
361  RTLIB::FLOOR_F80,
362  RTLIB::FLOOR_F128,
363  RTLIB::FLOOR_PPCF128),
364  NVT, Op, CallOptions, SDLoc(N)).first;
365 }
366 
367 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
368  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
369  SDValue Op = GetSoftenedFloat(N->getOperand(0));
371  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
372  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
373  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
374  RTLIB::LOG_F32,
375  RTLIB::LOG_F64,
376  RTLIB::LOG_F80,
377  RTLIB::LOG_F128,
378  RTLIB::LOG_PPCF128),
379  NVT, Op, CallOptions, SDLoc(N)).first;
380 }
381 
382 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
383  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
384  SDValue Op = GetSoftenedFloat(N->getOperand(0));
386  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
387  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
388  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
389  RTLIB::LOG2_F32,
390  RTLIB::LOG2_F64,
391  RTLIB::LOG2_F80,
392  RTLIB::LOG2_F128,
393  RTLIB::LOG2_PPCF128),
394  NVT, Op, CallOptions, SDLoc(N)).first;
395 }
396 
397 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
398  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
399  SDValue Op = GetSoftenedFloat(N->getOperand(0));
401  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
402  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
403  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
404  RTLIB::LOG10_F32,
405  RTLIB::LOG10_F64,
406  RTLIB::LOG10_F80,
407  RTLIB::LOG10_F128,
408  RTLIB::LOG10_PPCF128),
409  NVT, Op, CallOptions, SDLoc(N)).first;
410 }
411 
412 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
413  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
414  SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
415  GetSoftenedFloat(N->getOperand(1)),
416  GetSoftenedFloat(N->getOperand(2)) };
418  EVT OpsVT[3] = { N->getOperand(0).getValueType(),
419  N->getOperand(1).getValueType(),
420  N->getOperand(2).getValueType() };
421  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
422  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
423  RTLIB::FMA_F32,
424  RTLIB::FMA_F64,
425  RTLIB::FMA_F80,
426  RTLIB::FMA_F128,
427  RTLIB::FMA_PPCF128),
428  NVT, Ops, CallOptions, SDLoc(N)).first;
429 }
430 
431 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
432  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
433  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
434  GetSoftenedFloat(N->getOperand(1)) };
436  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
437  N->getOperand(1).getValueType() };
438  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
439  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
440  RTLIB::MUL_F32,
441  RTLIB::MUL_F64,
442  RTLIB::MUL_F80,
443  RTLIB::MUL_F128,
444  RTLIB::MUL_PPCF128),
445  NVT, Ops, CallOptions, SDLoc(N)).first;
446 }
447 
448 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
449  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
450  SDValue Op = GetSoftenedFloat(N->getOperand(0));
452  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
453  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
454  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
455  RTLIB::NEARBYINT_F32,
456  RTLIB::NEARBYINT_F64,
457  RTLIB::NEARBYINT_F80,
458  RTLIB::NEARBYINT_F128,
459  RTLIB::NEARBYINT_PPCF128),
460  NVT, Op, CallOptions, SDLoc(N)).first;
461 }
462 
463 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
464  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
465  SDLoc dl(N);
466 
467  EVT FloatVT = N->getValueType(0);
468  if (FloatVT == MVT::f32 || FloatVT == MVT::f64 || FloatVT == MVT::f128) {
469  // Expand Y = FNEG(X) -> Y = X ^ sign mask
470  APInt SignMask = APInt::getSignMask(NVT.getSizeInBits());
471  return DAG.getNode(ISD::XOR, dl, NVT, GetSoftenedFloat(N->getOperand(0)),
472  DAG.getConstant(SignMask, dl, NVT));
473  }
474 
475  // Expand Y = FNEG(X) -> Y = SUB -0.0, X
476  SDValue Ops[2] = { DAG.getConstantFP(-0.0, dl, N->getValueType(0)),
477  GetSoftenedFloat(N->getOperand(0)) };
479  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
480  RTLIB::SUB_F32,
481  RTLIB::SUB_F64,
482  RTLIB::SUB_F80,
483  RTLIB::SUB_F128,
484  RTLIB::SUB_PPCF128),
485  NVT, Ops, CallOptions, dl).first;
486 }
487 
488 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
489  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
490  SDValue Op = N->getOperand(0);
491 
492  // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
493  // entirely possible for both f16 and f32 to be legal, so use the fully
494  // hard-float FP_EXTEND rather than FP16_TO_FP.
495  if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
496  Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
497  if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
498  AddToWorklist(Op.getNode());
499  }
500 
501  if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat) {
502  Op = GetPromotedFloat(Op);
503  // If the promotion did the FP_EXTEND to the destination type for us,
504  // there's nothing left to do here.
505  if (Op.getValueType() == N->getValueType(0)) {
506  return BitConvertToInteger(Op);
507  }
508  }
509 
511  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
513  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
514  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
515  return TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, SDLoc(N)).first;
516 }
517 
518 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
519 // nodes?
520 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
521  EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
522  SDValue Op = N->getOperand(0);
524  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
525  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
526  SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, Op,
527  CallOptions, SDLoc(N)).first;
528  if (N->getValueType(0) == MVT::f32)
529  return Res32;
530 
531  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
533  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
534  return TLI.makeLibCall(DAG, LC, NVT, Res32, CallOptions, SDLoc(N)).first;
535 }
536 
537 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
538  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
539  SDValue Op = N->getOperand(0);
540  if (N->getValueType(0) == MVT::f16) {
541  // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
542  // storage-only type get a chance to select things.
543  return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
544  }
545 
547  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
549  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
550  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
551  return TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, SDLoc(N)).first;
552 }
553 
554 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
555  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
556  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
557  GetSoftenedFloat(N->getOperand(1)) };
559  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
560  N->getOperand(1).getValueType() };
561  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
562  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
563  RTLIB::POW_F32,
564  RTLIB::POW_F64,
565  RTLIB::POW_F80,
566  RTLIB::POW_F128,
567  RTLIB::POW_PPCF128),
568  NVT, Ops, CallOptions, SDLoc(N)).first;
569 }
570 
571 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
572  assert(N->getOperand(1).getValueType() == MVT::i32 &&
573  "Unsupported power type!");
574  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
575  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
577  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
578  N->getOperand(1).getValueType() };
579  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
580  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
581  RTLIB::POWI_F32,
582  RTLIB::POWI_F64,
583  RTLIB::POWI_F80,
584  RTLIB::POWI_F128,
585  RTLIB::POWI_PPCF128),
586  NVT, Ops, CallOptions, SDLoc(N)).first;
587 }
588 
589 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
590  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
591  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
592  GetSoftenedFloat(N->getOperand(1)) };
594  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
595  N->getOperand(1).getValueType() };
596  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
597  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
598  RTLIB::REM_F32,
599  RTLIB::REM_F64,
600  RTLIB::REM_F80,
601  RTLIB::REM_F128,
602  RTLIB::REM_PPCF128),
603  NVT, Ops, CallOptions, SDLoc(N)).first;
604 }
605 
606 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
607  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
608  SDValue Op = GetSoftenedFloat(N->getOperand(0));
610  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
611  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
612  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
613  RTLIB::RINT_F32,
614  RTLIB::RINT_F64,
615  RTLIB::RINT_F80,
616  RTLIB::RINT_F128,
617  RTLIB::RINT_PPCF128),
618  NVT, Op, CallOptions, SDLoc(N)).first;
619 }
620 
621 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
622  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
623  SDValue Op = GetSoftenedFloat(N->getOperand(0));
625  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
626  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
627  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
628  RTLIB::ROUND_F32,
629  RTLIB::ROUND_F64,
630  RTLIB::ROUND_F80,
631  RTLIB::ROUND_F128,
632  RTLIB::ROUND_PPCF128),
633  NVT, Op, CallOptions, SDLoc(N)).first;
634 }
635 
636 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
637  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
638  SDValue Op = GetSoftenedFloat(N->getOperand(0));
640  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
641  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
642  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
643  RTLIB::SIN_F32,
644  RTLIB::SIN_F64,
645  RTLIB::SIN_F80,
646  RTLIB::SIN_F128,
647  RTLIB::SIN_PPCF128),
648  NVT, Op, CallOptions, SDLoc(N)).first;
649 }
650 
651 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
652  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
653  SDValue Op = GetSoftenedFloat(N->getOperand(0));
655  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
656  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
657  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
658  RTLIB::SQRT_F32,
659  RTLIB::SQRT_F64,
660  RTLIB::SQRT_F80,
661  RTLIB::SQRT_F128,
662  RTLIB::SQRT_PPCF128),
663  NVT, Op, CallOptions, SDLoc(N)).first;
664 }
665 
666 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
667  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
668  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
669  GetSoftenedFloat(N->getOperand(1)) };
671  EVT OpsVT[2] = { N->getOperand(0).getValueType(),
672  N->getOperand(1).getValueType() };
673  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
674  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
675  RTLIB::SUB_F32,
676  RTLIB::SUB_F64,
677  RTLIB::SUB_F80,
678  RTLIB::SUB_F128,
679  RTLIB::SUB_PPCF128),
680  NVT, Ops, CallOptions, SDLoc(N)).first;
681 }
682 
683 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
684  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
685  if (N->getValueType(0) == MVT::f16)
686  return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
687 
688  SDValue Op = GetSoftenedFloat(N->getOperand(0));
690  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
691  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
692  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
693  RTLIB::TRUNC_F32,
694  RTLIB::TRUNC_F64,
695  RTLIB::TRUNC_F80,
696  RTLIB::TRUNC_F128,
697  RTLIB::TRUNC_PPCF128),
698  NVT, Op, CallOptions, SDLoc(N)).first;
699 }
700 
701 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
702  LoadSDNode *L = cast<LoadSDNode>(N);
703  EVT VT = N->getValueType(0);
704  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
705  SDLoc dl(N);
706 
707  auto MMOFlags =
708  L->getMemOperand()->getFlags() &
710  SDValue NewL;
711  if (L->getExtensionType() == ISD::NON_EXTLOAD) {
712  NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(), NVT, dl,
713  L->getChain(), L->getBasePtr(), L->getOffset(),
714  L->getPointerInfo(), NVT, L->getAlignment(), MMOFlags,
715  L->getAAInfo());
716  // Legalized the chain result - switch anything that used the old chain to
717  // use the new one.
718  if (N != NewL.getValue(1).getNode())
719  ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
720  return NewL;
721  }
722 
723  // Do a non-extending load followed by FP_EXTEND.
724  NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD, L->getMemoryVT(),
725  dl, L->getChain(), L->getBasePtr(), L->getOffset(),
726  L->getPointerInfo(), L->getMemoryVT(), L->getAlignment(),
727  MMOFlags, L->getAAInfo());
728  // Legalized the chain result - switch anything that used the old chain to
729  // use the new one.
730  ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
731  auto ExtendNode = DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL);
732  return BitConvertToInteger(ExtendNode);
733 }
734 
735 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
736  SDValue LHS = GetSoftenedFloat(N->getOperand(1));
737  SDValue RHS = GetSoftenedFloat(N->getOperand(2));
738  return DAG.getSelect(SDLoc(N),
739  LHS.getValueType(), N->getOperand(0), LHS, RHS);
740 }
741 
742 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
743  SDValue LHS = GetSoftenedFloat(N->getOperand(2));
744  SDValue RHS = GetSoftenedFloat(N->getOperand(3));
745  return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
746  LHS.getValueType(), N->getOperand(0),
747  N->getOperand(1), LHS, RHS, N->getOperand(4));
748 }
749 
750 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
751  return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
752  N->getValueType(0)));
753 }
754 
755 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
756  SDValue Chain = N->getOperand(0); // Get the chain.
757  SDValue Ptr = N->getOperand(1); // Get the pointer.
758  EVT VT = N->getValueType(0);
759  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
760  SDLoc dl(N);
761 
762  SDValue NewVAARG;
763  NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
764  N->getConstantOperandVal(3));
765 
766  // Legalized the chain result - switch anything that used the old chain to
767  // use the new one.
768  if (N != NewVAARG.getValue(1).getNode())
769  ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
770  return NewVAARG;
771 }
772 
773 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
774  bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
775  EVT SVT = N->getOperand(0).getValueType();
776  EVT RVT = N->getValueType(0);
777  EVT NVT = EVT();
778  SDLoc dl(N);
779 
780  // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
781  // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
782  // match. Look for an appropriate libcall.
783  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
784  for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
785  t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
786  NVT = (MVT::SimpleValueType)t;
787  // The source needs to big enough to hold the operand.
788  if (NVT.bitsGE(SVT))
789  LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
790  }
791  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
792 
793  // Sign/zero extend the argument if the libcall takes a larger type.
794  SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
795  NVT, N->getOperand(0));
797  CallOptions.setSExt(Signed);
798  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
799  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
800  return TLI.makeLibCall(DAG, LC,
801  TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
802  Op, CallOptions, dl).first;
803 }
804 
805 
806 //===----------------------------------------------------------------------===//
807 // Convert Float Operand to Integer
808 //===----------------------------------------------------------------------===//
809 
810 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
811  LLVM_DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
812  dbgs() << "\n");
813  SDValue Res = SDValue();
814 
815  switch (N->getOpcode()) {
816  default:
817 #ifndef NDEBUG
818  dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
819  N->dump(&DAG); dbgs() << "\n";
820 #endif
821  llvm_unreachable("Do not know how to soften this operator's operand!");
822 
823  case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
824  case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
825  case ISD::FP_EXTEND: Res = SoftenFloatOp_FP_EXTEND(N); break;
826  case ISD::FP_TO_FP16: // Same as FP_ROUND for softening purposes
827  case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
828  case ISD::FP_TO_SINT:
829  case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_XINT(N); break;
830  case ISD::LROUND: Res = SoftenFloatOp_LROUND(N); break;
831  case ISD::LLROUND: Res = SoftenFloatOp_LLROUND(N); break;
832  case ISD::LRINT: Res = SoftenFloatOp_LRINT(N); break;
833  case ISD::LLRINT: Res = SoftenFloatOp_LLRINT(N); break;
834  case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
835  case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
836  case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
837  }
838 
839  // If the result is null, the sub-method took care of registering results etc.
840  if (!Res.getNode()) return false;
841 
842  // If the result is N, the sub-method updated N in place. Tell the legalizer
843  // core about this to re-analyze.
844  if (Res.getNode() == N)
845  return true;
846 
847  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
848  "Invalid operand promotion");
849 
850  ReplaceValueWith(SDValue(N, 0), Res);
851  return false;
852 }
853 
854 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
855  SDValue Op0 = GetSoftenedFloat(N->getOperand(0));
856 
857  return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0), Op0);
858 }
859 
860 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
861  // If we get here, the result must be legal but the source illegal.
862  EVT SVT = N->getOperand(0).getValueType();
863  EVT RVT = N->getValueType(0);
864  SDValue Op = GetSoftenedFloat(N->getOperand(0));
865 
866  if (SVT == MVT::f16)
867  return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
868 
869  RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
870  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
871 
873  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
874  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
875  return TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, SDLoc(N)).first;
876 }
877 
878 
879 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
880  // We actually deal with the partially-softened FP_TO_FP16 node too, which
881  // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
883 
884  EVT SVT = N->getOperand(0).getValueType();
885  EVT RVT = N->getValueType(0);
886  EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
887 
888  RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
889  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
890 
891  SDValue Op = GetSoftenedFloat(N->getOperand(0));
893  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
894  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
895  return TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, SDLoc(N)).first;
896 }
897 
898 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
899  SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
900  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
901 
902  EVT VT = NewLHS.getValueType();
903  NewLHS = GetSoftenedFloat(NewLHS);
904  NewRHS = GetSoftenedFloat(NewRHS);
905  TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N),
906  N->getOperand(2), N->getOperand(3));
907 
908  // If softenSetCCOperands returned a scalar, we need to compare the result
909  // against zero to select between true and false values.
910  if (!NewRHS.getNode()) {
911  NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
912  CCCode = ISD::SETNE;
913  }
914 
915  // Update N to have the operands specified.
916  return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
917  DAG.getCondCode(CCCode), NewLHS, NewRHS,
918  N->getOperand(4)),
919  0);
920 }
921 
922 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT(SDNode *N) {
923  bool Signed = N->getOpcode() == ISD::FP_TO_SINT;
924  EVT SVT = N->getOperand(0).getValueType();
925  EVT RVT = N->getValueType(0);
926  EVT NVT = EVT();
927  SDLoc dl(N);
928 
929  // If the result is not legal, eg: fp -> i1, then it needs to be promoted to
930  // a larger type, eg: fp -> i32. Even if it is legal, no libcall may exactly
931  // match, eg. we don't have fp -> i8 conversions.
932  // Look for an appropriate libcall.
933  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
934  for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
935  IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
936  ++IntVT) {
937  NVT = (MVT::SimpleValueType)IntVT;
938  // The type needs to big enough to hold the result.
939  if (NVT.bitsGE(RVT))
940  LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT):RTLIB::getFPTOUINT(SVT, NVT);
941  }
942  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_XINT!");
943 
944  SDValue Op = GetSoftenedFloat(N->getOperand(0));
946  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
947  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
948  SDValue Res = TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl).first;
949 
950  // Truncate the result if the libcall returns a larger type.
951  return DAG.getNode(ISD::TRUNCATE, dl, RVT, Res);
952 }
953 
954 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
955  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
956  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
957 
958  EVT VT = NewLHS.getValueType();
959  NewLHS = GetSoftenedFloat(NewLHS);
960  NewRHS = GetSoftenedFloat(NewRHS);
961  TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N),
962  N->getOperand(0), N->getOperand(1));
963 
964  // If softenSetCCOperands returned a scalar, we need to compare the result
965  // against zero to select between true and false values.
966  if (!NewRHS.getNode()) {
967  NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
968  CCCode = ISD::SETNE;
969  }
970 
971  // Update N to have the operands specified.
972  return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
973  N->getOperand(2), N->getOperand(3),
974  DAG.getCondCode(CCCode)),
975  0);
976 }
977 
978 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
979  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
980  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
981 
982  EVT VT = NewLHS.getValueType();
983  NewLHS = GetSoftenedFloat(NewLHS);
984  NewRHS = GetSoftenedFloat(NewRHS);
985  TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N),
986  N->getOperand(0), N->getOperand(1));
987 
988  // If softenSetCCOperands returned a scalar, use it.
989  if (!NewRHS.getNode()) {
990  assert(NewLHS.getValueType() == N->getValueType(0) &&
991  "Unexpected setcc expansion!");
992  return NewLHS;
993  }
994 
995  // Otherwise, update N to have the operands specified.
996  return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
997  DAG.getCondCode(CCCode)),
998  0);
999 }
1000 
1001 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
1002  assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1003  assert(OpNo == 1 && "Can only soften the stored value!");
1004  StoreSDNode *ST = cast<StoreSDNode>(N);
1005  SDValue Val = ST->getValue();
1006  SDLoc dl(N);
1007 
1008  if (ST->isTruncatingStore())
1009  // Do an FP_ROUND followed by a non-truncating store.
1010  Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
1011  Val, DAG.getIntPtrConstant(0, dl)));
1012  else
1013  Val = GetSoftenedFloat(Val);
1014 
1015  return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
1016  ST->getMemOperand());
1017 }
1018 
1019 SDValue DAGTypeLegalizer::SoftenFloatOp_LROUND(SDNode *N) {
1020  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1021 
1022  SDValue Op = GetSoftenedFloat(N->getOperand(0));
1023  EVT RetVT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
1025  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
1026  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
1027  return TLI.makeLibCall(DAG, GetFPLibCall(RetVT,
1028  RTLIB::LROUND_F32,
1029  RTLIB::LROUND_F64,
1030  RTLIB::LROUND_F80,
1031  RTLIB::LROUND_F128,
1032  RTLIB::LROUND_PPCF128),
1033  NVT, Op, CallOptions, SDLoc(N)).first;
1034 }
1035 
1036 SDValue DAGTypeLegalizer::SoftenFloatOp_LLROUND(SDNode *N) {
1037  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1038 
1039  SDValue Op = GetSoftenedFloat(N->getOperand(0));
1040  EVT RetVT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
1042  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
1043  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
1044  return TLI.makeLibCall(DAG, GetFPLibCall(RetVT,
1045  RTLIB::LLROUND_F32,
1046  RTLIB::LLROUND_F64,
1047  RTLIB::LLROUND_F80,
1048  RTLIB::LLROUND_F128,
1049  RTLIB::LLROUND_PPCF128),
1050  NVT, Op, CallOptions, SDLoc(N)).first;
1051 }
1052 
1053 SDValue DAGTypeLegalizer::SoftenFloatOp_LRINT(SDNode *N) {
1054  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1055 
1056  SDValue Op = GetSoftenedFloat(N->getOperand(0));
1057  EVT RetVT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
1059  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
1060  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
1061  return TLI.makeLibCall(DAG, GetFPLibCall(RetVT,
1062  RTLIB::LRINT_F32,
1063  RTLIB::LRINT_F64,
1064  RTLIB::LRINT_F80,
1065  RTLIB::LRINT_F128,
1066  RTLIB::LRINT_PPCF128),
1067  NVT, Op, CallOptions, SDLoc(N)).first;
1068 }
1069 
1070 SDValue DAGTypeLegalizer::SoftenFloatOp_LLRINT(SDNode *N) {
1071  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1072 
1073  SDValue Op = GetSoftenedFloat(N->getOperand(0));
1074  EVT RetVT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
1076  EVT OpsVT[1] = { N->getOperand(0).getValueType() };
1077  CallOptions.setTypeListBeforeSoften(OpsVT, N->getValueType(0), true);
1078  return TLI.makeLibCall(DAG, GetFPLibCall(RetVT,
1079  RTLIB::LLRINT_F32,
1080  RTLIB::LLRINT_F64,
1081  RTLIB::LLRINT_F80,
1082  RTLIB::LLRINT_F128,
1083  RTLIB::LLRINT_PPCF128),
1084  NVT, Op, CallOptions, SDLoc(N)).first;
1085 }
1086 
1087 //===----------------------------------------------------------------------===//
1088 // Float Result Expansion
1089 //===----------------------------------------------------------------------===//
1090 
1091 /// ExpandFloatResult - This method is called when the specified result of the
1092 /// specified node is found to need expansion. At this point, the node may also
1093 /// have invalid operands or may have other results that need promotion, we just
1094 /// know that (at least) one result needs expansion.
1095 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
1096  LLVM_DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
1097  SDValue Lo, Hi;
1098  Lo = Hi = SDValue();
1099 
1100  // See if the target wants to custom expand this node.
1101  if (CustomLowerNode(N, N->getValueType(ResNo), true))
1102  return;
1103 
1104  switch (N->getOpcode()) {
1105  default:
1106 #ifndef NDEBUG
1107  dbgs() << "ExpandFloatResult #" << ResNo << ": ";
1108  N->dump(&DAG); dbgs() << "\n";
1109 #endif
1110  llvm_unreachable("Do not know how to expand the result of this operator!");
1111 
1112  case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
1113  case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
1114  case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
1115 
1116  case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
1117  case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
1118  case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
1119  case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
1120  case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
1121  case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
1122 
1123  case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
1124  case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
1125  case ISD::FMINNUM: ExpandFloatRes_FMINNUM(N, Lo, Hi); break;
1126  case ISD::FMAXNUM: ExpandFloatRes_FMAXNUM(N, Lo, Hi); break;
1127  case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
1128  case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
1129  case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
1130  case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
1131  case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
1132  case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
1133  case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
1134  case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
1135  case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
1136  case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
1137  case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
1138  case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
1139  case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
1140  case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
1141  case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
1142  case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
1143  case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
1144  case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
1145  case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
1146  case ISD::FROUND: ExpandFloatRes_FROUND(N, Lo, Hi); break;
1147  case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
1148  case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
1149  case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
1150  case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
1151  case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
1152  case ISD::SINT_TO_FP:
1153  case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
1154  case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
1155  }
1156 
1157  // If Lo/Hi is null, the sub-method took care of registering results etc.
1158  if (Lo.getNode())
1159  SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
1160 }
1161 
1162 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
1163  SDValue &Hi) {
1164  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1165  assert(NVT.getSizeInBits() == 64 &&
1166  "Do not know how to expand this float constant!");
1167  APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
1168  SDLoc dl(N);
1169  Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1170  APInt(64, C.getRawData()[1])),
1171  dl, NVT);
1172  Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1173  APInt(64, C.getRawData()[0])),
1174  dl, NVT);
1175 }
1176 
1177 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
1178  SDValue &Hi) {
1179  assert(N->getValueType(0) == MVT::ppcf128 &&
1180  "Logic only correct for ppcf128!");
1181  SDLoc dl(N);
1182  SDValue Tmp;
1183  GetExpandedFloat(N->getOperand(0), Lo, Tmp);
1184  Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
1185  // Lo = Hi==fabs(Hi) ? Lo : -Lo;
1186  Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
1187  DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
1188  ISD::SETEQ);
1189 }
1190 
1191 void DAGTypeLegalizer::ExpandFloatRes_FMINNUM(SDNode *N, SDValue &Lo,
1192  SDValue &Hi) {
1193  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1194  RTLIB::FMIN_F32, RTLIB::FMIN_F64,
1195  RTLIB::FMIN_F80, RTLIB::FMIN_F128,
1196  RTLIB::FMIN_PPCF128),
1197  N, false);
1198  GetPairElements(Call, Lo, Hi);
1199 }
1200 
1201 void DAGTypeLegalizer::ExpandFloatRes_FMAXNUM(SDNode *N, SDValue &Lo,
1202  SDValue &Hi) {
1203  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1204  RTLIB::FMAX_F32, RTLIB::FMAX_F64,
1205  RTLIB::FMAX_F80, RTLIB::FMAX_F128,
1206  RTLIB::FMAX_PPCF128),
1207  N, false);
1208  GetPairElements(Call, Lo, Hi);
1209 }
1210 
1211 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
1212  SDValue &Hi) {
1213  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1214  RTLIB::ADD_F32, RTLIB::ADD_F64,
1215  RTLIB::ADD_F80, RTLIB::ADD_F128,
1216  RTLIB::ADD_PPCF128),
1217  N, false);
1218  GetPairElements(Call, Lo, Hi);
1219 }
1220 
1221 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
1222  SDValue &Lo, SDValue &Hi) {
1223  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1224  RTLIB::CEIL_F32, RTLIB::CEIL_F64,
1225  RTLIB::CEIL_F80, RTLIB::CEIL_F128,
1226  RTLIB::CEIL_PPCF128),
1227  N, false);
1228  GetPairElements(Call, Lo, Hi);
1229 }
1230 
1231 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
1232  SDValue &Lo, SDValue &Hi) {
1233  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1234  RTLIB::COPYSIGN_F32,
1235  RTLIB::COPYSIGN_F64,
1236  RTLIB::COPYSIGN_F80,
1237  RTLIB::COPYSIGN_F128,
1238  RTLIB::COPYSIGN_PPCF128),
1239  N, false);
1240  GetPairElements(Call, Lo, Hi);
1241 }
1242 
1243 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
1244  SDValue &Lo, SDValue &Hi) {
1245  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1246  RTLIB::COS_F32, RTLIB::COS_F64,
1247  RTLIB::COS_F80, RTLIB::COS_F128,
1248  RTLIB::COS_PPCF128),
1249  N, false);
1250  GetPairElements(Call, Lo, Hi);
1251 }
1252 
1253 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
1254  SDValue &Hi) {
1255  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1257  SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1258  RTLIB::DIV_F32,
1259  RTLIB::DIV_F64,
1260  RTLIB::DIV_F80,
1261  RTLIB::DIV_F128,
1262  RTLIB::DIV_PPCF128),
1263  N->getValueType(0), Ops, CallOptions,
1264  SDLoc(N)).first;
1265  GetPairElements(Call, Lo, Hi);
1266 }
1267 
1268 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
1269  SDValue &Lo, SDValue &Hi) {
1270  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1271  RTLIB::EXP_F32, RTLIB::EXP_F64,
1272  RTLIB::EXP_F80, RTLIB::EXP_F128,
1273  RTLIB::EXP_PPCF128),
1274  N, false);
1275  GetPairElements(Call, Lo, Hi);
1276 }
1277 
1278 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
1279  SDValue &Lo, SDValue &Hi) {
1280  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1281  RTLIB::EXP2_F32, RTLIB::EXP2_F64,
1282  RTLIB::EXP2_F80, RTLIB::EXP2_F128,
1283  RTLIB::EXP2_PPCF128),
1284  N, false);
1285  GetPairElements(Call, Lo, Hi);
1286 }
1287 
1288 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
1289  SDValue &Lo, SDValue &Hi) {
1290  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1291  RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
1292  RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
1293  RTLIB::FLOOR_PPCF128),
1294  N, false);
1295  GetPairElements(Call, Lo, Hi);
1296 }
1297 
1298 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1299  SDValue &Lo, SDValue &Hi) {
1300  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1301  RTLIB::LOG_F32, RTLIB::LOG_F64,
1302  RTLIB::LOG_F80, RTLIB::LOG_F128,
1303  RTLIB::LOG_PPCF128),
1304  N, false);
1305  GetPairElements(Call, Lo, Hi);
1306 }
1307 
1308 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1309  SDValue &Lo, SDValue &Hi) {
1310  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1311  RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1312  RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1313  RTLIB::LOG2_PPCF128),
1314  N, false);
1315  GetPairElements(Call, Lo, Hi);
1316 }
1317 
1318 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1319  SDValue &Lo, SDValue &Hi) {
1320  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1321  RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1322  RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1323  RTLIB::LOG10_PPCF128),
1324  N, false);
1325  GetPairElements(Call, Lo, Hi);
1326 }
1327 
1328 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1329  SDValue &Hi) {
1330  SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1332  SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1333  RTLIB::FMA_F32,
1334  RTLIB::FMA_F64,
1335  RTLIB::FMA_F80,
1336  RTLIB::FMA_F128,
1337  RTLIB::FMA_PPCF128),
1338  N->getValueType(0), Ops, CallOptions,
1339  SDLoc(N)).first;
1340  GetPairElements(Call, Lo, Hi);
1341 }
1342 
1343 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1344  SDValue &Hi) {
1345  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1347  SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1348  RTLIB::MUL_F32,
1349  RTLIB::MUL_F64,
1350  RTLIB::MUL_F80,
1351  RTLIB::MUL_F128,
1352  RTLIB::MUL_PPCF128),
1353  N->getValueType(0), Ops, CallOptions,
1354  SDLoc(N)).first;
1355  GetPairElements(Call, Lo, Hi);
1356 }
1357 
1358 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1359  SDValue &Lo, SDValue &Hi) {
1360  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1361  RTLIB::NEARBYINT_F32,
1362  RTLIB::NEARBYINT_F64,
1363  RTLIB::NEARBYINT_F80,
1364  RTLIB::NEARBYINT_F128,
1365  RTLIB::NEARBYINT_PPCF128),
1366  N, false);
1367  GetPairElements(Call, Lo, Hi);
1368 }
1369 
1370 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1371  SDValue &Hi) {
1372  SDLoc dl(N);
1373  GetExpandedFloat(N->getOperand(0), Lo, Hi);
1374  Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1375  Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1376 }
1377 
1378 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1379  SDValue &Hi) {
1380  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1381  SDLoc dl(N);
1382  Hi = DAG.getNode(ISD::FP_EXTEND, dl, NVT, N->getOperand(0));
1383  Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1384  APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1385 }
1386 
1387 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1388  SDValue &Lo, SDValue &Hi) {
1389  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1390  RTLIB::POW_F32, RTLIB::POW_F64,
1391  RTLIB::POW_F80, RTLIB::POW_F128,
1392  RTLIB::POW_PPCF128),
1393  N, false);
1394  GetPairElements(Call, Lo, Hi);
1395 }
1396 
1397 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1398  SDValue &Lo, SDValue &Hi) {
1399  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1400  RTLIB::POWI_F32, RTLIB::POWI_F64,
1401  RTLIB::POWI_F80, RTLIB::POWI_F128,
1402  RTLIB::POWI_PPCF128),
1403  N, false);
1404  GetPairElements(Call, Lo, Hi);
1405 }
1406 
1407 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1408  SDValue &Lo, SDValue &Hi) {
1409  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1410  RTLIB::REM_F32, RTLIB::REM_F64,
1411  RTLIB::REM_F80, RTLIB::REM_F128,
1412  RTLIB::REM_PPCF128),
1413  N, false);
1414  GetPairElements(Call, Lo, Hi);
1415 }
1416 
1417 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1418  SDValue &Lo, SDValue &Hi) {
1419  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1420  RTLIB::RINT_F32, RTLIB::RINT_F64,
1421  RTLIB::RINT_F80, RTLIB::RINT_F128,
1422  RTLIB::RINT_PPCF128),
1423  N, false);
1424  GetPairElements(Call, Lo, Hi);
1425 }
1426 
1427 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1428  SDValue &Lo, SDValue &Hi) {
1429  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1430  RTLIB::ROUND_F32,
1431  RTLIB::ROUND_F64,
1432  RTLIB::ROUND_F80,
1433  RTLIB::ROUND_F128,
1434  RTLIB::ROUND_PPCF128),
1435  N, false);
1436  GetPairElements(Call, Lo, Hi);
1437 }
1438 
1439 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1440  SDValue &Lo, SDValue &Hi) {
1441  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1442  RTLIB::SIN_F32, RTLIB::SIN_F64,
1443  RTLIB::SIN_F80, RTLIB::SIN_F128,
1444  RTLIB::SIN_PPCF128),
1445  N, false);
1446  GetPairElements(Call, Lo, Hi);
1447 }
1448 
1449 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1450  SDValue &Lo, SDValue &Hi) {
1451  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1452  RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1453  RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1454  RTLIB::SQRT_PPCF128),
1455  N, false);
1456  GetPairElements(Call, Lo, Hi);
1457 }
1458 
1459 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1460  SDValue &Hi) {
1461  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1463  SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1464  RTLIB::SUB_F32,
1465  RTLIB::SUB_F64,
1466  RTLIB::SUB_F80,
1467  RTLIB::SUB_F128,
1468  RTLIB::SUB_PPCF128),
1469  N->getValueType(0), Ops, CallOptions,
1470  SDLoc(N)).first;
1471  GetPairElements(Call, Lo, Hi);
1472 }
1473 
1474 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1475  SDValue &Lo, SDValue &Hi) {
1476  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1477  RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1478  RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1479  RTLIB::TRUNC_PPCF128),
1480  N, false);
1481  GetPairElements(Call, Lo, Hi);
1482 }
1483 
1484 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1485  SDValue &Hi) {
1486  if (ISD::isNormalLoad(N)) {
1487  ExpandRes_NormalLoad(N, Lo, Hi);
1488  return;
1489  }
1490 
1491  assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1492  LoadSDNode *LD = cast<LoadSDNode>(N);
1493  SDValue Chain = LD->getChain();
1494  SDValue Ptr = LD->getBasePtr();
1495  SDLoc dl(N);
1496 
1497  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1498  assert(NVT.isByteSized() && "Expanded type not byte sized!");
1499  assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1500 
1501  Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1502  LD->getMemoryVT(), LD->getMemOperand());
1503 
1504  // Remember the chain.
1505  Chain = Hi.getValue(1);
1506 
1507  // The low part is zero.
1508  Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1509  APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1510 
1511  // Modified the chain - switch anything that used the old chain to use the
1512  // new one.
1513  ReplaceValueWith(SDValue(LD, 1), Chain);
1514 }
1515 
1516 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1517  SDValue &Hi) {
1518  assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1519  EVT VT = N->getValueType(0);
1520  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1521  SDValue Src = N->getOperand(0);
1522  EVT SrcVT = Src.getValueType();
1523  bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1524  SDLoc dl(N);
1525 
1526  // First do an SINT_TO_FP, whether the original was signed or unsigned.
1527  // When promoting partial word types to i32 we must honor the signedness,
1528  // though.
1529  if (SrcVT.bitsLE(MVT::i32)) {
1530  // The integer can be represented exactly in an f64.
1531  Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1532  MVT::i32, Src);
1533  Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1534  APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1535  Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1536  } else {
1537  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1538  if (SrcVT.bitsLE(MVT::i64)) {
1539  Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1540  MVT::i64, Src);
1541  LC = RTLIB::SINTTOFP_I64_PPCF128;
1542  } else if (SrcVT.bitsLE(MVT::i128)) {
1543  Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1544  LC = RTLIB::SINTTOFP_I128_PPCF128;
1545  }
1546  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1547 
1549  CallOptions.setSExt(true);
1550  Hi = TLI.makeLibCall(DAG, LC, VT, Src, CallOptions, dl).first;
1551  GetPairElements(Hi, Lo, Hi);
1552  }
1553 
1554  if (isSigned)
1555  return;
1556 
1557  // Unsigned - fix up the SINT_TO_FP value just calculated.
1558  Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1559  SrcVT = Src.getValueType();
1560 
1561  // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1562  static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1563  static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1564  static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1565  ArrayRef<uint64_t> Parts;
1566 
1567  switch (SrcVT.getSimpleVT().SimpleTy) {
1568  default:
1569  llvm_unreachable("Unsupported UINT_TO_FP!");
1570  case MVT::i32:
1571  Parts = TwoE32;
1572  break;
1573  case MVT::i64:
1574  Parts = TwoE64;
1575  break;
1576  case MVT::i128:
1577  Parts = TwoE128;
1578  break;
1579  }
1580 
1581  // TODO: Are there fast-math-flags to propagate to this FADD?
1582  Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1584  APInt(128, Parts)),
1585  dl, MVT::ppcf128));
1586  Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, dl, SrcVT),
1587  Lo, Hi, ISD::SETLT);
1588  GetPairElements(Lo, Lo, Hi);
1589 }
1590 
1591 
1592 //===----------------------------------------------------------------------===//
1593 // Float Operand Expansion
1594 //===----------------------------------------------------------------------===//
1595 
1596 /// ExpandFloatOperand - This method is called when the specified operand of the
1597 /// specified node is found to need expansion. At this point, all of the result
1598 /// types of the node are known to be legal, but other operands of the node may
1599 /// need promotion or expansion as well as the specified one.
1600 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1601  LLVM_DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1602  SDValue Res = SDValue();
1603 
1604  // See if the target wants to custom expand this node.
1605  if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1606  return false;
1607 
1608  switch (N->getOpcode()) {
1609  default:
1610 #ifndef NDEBUG
1611  dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1612  N->dump(&DAG); dbgs() << "\n";
1613 #endif
1614  llvm_unreachable("Do not know how to expand this operator's operand!");
1615 
1616  case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1617  case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1618  case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1619 
1620  case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1621  case ISD::FCOPYSIGN: Res = ExpandFloatOp_FCOPYSIGN(N); break;
1622  case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1623  case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1624  case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1625  case ISD::LROUND: Res = ExpandFloatOp_LROUND(N); break;
1626  case ISD::LLROUND: Res = ExpandFloatOp_LLROUND(N); break;
1627  case ISD::LRINT: Res = ExpandFloatOp_LRINT(N); break;
1628  case ISD::LLRINT: Res = ExpandFloatOp_LLRINT(N); break;
1629  case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1630  case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1631  case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1632  OpNo); break;
1633  }
1634 
1635  // If the result is null, the sub-method took care of registering results etc.
1636  if (!Res.getNode()) return false;
1637 
1638  // If the result is N, the sub-method updated N in place. Tell the legalizer
1639  // core about this.
1640  if (Res.getNode() == N)
1641  return true;
1642 
1643  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1644  "Invalid operand expansion");
1645 
1646  ReplaceValueWith(SDValue(N, 0), Res);
1647  return false;
1648 }
1649 
1650 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1651 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1652 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1653  SDValue &NewRHS,
1654  ISD::CondCode &CCCode,
1655  const SDLoc &dl) {
1656  SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1657  GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1658  GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1659 
1660  assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1661 
1662  // FIXME: This generated code sucks. We want to generate
1663  // FCMPU crN, hi1, hi2
1664  // BNE crN, L:
1665  // FCMPU crN, lo1, lo2
1666  // The following can be improved, but not that much.
1667  SDValue Tmp1, Tmp2, Tmp3;
1668  Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1669  LHSHi, RHSHi, ISD::SETOEQ);
1670  Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1671  LHSLo, RHSLo, CCCode);
1672  Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1673  Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1674  LHSHi, RHSHi, ISD::SETUNE);
1675  Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1676  LHSHi, RHSHi, CCCode);
1677  Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1678  NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1679  NewRHS = SDValue(); // LHS is the result, not a compare.
1680 }
1681 
1682 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1683  SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1684  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1685  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1686 
1687  // If ExpandSetCCOperands returned a scalar, we need to compare the result
1688  // against zero to select between true and false values.
1689  if (!NewRHS.getNode()) {
1690  NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1691  CCCode = ISD::SETNE;
1692  }
1693 
1694  // Update N to have the operands specified.
1695  return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1696  DAG.getCondCode(CCCode), NewLHS, NewRHS,
1697  N->getOperand(4)), 0);
1698 }
1699 
1700 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1702  "Logic only correct for ppcf128!");
1703  SDValue Lo, Hi;
1704  GetExpandedFloat(N->getOperand(1), Lo, Hi);
1705  // The ppcf128 value is providing only the sign; take it from the
1706  // higher-order double (which must have the larger magnitude).
1707  return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1708  N->getValueType(0), N->getOperand(0), Hi);
1709 }
1710 
1711 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1713  "Logic only correct for ppcf128!");
1714  SDValue Lo, Hi;
1715  GetExpandedFloat(N->getOperand(0), Lo, Hi);
1716  // Round it the rest of the way (e.g. to f32) if needed.
1717  return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1718  N->getValueType(0), Hi, N->getOperand(1));
1719 }
1720 
1721 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1722  EVT RVT = N->getValueType(0);
1723  SDLoc dl(N);
1724 
1726  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1728  return TLI.makeLibCall(DAG, LC, RVT, N->getOperand(0), CallOptions, dl).first;
1729 }
1730 
1731 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1732  EVT RVT = N->getValueType(0);
1733  SDLoc dl(N);
1734 
1736  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1738  return TLI.makeLibCall(DAG, LC, N->getValueType(0), N->getOperand(0),
1739  CallOptions, dl).first;
1740 }
1741 
1742 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1743  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1744  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1745  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1746 
1747  // If ExpandSetCCOperands returned a scalar, we need to compare the result
1748  // against zero to select between true and false values.
1749  if (!NewRHS.getNode()) {
1750  NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1751  CCCode = ISD::SETNE;
1752  }
1753 
1754  // Update N to have the operands specified.
1755  return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1756  N->getOperand(2), N->getOperand(3),
1757  DAG.getCondCode(CCCode)), 0);
1758 }
1759 
1760 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1761  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1762  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1763  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1764 
1765  // If ExpandSetCCOperands returned a scalar, use it.
1766  if (!NewRHS.getNode()) {
1767  assert(NewLHS.getValueType() == N->getValueType(0) &&
1768  "Unexpected setcc expansion!");
1769  return NewLHS;
1770  }
1771 
1772  // Otherwise, update N to have the operands specified.
1773  return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1774  DAG.getCondCode(CCCode)), 0);
1775 }
1776 
1777 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1778  if (ISD::isNormalStore(N))
1779  return ExpandOp_NormalStore(N, OpNo);
1780 
1781  assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1782  assert(OpNo == 1 && "Can only expand the stored value so far");
1783  StoreSDNode *ST = cast<StoreSDNode>(N);
1784 
1785  SDValue Chain = ST->getChain();
1786  SDValue Ptr = ST->getBasePtr();
1787 
1788  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1789  ST->getValue().getValueType());
1790  assert(NVT.isByteSized() && "Expanded type not byte sized!");
1791  assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1792  (void)NVT;
1793 
1794  SDValue Lo, Hi;
1795  GetExpandedOp(ST->getValue(), Lo, Hi);
1796 
1797  return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1798  ST->getMemoryVT(), ST->getMemOperand());
1799 }
1800 
1801 SDValue DAGTypeLegalizer::ExpandFloatOp_LROUND(SDNode *N) {
1802  EVT RVT = N->getValueType(0);
1803  EVT RetVT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
1805  return TLI.makeLibCall(DAG, GetFPLibCall(RetVT,
1806  RTLIB::LROUND_F32,
1807  RTLIB::LROUND_F64,
1808  RTLIB::LROUND_F80,
1809  RTLIB::LROUND_F128,
1810  RTLIB::LROUND_PPCF128),
1811  RVT, N->getOperand(0), CallOptions, SDLoc(N)).first;
1812 }
1813 
1814 SDValue DAGTypeLegalizer::ExpandFloatOp_LLROUND(SDNode *N) {
1815  EVT RVT = N->getValueType(0);
1816  EVT RetVT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
1818  return TLI.makeLibCall(DAG, GetFPLibCall(RetVT,
1819  RTLIB::LLROUND_F32,
1820  RTLIB::LLROUND_F64,
1821  RTLIB::LLROUND_F80,
1822  RTLIB::LLROUND_F128,
1823  RTLIB::LLROUND_PPCF128),
1824  RVT, N->getOperand(0), CallOptions, SDLoc(N)).first;
1825 }
1826 
1827 SDValue DAGTypeLegalizer::ExpandFloatOp_LRINT(SDNode *N) {
1828  EVT RVT = N->getValueType(0);
1829  EVT RetVT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
1831  return TLI.makeLibCall(DAG, GetFPLibCall(RetVT,
1832  RTLIB::LRINT_F32,
1833  RTLIB::LRINT_F64,
1834  RTLIB::LRINT_F80,
1835  RTLIB::LRINT_F128,
1836  RTLIB::LRINT_PPCF128),
1837  RVT, N->getOperand(0), CallOptions, SDLoc(N)).first;
1838 }
1839 
1840 SDValue DAGTypeLegalizer::ExpandFloatOp_LLRINT(SDNode *N) {
1841  EVT RVT = N->getValueType(0);
1842  EVT RetVT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
1844  return TLI.makeLibCall(DAG, GetFPLibCall(RetVT,
1845  RTLIB::LLRINT_F32,
1846  RTLIB::LLRINT_F64,
1847  RTLIB::LLRINT_F80,
1848  RTLIB::LLRINT_F128,
1849  RTLIB::LLRINT_PPCF128),
1850  RVT, N->getOperand(0), CallOptions, SDLoc(N)).first;
1851 }
1852 
1853 //===----------------------------------------------------------------------===//
1854 // Float Operand Promotion
1855 //===----------------------------------------------------------------------===//
1856 //
1857 
1859  if (OpVT == MVT::f16) {
1860  return ISD::FP16_TO_FP;
1861  } else if (RetVT == MVT::f16) {
1862  return ISD::FP_TO_FP16;
1863  }
1864 
1865  report_fatal_error("Attempt at an invalid promotion-related conversion");
1866 }
1867 
1868 bool DAGTypeLegalizer::PromoteFloatOperand(SDNode *N, unsigned OpNo) {
1869  LLVM_DEBUG(dbgs() << "Promote float operand " << OpNo << ": "; N->dump(&DAG);
1870  dbgs() << "\n");
1871  SDValue R = SDValue();
1872 
1873  if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
1874  LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
1875  return false;
1876  }
1877 
1878  // Nodes that use a promotion-requiring floating point operand, but doesn't
1879  // produce a promotion-requiring floating point result, need to be legalized
1880  // to use the promoted float operand. Nodes that produce at least one
1881  // promotion-requiring floating point result have their operands legalized as
1882  // a part of PromoteFloatResult.
1883  switch (N->getOpcode()) {
1884  default:
1885  #ifndef NDEBUG
1886  dbgs() << "PromoteFloatOperand Op #" << OpNo << ": ";
1887  N->dump(&DAG); dbgs() << "\n";
1888  #endif
1889  llvm_unreachable("Do not know how to promote this operator's operand!");
1890 
1891  case ISD::BITCAST: R = PromoteFloatOp_BITCAST(N, OpNo); break;
1892  case ISD::FCOPYSIGN: R = PromoteFloatOp_FCOPYSIGN(N, OpNo); break;
1893  case ISD::FP_TO_SINT:
1894  case ISD::FP_TO_UINT: R = PromoteFloatOp_FP_TO_XINT(N, OpNo); break;
1895  case ISD::FP_EXTEND: R = PromoteFloatOp_FP_EXTEND(N, OpNo); break;
1896  case ISD::SELECT_CC: R = PromoteFloatOp_SELECT_CC(N, OpNo); break;
1897  case ISD::SETCC: R = PromoteFloatOp_SETCC(N, OpNo); break;
1898  case ISD::STORE: R = PromoteFloatOp_STORE(N, OpNo); break;
1899  }
1900 
1901  if (R.getNode())
1902  ReplaceValueWith(SDValue(N, 0), R);
1903  return false;
1904 }
1905 
1906 SDValue DAGTypeLegalizer::PromoteFloatOp_BITCAST(SDNode *N, unsigned OpNo) {
1907  SDValue Op = N->getOperand(0);
1908  EVT OpVT = Op->getValueType(0);
1909 
1910  SDValue Promoted = GetPromotedFloat(N->getOperand(0));
1911  EVT PromotedVT = Promoted->getValueType(0);
1912 
1913  // Convert the promoted float value to the desired IVT.
1914  EVT IVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
1915  SDValue Convert = DAG.getNode(GetPromotionOpcode(PromotedVT, OpVT), SDLoc(N),
1916  IVT, Promoted);
1917  // The final result type might not be an scalar so we need a bitcast. The
1918  // bitcast will be further legalized if needed.
1919  return DAG.getBitcast(N->getValueType(0), Convert);
1920 }
1921 
1922 // Promote Operand 1 of FCOPYSIGN. Operand 0 ought to be handled by
1923 // PromoteFloatRes_FCOPYSIGN.
1924 SDValue DAGTypeLegalizer::PromoteFloatOp_FCOPYSIGN(SDNode *N, unsigned OpNo) {
1925  assert (OpNo == 1 && "Only Operand 1 must need promotion here");
1926  SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1927 
1928  return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1929  N->getOperand(0), Op1);
1930 }
1931 
1932 // Convert the promoted float value to the desired integer type
1933 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_TO_XINT(SDNode *N, unsigned OpNo) {
1934  SDValue Op = GetPromotedFloat(N->getOperand(0));
1935  return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), Op);
1936 }
1937 
1938 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_EXTEND(SDNode *N, unsigned OpNo) {
1939  SDValue Op = GetPromotedFloat(N->getOperand(0));
1940  EVT VT = N->getValueType(0);
1941 
1942  // Desired VT is same as promoted type. Use promoted float directly.
1943  if (VT == Op->getValueType(0))
1944  return Op;
1945 
1946  // Else, extend the promoted float value to the desired VT.
1947  return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Op);
1948 }
1949 
1950 // Promote the float operands used for comparison. The true- and false-
1951 // operands have the same type as the result and are promoted, if needed, by
1952 // PromoteFloatRes_SELECT_CC
1953 SDValue DAGTypeLegalizer::PromoteFloatOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1954  SDValue LHS = GetPromotedFloat(N->getOperand(0));
1955  SDValue RHS = GetPromotedFloat(N->getOperand(1));
1956 
1957  return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1958  LHS, RHS, N->getOperand(2), N->getOperand(3),
1959  N->getOperand(4));
1960 }
1961 
1962 // Construct a SETCC that compares the promoted values and sets the conditional
1963 // code.
1964 SDValue DAGTypeLegalizer::PromoteFloatOp_SETCC(SDNode *N, unsigned OpNo) {
1965  EVT VT = N->getValueType(0);
1966  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1967  SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1968  SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1969  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1970 
1971  return DAG.getSetCC(SDLoc(N), NVT, Op0, Op1, CCCode);
1972 
1973 }
1974 
1975 // Lower the promoted Float down to the integer value of same size and construct
1976 // a STORE of the integer value.
1977 SDValue DAGTypeLegalizer::PromoteFloatOp_STORE(SDNode *N, unsigned OpNo) {
1978  StoreSDNode *ST = cast<StoreSDNode>(N);
1979  SDValue Val = ST->getValue();
1980  SDLoc DL(N);
1981 
1982  SDValue Promoted = GetPromotedFloat(Val);
1983  EVT VT = ST->getOperand(1).getValueType();
1984  EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1985 
1986  SDValue NewVal;
1987  NewVal = DAG.getNode(GetPromotionOpcode(Promoted.getValueType(), VT), DL,
1988  IVT, Promoted);
1989 
1990  return DAG.getStore(ST->getChain(), DL, NewVal, ST->getBasePtr(),
1991  ST->getMemOperand());
1992 }
1993 
1994 //===----------------------------------------------------------------------===//
1995 // Float Result Promotion
1996 //===----------------------------------------------------------------------===//
1997 
1998 void DAGTypeLegalizer::PromoteFloatResult(SDNode *N, unsigned ResNo) {
1999  LLVM_DEBUG(dbgs() << "Promote float result " << ResNo << ": "; N->dump(&DAG);
2000  dbgs() << "\n");
2001  SDValue R = SDValue();
2002 
2003  // See if the target wants to custom expand this node.
2004  if (CustomLowerNode(N, N->getValueType(ResNo), true)) {
2005  LLVM_DEBUG(dbgs() << "Node has been custom expanded, done\n");
2006  return;
2007  }
2008 
2009  switch (N->getOpcode()) {
2010  // These opcodes cannot appear if promotion of FP16 is done in the backend
2011  // instead of Clang
2012  case ISD::FP16_TO_FP:
2013  case ISD::FP_TO_FP16:
2014  default:
2015 #ifndef NDEBUG
2016  dbgs() << "PromoteFloatResult #" << ResNo << ": ";
2017  N->dump(&DAG); dbgs() << "\n";
2018 #endif
2019  llvm_unreachable("Do not know how to promote this operator's result!");
2020 
2021  case ISD::BITCAST: R = PromoteFloatRes_BITCAST(N); break;
2022  case ISD::ConstantFP: R = PromoteFloatRes_ConstantFP(N); break;
2024  R = PromoteFloatRes_EXTRACT_VECTOR_ELT(N); break;
2025  case ISD::FCOPYSIGN: R = PromoteFloatRes_FCOPYSIGN(N); break;
2026 
2027  // Unary FP Operations
2028  case ISD::FABS:
2029  case ISD::FCEIL:
2030  case ISD::FCOS:
2031  case ISD::FEXP:
2032  case ISD::FEXP2:
2033  case ISD::FFLOOR:
2034  case ISD::FLOG:
2035  case ISD::FLOG2:
2036  case ISD::FLOG10:
2037  case ISD::FNEARBYINT:
2038  case ISD::FNEG:
2039  case ISD::FRINT:
2040  case ISD::FROUND:
2041  case ISD::FSIN:
2042  case ISD::FSQRT:
2043  case ISD::FTRUNC:
2044  case ISD::FCANONICALIZE: R = PromoteFloatRes_UnaryOp(N); break;
2045 
2046  // Binary FP Operations
2047  case ISD::FADD:
2048  case ISD::FDIV:
2049  case ISD::FMAXIMUM:
2050  case ISD::FMINIMUM:
2051  case ISD::FMAXNUM:
2052  case ISD::FMINNUM:
2053  case ISD::FMUL:
2054  case ISD::FPOW:
2055  case ISD::FREM:
2056  case ISD::FSUB: R = PromoteFloatRes_BinOp(N); break;
2057 
2058  case ISD::FMA: // FMA is same as FMAD
2059  case ISD::FMAD: R = PromoteFloatRes_FMAD(N); break;
2060 
2061  case ISD::FPOWI: R = PromoteFloatRes_FPOWI(N); break;
2062 
2063  case ISD::FP_ROUND: R = PromoteFloatRes_FP_ROUND(N); break;
2064  case ISD::LOAD: R = PromoteFloatRes_LOAD(N); break;
2065  case ISD::SELECT: R = PromoteFloatRes_SELECT(N); break;
2066  case ISD::SELECT_CC: R = PromoteFloatRes_SELECT_CC(N); break;
2067 
2068  case ISD::SINT_TO_FP:
2069  case ISD::UINT_TO_FP: R = PromoteFloatRes_XINT_TO_FP(N); break;
2070  case ISD::UNDEF: R = PromoteFloatRes_UNDEF(N); break;
2071  case ISD::ATOMIC_SWAP: R = BitcastToInt_ATOMIC_SWAP(N); break;
2072  }
2073 
2074  if (R.getNode())
2075  SetPromotedFloat(SDValue(N, ResNo), R);
2076 }
2077 
2078 // Bitcast from i16 to f16: convert the i16 to a f32 value instead.
2079 // At this point, it is not possible to determine if the bitcast value is
2080 // eventually stored to memory or promoted to f32 or promoted to a floating
2081 // point at a higher precision. Some of these cases are handled by FP_EXTEND,
2082 // STORE promotion handlers.
2083 SDValue DAGTypeLegalizer::PromoteFloatRes_BITCAST(SDNode *N) {
2084  EVT VT = N->getValueType(0);
2085  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2086  // Input type isn't guaranteed to be a scalar int so bitcast if not. The
2087  // bitcast will be legalized further if necessary.
2088  EVT IVT = EVT::getIntegerVT(*DAG.getContext(),
2090  SDValue Cast = DAG.getBitcast(IVT, N->getOperand(0));
2091  return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, Cast);
2092 }
2093 
2094 SDValue DAGTypeLegalizer::PromoteFloatRes_ConstantFP(SDNode *N) {
2095  ConstantFPSDNode *CFPNode = cast<ConstantFPSDNode>(N);
2096  EVT VT = N->getValueType(0);
2097  SDLoc DL(N);
2098 
2099  // Get the (bit-cast) APInt of the APFloat and build an integer constant
2100  EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
2101  SDValue C = DAG.getConstant(CFPNode->getValueAPF().bitcastToAPInt(), DL,
2102  IVT);
2103 
2104  // Convert the Constant to the desired FP type
2105  // FIXME We might be able to do the conversion during compilation and get rid
2106  // of it from the object code
2107  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2108  return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, C);
2109 }
2110 
2111 // If the Index operand is a constant, try to redirect the extract operation to
2112 // the correct legalized vector. If not, bit-convert the input vector to
2113 // equivalent integer vector. Extract the element as an (bit-cast) integer
2114 // value and convert it to the promoted type.
2115 SDValue DAGTypeLegalizer::PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
2116  SDLoc DL(N);
2117 
2118  // If the index is constant, try to extract the value from the legalized
2119  // vector type.
2120  if (isa<ConstantSDNode>(N->getOperand(1))) {
2121  SDValue Vec = N->getOperand(0);
2122  SDValue Idx = N->getOperand(1);
2123  EVT VecVT = Vec->getValueType(0);
2124  EVT EltVT = VecVT.getVectorElementType();
2125 
2126  uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
2127 
2128  switch (getTypeAction(VecVT)) {
2129  default: break;
2131  SDValue Res = GetScalarizedVector(N->getOperand(0));
2132  ReplaceValueWith(SDValue(N, 0), Res);
2133  return SDValue();
2134  }
2136  Vec = GetWidenedVector(Vec);
2137  SDValue Res = DAG.getNode(N->getOpcode(), DL, EltVT, Vec, Idx);
2138  ReplaceValueWith(SDValue(N, 0), Res);
2139  return SDValue();
2140  }
2142  SDValue Lo, Hi;
2143  GetSplitVector(Vec, Lo, Hi);
2144 
2145  uint64_t LoElts = Lo.getValueType().getVectorNumElements();
2146  SDValue Res;
2147  if (IdxVal < LoElts)
2148  Res = DAG.getNode(N->getOpcode(), DL, EltVT, Lo, Idx);
2149  else
2150  Res = DAG.getNode(N->getOpcode(), DL, EltVT, Hi,
2151  DAG.getConstant(IdxVal - LoElts, DL,
2152  Idx.getValueType()));
2153  ReplaceValueWith(SDValue(N, 0), Res);
2154  return SDValue();
2155  }
2156 
2157  }
2158  }
2159 
2160  // Bit-convert the input vector to the equivalent integer vector
2161  SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
2162  EVT IVT = NewOp.getValueType().getVectorElementType();
2163 
2164  // Extract the element as an (bit-cast) integer value
2165  SDValue NewVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IVT,
2166  NewOp, N->getOperand(1));
2167 
2168  // Convert the element to the desired FP type
2169  EVT VT = N->getValueType(0);
2170  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2171  return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, NewVal);
2172 }
2173 
2174 // FCOPYSIGN(X, Y) returns the value of X with the sign of Y. If the result
2175 // needs promotion, so does the argument X. Note that Y, if needed, will be
2176 // handled during operand promotion.
2177 SDValue DAGTypeLegalizer::PromoteFloatRes_FCOPYSIGN(SDNode *N) {
2178  EVT VT = N->getValueType(0);
2179  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2180  SDValue Op0 = GetPromotedFloat(N->getOperand(0));
2181 
2182  SDValue Op1 = N->getOperand(1);
2183 
2184  return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
2185 }
2186 
2187 // Unary operation where the result and the operand have PromoteFloat type
2188 // action. Construct a new SDNode with the promoted float value of the old
2189 // operand.
2190 SDValue DAGTypeLegalizer::PromoteFloatRes_UnaryOp(SDNode *N) {
2191  EVT VT = N->getValueType(0);
2192  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2193  SDValue Op = GetPromotedFloat(N->getOperand(0));
2194 
2195  return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op);
2196 }
2197 
2198 // Binary operations where the result and both operands have PromoteFloat type
2199 // action. Construct a new SDNode with the promoted float values of the old
2200 // operands.
2201 SDValue DAGTypeLegalizer::PromoteFloatRes_BinOp(SDNode *N) {
2202  EVT VT = N->getValueType(0);
2203  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2204  SDValue Op0 = GetPromotedFloat(N->getOperand(0));
2205  SDValue Op1 = GetPromotedFloat(N->getOperand(1));
2206  return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, N->getFlags());
2207 }
2208 
2209 SDValue DAGTypeLegalizer::PromoteFloatRes_FMAD(SDNode *N) {
2210  EVT VT = N->getValueType(0);
2211  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2212  SDValue Op0 = GetPromotedFloat(N->getOperand(0));
2213  SDValue Op1 = GetPromotedFloat(N->getOperand(1));
2214  SDValue Op2 = GetPromotedFloat(N->getOperand(2));
2215 
2216  return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, Op2);
2217 }
2218 
2219 // Promote the Float (first) operand and retain the Integer (second) operand
2220 SDValue DAGTypeLegalizer::PromoteFloatRes_FPOWI(SDNode *N) {
2221  EVT VT = N->getValueType(0);
2222  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2223  SDValue Op0 = GetPromotedFloat(N->getOperand(0));
2224  SDValue Op1 = N->getOperand(1);
2225 
2226  return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
2227 }
2228 
2229 // Explicit operation to reduce precision. Reduce the value to half precision
2230 // and promote it back to the legal type.
2231 SDValue DAGTypeLegalizer::PromoteFloatRes_FP_ROUND(SDNode *N) {
2232  SDLoc DL(N);
2233 
2234  SDValue Op = N->getOperand(0);
2235  EVT VT = N->getValueType(0);
2236  EVT OpVT = Op->getValueType(0);
2237  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2238  EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
2239 
2240  // Round promoted float to desired precision
2241  SDValue Round = DAG.getNode(GetPromotionOpcode(OpVT, VT), DL, IVT, Op);
2242  // Promote it back to the legal output type
2243  return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, Round);
2244 }
2245 
2246 SDValue DAGTypeLegalizer::PromoteFloatRes_LOAD(SDNode *N) {
2247  LoadSDNode *L = cast<LoadSDNode>(N);
2248  EVT VT = N->getValueType(0);
2249 
2250  // Load the value as an integer value with the same number of bits.
2251  EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
2252  SDValue newL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(), IVT,
2253  SDLoc(N), L->getChain(), L->getBasePtr(),
2254  L->getOffset(), L->getPointerInfo(), IVT,
2255  L->getAlignment(),
2256  L->getMemOperand()->getFlags(),
2257  L->getAAInfo());
2258  // Legalize the chain result by replacing uses of the old value chain with the
2259  // new one
2260  ReplaceValueWith(SDValue(N, 1), newL.getValue(1));
2261 
2262  // Convert the integer value to the desired FP type
2263  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2264  return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, newL);
2265 }
2266 
2267 // Construct a new SELECT node with the promoted true- and false- values.
2268 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT(SDNode *N) {
2269  SDValue TrueVal = GetPromotedFloat(N->getOperand(1));
2270  SDValue FalseVal = GetPromotedFloat(N->getOperand(2));
2271 
2272  return DAG.getNode(ISD::SELECT, SDLoc(N), TrueVal->getValueType(0),
2273  N->getOperand(0), TrueVal, FalseVal);
2274 }
2275 
2276 // Construct a new SELECT_CC node with the promoted true- and false- values.
2277 // The operands used for comparison are promoted by PromoteFloatOp_SELECT_CC.
2278 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT_CC(SDNode *N) {
2279  SDValue TrueVal = GetPromotedFloat(N->getOperand(2));
2280  SDValue FalseVal = GetPromotedFloat(N->getOperand(3));
2281 
2282  return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
2283  TrueVal.getNode()->getValueType(0), N->getOperand(0),
2284  N->getOperand(1), TrueVal, FalseVal, N->getOperand(4));
2285 }
2286 
2287 // Construct a SDNode that transforms the SINT or UINT operand to the promoted
2288 // float type.
2289 SDValue DAGTypeLegalizer::PromoteFloatRes_XINT_TO_FP(SDNode *N) {
2290  SDLoc DL(N);
2291  EVT VT = N->getValueType(0);
2292  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2293  SDValue NV = DAG.getNode(N->getOpcode(), DL, NVT, N->getOperand(0));
2294  // Round the value to the desired precision (that of the source type).
2295  return DAG.getNode(
2296  ISD::FP_EXTEND, DL, NVT,
2297  DAG.getNode(ISD::FP_ROUND, DL, VT, NV, DAG.getIntPtrConstant(0, DL)));
2298 }
2299 
2300 SDValue DAGTypeLegalizer::PromoteFloatRes_UNDEF(SDNode *N) {
2301  return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
2302  N->getValueType(0)));
2303 }
2304 
2305 SDValue DAGTypeLegalizer::BitcastToInt_ATOMIC_SWAP(SDNode *N) {
2306  EVT VT = N->getValueType(0);
2307  EVT NFPVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2308 
2309  AtomicSDNode *AM = cast<AtomicSDNode>(N);
2310  SDLoc SL(N);
2311 
2312  SDValue CastVal = BitConvertToInteger(AM->getVal());
2313  EVT CastVT = CastVal.getValueType();
2314 
2315  SDValue NewAtomic
2316  = DAG.getAtomic(ISD::ATOMIC_SWAP, SL, CastVT,
2317  DAG.getVTList(CastVT, MVT::Other),
2318  { AM->getChain(), AM->getBasePtr(), CastVal },
2319  AM->getMemOperand());
2320 
2321  SDValue ResultCast = DAG.getNode(GetPromotionOpcode(VT, NFPVT), SL, NFPVT,
2322  NewAtomic);
2323  // Legalize the chain result by replacing uses of the old value chain with the
2324  // new one
2325  ReplaceValueWith(SDValue(N, 1), NewAtomic.getValue(1));
2326 
2327  return ResultCast;
2328 
2329 }
2330 
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
uint64_t CallInst * C
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:603
X = FP_ROUND(Y, TRUNC) - Rounding &#39;Y&#39; from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:577
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:630
EVT getValueType() const
Return the ValueType of the referenced return value.
const SDValue & getOffset() const
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant, which is required to be operand #1) half of the integer or float value specified as operand #0.
Definition: ISDOpcodes.h:183
MakeLibCallOptions & setTypeListBeforeSoften(ArrayRef< EVT > OpsVT, EVT RetVT, bool Value=true)
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:561
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
DiagnosticInfoOptimizationBase::Argument NV
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond)
Helper function to make it easier to build SetCC&#39;s if you just have an ISD::CondCode instead of an SD...
Definition: SelectionDAG.h:963
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:686
This class represents lattice values for constants.
Definition: AllocatorList.h:23
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0...
Definition: ISDOpcodes.h:641
const SDValue & getVal() const
This structure is used to pass arguments to makeLibCall function.
const SDValue & getBasePtr() const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
const SDValue & getValue() const
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:252
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit...
AAMDNodes getAAInfo() const
Returns the AA info that describes the dereference.
const SDValue & getBasePtr() const
const SDValue & getChain() const
ISD::MemIndexedMode getAddressingMode() const
Return the addressing mode for this load or store: unindexed, pre-inc, pre-dec, post-inc, or post-dec.
static const fltSemantics & EVTToAPFloatSemantics(EVT VT)
Returns an APFloat semantics tag appropriate for the given type.
unsigned getAlignment() const
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS)
Helper function to make it easier to build Select&#39;s if you just have operands and don&#39;t want to check...
Definition: SelectionDAG.h:976
const SDNodeFlags getFlags() const
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
bool isByteSized() const
Return true if the bit size is a multiple of 8.
Definition: ValueTypes.h:211
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:474
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition: ISDOpcodes.h:38
Libcall getFPROUND(EVT OpVT, EVT RetVT)
getFPROUND - Return the FPROUND_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
bool isTruncatingStore() const
Return true if the op does a truncation before store.
Libcall getUINTTOFP(EVT OpVT, EVT RetVT)
getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
Shift and rotation operations.
Definition: ISDOpcodes.h:449
bool isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:190
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
SimpleValueType SimpleTy
The memory access is dereferenceable (i.e., doesn&#39;t trap).
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN, ptr, amt) For double-word atomic operations: ValLo, ValHi, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amtLo, amtHi) ValLo, ValHi, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN, ptr, amtLo, amtHi) These correspond to the atomicrmw instruction.
Definition: ISDOpcodes.h:849
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:417
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
This is an SDNode representing atomic operations.
SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:291
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:517
bool isUNINDEXEDStore(const SDNode *N)
Returns true if the specified node is an unindexed store.
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1461
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
Definition: SelectionDAG.h:878
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out...
Definition: ISDOpcodes.h:1020
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl) const
Returns a pair of (return value, chain).
static RTLIB::Libcall GetFPLibCall(EVT VT, RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128, RTLIB::Libcall Call_PPCF128)
GetFPLibCall - Return the right libcall for the given floating point type.
SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands...
UNDEF - An undefined node.
Definition: ISDOpcodes.h:177
This class is used to represent ISD::STORE nodes.
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:563
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector with the specified, possibly variable...
Definition: ISDOpcodes.h:359
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
const SDValue & getBasePtr() const
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:234
bool isNormalLoad(const SDNode *N)
Returns true if the specified node is a non-extending and unindexed load.
Simple binary floating point operators.
Definition: ISDOpcodes.h:287
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:272
const SDValue & getOperand(unsigned Num) const
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:246
Libcall getFPTOSINT(EVT OpVT, EVT RetVT)
getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:758
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:588
Extended Value Type.
Definition: ValueTypes.h:33
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands...
unsigned first
bool isUNINDEXEDLoad(const SDNode *N)
Returns true if the specified node is an unindexed load.
SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type...
Libcall getFPEXT(EVT OpVT, EVT RetVT)
getFPEXT - Return the FPEXT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:554
void dump() const
Dump this node, for debugging.
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:351
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:371
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:264
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
MakeLibCallOptions & setSExt(bool Value=true)
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:613
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
EVT getMemoryVT() const
Return the type of the in-memory value.
Class for arbitrary precision integers.
Definition: APInt.h:69
SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:459
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:507
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:510
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:344
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:674
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
Definition: APFloat.cpp:170
Libcall getFPTOUINT(EVT OpVT, EVT RetVT)
getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:426
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:650
#define N
Flags getFlags() const
Return the raw flags of the source value,.
The memory access always returns the same value (or traps).
uint32_t Size
Definition: Profile.cpp:46
SDValue getValue(unsigned R) const
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getCondCode(ISD::CondCode Cond)
Libcall getSINTTOFP(EVT OpVT, EVT RetVT)
getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input...
const MachinePointerInfo & getPointerInfo() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:334
SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True, SDValue False, ISD::CondCode Cond)
Helper function to make it easier to build SelectCC&#39;s if you just have an ISD::CondCode instead of an...
Definition: SelectionDAG.h:988
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
const APFloat & getValueAPF() const
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations...
Definition: ISDOpcodes.h:338
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:482
APInt bitcastToAPInt() const
Definition: APFloat.h:1109
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:197
Conversion operators.
Definition: ISDOpcodes.h:504
bool isBigEndian() const
Definition: DataLayout.h:233
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:513
#define LLVM_DEBUG(X)
Definition: Debug.h:122
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:618
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:63
EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
static ISD::NodeType GetPromotionOpcode(EVT OpVT, EVT RetVT)
LLVMContext * getContext() const
Definition: SelectionDAG.h:424
void softenSetCCOperands(SelectionDAG &DAG, EVT VT, SDValue &NewLHS, SDValue &NewRHS, ISD::CondCode &CCCode, const SDLoc &DL, const SDValue OldLHS, const SDValue OldRHS) const
Soften the operands of a comparison.
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL, bool LegalTypes=true) const
This class is used to represent ISD::LOAD nodes.