blob: 8aa3e940025a9dcf43cf34a4dca3093ac2e2fb65 [file] [log] [blame]
Duncan Sands69b01e92008-06-17 14:27:01 +00001//===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
Duncan Sandsd8742ee2008-03-12 21:27:04 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Duncan Sands4fc4fd62008-06-20 17:49:55 +000010// This file implements float type expansion and softening for LegalizeTypes.
11// Softening is the act of turning a computation in an illegal floating point
12// type into a computation in an integer type of the same size; also known as
13// "soft float". For example, turning f32 arithmetic into operations using i32.
14// The resulting integer value is the same as what you would get by performing
15// the floating point operation and bitcasting the result to the integer type.
Duncan Sands69b01e92008-06-17 14:27:01 +000016// Expansion is the act of changing a computation in an illegal type to be a
Duncan Sands78cd6492008-06-20 18:40:50 +000017// computation in two identical registers of a smaller type. For example,
Duncan Sands69b01e92008-06-17 14:27:01 +000018// implementing ppcf128 arithmetic in two f64 registers.
Duncan Sandsd8742ee2008-03-12 21:27:04 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "LegalizeTypes.h"
23using namespace llvm;
24
Duncan Sands37bcda32008-04-18 20:56:03 +000025/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +000026static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37bcda32008-04-18 20:56:03 +000027 RTLIB::Libcall Call_F32,
28 RTLIB::Libcall Call_F64,
29 RTLIB::Libcall Call_F80,
30 RTLIB::Libcall Call_PPCF128) {
31 return
32 VT == MVT::f32 ? Call_F32 :
33 VT == MVT::f64 ? Call_F64 :
34 VT == MVT::f80 ? Call_F80 :
35 VT == MVT::ppcf128 ? Call_PPCF128 :
36 RTLIB::UNKNOWN_LIBCALL;
37}
38
Duncan Sandsd8742ee2008-03-12 21:27:04 +000039//===----------------------------------------------------------------------===//
40// Result Float to Integer Conversion.
41//===----------------------------------------------------------------------===//
42
Duncan Sands4fc4fd62008-06-20 17:49:55 +000043void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
44 DEBUG(cerr << "Soften float result " << ResNo << ": "; N->dump(&DAG);
Duncan Sandsd8742ee2008-03-12 21:27:04 +000045 cerr << "\n");
Dan Gohman475871a2008-07-27 21:46:04 +000046 SDValue R = SDValue();
Duncan Sandsd8742ee2008-03-12 21:27:04 +000047
Duncan Sandsd8742ee2008-03-12 21:27:04 +000048 switch (N->getOpcode()) {
49 default:
50#ifndef NDEBUG
Duncan Sands4fc4fd62008-06-20 17:49:55 +000051 cerr << "SoftenFloatResult #" << ResNo << ": ";
Duncan Sandsd8742ee2008-03-12 21:27:04 +000052 N->dump(&DAG); cerr << "\n";
53#endif
Duncan Sands28124ac2008-07-08 10:50:55 +000054 assert(0 && "Do not know how to soften the result of this operator!");
Duncan Sandsd8742ee2008-03-12 21:27:04 +000055 abort();
56
Duncan Sands4fc4fd62008-06-20 17:49:55 +000057 case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
58 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
Duncan Sands37bcda32008-04-18 20:56:03 +000059 case ISD::ConstantFP:
Duncan Sands4fc4fd62008-06-20 17:49:55 +000060 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
Duncan Sands37bcda32008-04-18 20:56:03 +000061 break;
Duncan Sands004e27c2009-03-29 13:51:06 +000062 case ISD::EXTRACT_VECTOR_ELT:
63 R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +000064 case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
Duncan Sands05c397d2008-07-16 11:41:33 +000065 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
Duncan Sands51d83fd2008-11-18 09:15:03 +000066 case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
Duncan Sands4fc4fd62008-06-20 17:49:55 +000067 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
Duncan Sands51d83fd2008-11-18 09:15:03 +000068 case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
Duncan Sandsc3e26722008-07-18 21:18:48 +000069 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
Duncan Sands51d83fd2008-11-18 09:15:03 +000070 case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
71 case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
72 case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
73 case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
74 case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
75 case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
Duncan Sands05c397d2008-07-16 11:41:33 +000076 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
Duncan Sands51d83fd2008-11-18 09:15:03 +000077 case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
78 case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
Duncan Sands28124ac2008-07-08 10:50:55 +000079 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
80 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
Duncan Sands2cbe7fe2008-10-22 11:49:09 +000081 case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
Duncan Sands05c397d2008-07-16 11:41:33 +000082 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
Duncan Sands51d83fd2008-11-18 09:15:03 +000083 case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
84 case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
85 case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
Duncan Sands05c397d2008-07-16 11:41:33 +000086 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
Duncan Sands51d83fd2008-11-18 09:15:03 +000087 case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
Duncan Sands4fc4fd62008-06-20 17:49:55 +000088 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
Duncan Sands7ba11c52008-07-08 20:03:24 +000089 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
90 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
Duncan Sandsf5092242008-11-10 17:36:26 +000091 case ISD::SINT_TO_FP:
92 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
Richard Pennington278f83d2009-02-21 19:11:18 +000093 case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
Duncan Sandsd8742ee2008-03-12 21:27:04 +000094 }
95
96 // If R is null, the sub-method took care of registering the result.
Gabor Greifba36cb52008-08-28 21:40:38 +000097 if (R.getNode())
Dan Gohman475871a2008-07-27 21:46:04 +000098 SetSoftenedFloat(SDValue(N, ResNo), R);
Duncan Sandsd8742ee2008-03-12 21:27:04 +000099}
100
Dan Gohman475871a2008-07-27 21:46:04 +0000101SDValue DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000102 return BitConvertToInteger(N->getOperand(0));
103}
104
Dan Gohman475871a2008-07-27 21:46:04 +0000105SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000106 // Convert the inputs to integers, and build a new pair out of them.
Dale Johannesen91b49b92009-01-31 00:43:08 +0000107 return DAG.getNode(ISD::BUILD_PAIR, N->getDebugLoc(),
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000108 TLI.getTypeToTransformTo(N->getValueType(0)),
109 BitConvertToInteger(N->getOperand(0)),
110 BitConvertToInteger(N->getOperand(1)));
111}
112
Dan Gohman475871a2008-07-27 21:46:04 +0000113SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000114 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
Duncan Sands37bcda32008-04-18 20:56:03 +0000115 TLI.getTypeToTransformTo(N->getValueType(0)));
116}
117
Duncan Sands004e27c2009-03-29 13:51:06 +0000118SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
119 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
120 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(),
121 NewOp.getValueType().getVectorElementType(),
122 NewOp, N->getOperand(1));
123}
124
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000125SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
126 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
127 unsigned Size = NVT.getSizeInBits();
128
129 // Mask = ~(1 << (Size-1))
Duncan Sands51d83fd2008-11-18 09:15:03 +0000130 SDValue Mask = DAG.getConstant(APInt::getAllOnesValue(Size).clear(Size-1),
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000131 NVT);
132 SDValue Op = GetSoftenedFloat(N->getOperand(0));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000133 return DAG.getNode(ISD::AND, N->getDebugLoc(), NVT, Op, Mask);
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000134}
135
Dan Gohman475871a2008-07-27 21:46:04 +0000136SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000137 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
Dan Gohman475871a2008-07-27 21:46:04 +0000138 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
Duncan Sands06f0aff2008-10-31 14:06:52 +0000139 GetSoftenedFloat(N->getOperand(1)) };
Duncan Sands37bcda32008-04-18 20:56:03 +0000140 return MakeLibCall(GetFPLibCall(N->getValueType(0),
141 RTLIB::ADD_F32,
142 RTLIB::ADD_F64,
143 RTLIB::ADD_F80,
144 RTLIB::ADD_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000145 NVT, Ops, 2, false, N->getDebugLoc());
Duncan Sands37bcda32008-04-18 20:56:03 +0000146}
147
Duncan Sands51d83fd2008-11-18 09:15:03 +0000148SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
149 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
150 SDValue Op = GetSoftenedFloat(N->getOperand(0));
151 return MakeLibCall(GetFPLibCall(N->getValueType(0),
152 RTLIB::CEIL_F32,
153 RTLIB::CEIL_F64,
154 RTLIB::CEIL_F80,
155 RTLIB::CEIL_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000156 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000157}
158
Dan Gohman475871a2008-07-27 21:46:04 +0000159SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
160 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
161 SDValue RHS = BitConvertToInteger(N->getOperand(1));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000162 DebugLoc dl = N->getDebugLoc();
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000163
Duncan Sands83ec4b62008-06-06 12:08:01 +0000164 MVT LVT = LHS.getValueType();
165 MVT RVT = RHS.getValueType();
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000166
Duncan Sands83ec4b62008-06-06 12:08:01 +0000167 unsigned LSize = LVT.getSizeInBits();
168 unsigned RSize = RVT.getSizeInBits();
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000169
170 // First get the sign bit of second operand.
Dale Johannesen91b49b92009-01-31 00:43:08 +0000171 SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000172 DAG.getConstant(RSize - 1,
173 TLI.getShiftAmountTy()));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000174 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000175
176 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000177 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000178 if (SizeDiff > 0) {
Dale Johannesen91b49b92009-01-31 00:43:08 +0000179 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000180 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000181 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000182 } else if (SizeDiff < 0) {
Dale Johannesen91b49b92009-01-31 00:43:08 +0000183 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
184 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000185 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
186 }
187
188 // Clear the sign bit of the first operand.
Dale Johannesen91b49b92009-01-31 00:43:08 +0000189 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000190 DAG.getConstant(LSize - 1,
191 TLI.getShiftAmountTy()));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000192 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
193 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000194
195 // Or the value with the sign bit.
Dale Johannesen91b49b92009-01-31 00:43:08 +0000196 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000197}
198
Duncan Sands51d83fd2008-11-18 09:15:03 +0000199SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
200 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
201 SDValue Op = GetSoftenedFloat(N->getOperand(0));
202 return MakeLibCall(GetFPLibCall(N->getValueType(0),
203 RTLIB::COS_F32,
204 RTLIB::COS_F64,
205 RTLIB::COS_F80,
206 RTLIB::COS_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000207 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000208}
209
Dan Gohman475871a2008-07-27 21:46:04 +0000210SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
Duncan Sandsc3e26722008-07-18 21:18:48 +0000211 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
Dan Gohman475871a2008-07-27 21:46:04 +0000212 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
Duncan Sands2cbe7fe2008-10-22 11:49:09 +0000213 GetSoftenedFloat(N->getOperand(1)) };
Duncan Sandsc3e26722008-07-18 21:18:48 +0000214 return MakeLibCall(GetFPLibCall(N->getValueType(0),
215 RTLIB::DIV_F32,
216 RTLIB::DIV_F64,
217 RTLIB::DIV_F80,
218 RTLIB::DIV_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000219 NVT, Ops, 2, false, N->getDebugLoc());
Duncan Sandsc3e26722008-07-18 21:18:48 +0000220}
221
Duncan Sands51d83fd2008-11-18 09:15:03 +0000222SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
223 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
224 SDValue Op = GetSoftenedFloat(N->getOperand(0));
225 return MakeLibCall(GetFPLibCall(N->getValueType(0),
226 RTLIB::EXP_F32,
227 RTLIB::EXP_F64,
228 RTLIB::EXP_F80,
229 RTLIB::EXP_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000230 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000231}
232
233SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
234 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
235 SDValue Op = GetSoftenedFloat(N->getOperand(0));
236 return MakeLibCall(GetFPLibCall(N->getValueType(0),
237 RTLIB::EXP2_F32,
238 RTLIB::EXP2_F64,
239 RTLIB::EXP2_F80,
240 RTLIB::EXP2_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000241 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000242}
243
244SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
245 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
246 SDValue Op = GetSoftenedFloat(N->getOperand(0));
247 return MakeLibCall(GetFPLibCall(N->getValueType(0),
248 RTLIB::FLOOR_F32,
249 RTLIB::FLOOR_F64,
250 RTLIB::FLOOR_F80,
251 RTLIB::FLOOR_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000252 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000253}
254
255SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
256 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
257 SDValue Op = GetSoftenedFloat(N->getOperand(0));
258 return MakeLibCall(GetFPLibCall(N->getValueType(0),
259 RTLIB::LOG_F32,
260 RTLIB::LOG_F64,
261 RTLIB::LOG_F80,
262 RTLIB::LOG_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000263 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000264}
265
266SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
267 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
268 SDValue Op = GetSoftenedFloat(N->getOperand(0));
269 return MakeLibCall(GetFPLibCall(N->getValueType(0),
270 RTLIB::LOG2_F32,
271 RTLIB::LOG2_F64,
272 RTLIB::LOG2_F80,
273 RTLIB::LOG2_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000274 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000275}
276
277SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
278 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
279 SDValue Op = GetSoftenedFloat(N->getOperand(0));
280 return MakeLibCall(GetFPLibCall(N->getValueType(0),
281 RTLIB::LOG10_F32,
282 RTLIB::LOG10_F64,
283 RTLIB::LOG10_F80,
284 RTLIB::LOG10_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000285 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000286}
287
Dan Gohman475871a2008-07-27 21:46:04 +0000288SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000289 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
Duncan Sands06f0aff2008-10-31 14:06:52 +0000291 GetSoftenedFloat(N->getOperand(1)) };
Duncan Sands37bcda32008-04-18 20:56:03 +0000292 return MakeLibCall(GetFPLibCall(N->getValueType(0),
293 RTLIB::MUL_F32,
294 RTLIB::MUL_F64,
295 RTLIB::MUL_F80,
296 RTLIB::MUL_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000297 NVT, Ops, 2, false, N->getDebugLoc());
Duncan Sands37bcda32008-04-18 20:56:03 +0000298}
Duncan Sands14ea39c2008-03-27 20:23:40 +0000299
Duncan Sands51d83fd2008-11-18 09:15:03 +0000300SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
301 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
302 SDValue Op = GetSoftenedFloat(N->getOperand(0));
303 return MakeLibCall(GetFPLibCall(N->getValueType(0),
304 RTLIB::NEARBYINT_F32,
305 RTLIB::NEARBYINT_F64,
306 RTLIB::NEARBYINT_F80,
307 RTLIB::NEARBYINT_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000308 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000309}
310
311SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
312 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
313 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
314 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
315 GetSoftenedFloat(N->getOperand(0)) };
316 return MakeLibCall(GetFPLibCall(N->getValueType(0),
317 RTLIB::SUB_F32,
318 RTLIB::SUB_F64,
319 RTLIB::SUB_F80,
320 RTLIB::SUB_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000321 NVT, Ops, 2, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000322}
323
Dan Gohman475871a2008-07-27 21:46:04 +0000324SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
Duncan Sands28124ac2008-07-08 10:50:55 +0000325 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
Dan Gohman475871a2008-07-27 21:46:04 +0000326 SDValue Op = N->getOperand(0);
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000327 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
328 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000329 return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands28124ac2008-07-08 10:50:55 +0000330}
331
Dan Gohman475871a2008-07-27 21:46:04 +0000332SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
Duncan Sands28124ac2008-07-08 10:50:55 +0000333 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
Dan Gohman475871a2008-07-27 21:46:04 +0000334 SDValue Op = N->getOperand(0);
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000335 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
336 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000337 return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands28124ac2008-07-08 10:50:55 +0000338}
339
Duncan Sands2cbe7fe2008-10-22 11:49:09 +0000340SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
341 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
342 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
343 GetSoftenedFloat(N->getOperand(1)) };
344 return MakeLibCall(GetFPLibCall(N->getValueType(0),
345 RTLIB::POW_F32,
346 RTLIB::POW_F64,
347 RTLIB::POW_F80,
348 RTLIB::POW_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000349 NVT, Ops, 2, false, N->getDebugLoc());
Duncan Sands2cbe7fe2008-10-22 11:49:09 +0000350}
351
Dan Gohman475871a2008-07-27 21:46:04 +0000352SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
Duncan Sandsf5092242008-11-10 17:36:26 +0000353 assert(N->getOperand(1).getValueType() == MVT::i32 &&
354 "Unsupported power type!");
Duncan Sands851b1ee2008-07-09 11:11:47 +0000355 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
Dan Gohman475871a2008-07-27 21:46:04 +0000356 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
Duncan Sands851b1ee2008-07-09 11:11:47 +0000357 return MakeLibCall(GetFPLibCall(N->getValueType(0),
358 RTLIB::POWI_F32,
359 RTLIB::POWI_F64,
360 RTLIB::POWI_F80,
361 RTLIB::POWI_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000362 NVT, Ops, 2, false, N->getDebugLoc());
Duncan Sands851b1ee2008-07-09 11:11:47 +0000363}
364
Duncan Sands51d83fd2008-11-18 09:15:03 +0000365SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
366 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
367 SDValue Op = GetSoftenedFloat(N->getOperand(0));
368 return MakeLibCall(GetFPLibCall(N->getValueType(0),
369 RTLIB::RINT_F32,
370 RTLIB::RINT_F64,
371 RTLIB::RINT_F80,
372 RTLIB::RINT_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000373 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000374}
375
376SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
377 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
378 SDValue Op = GetSoftenedFloat(N->getOperand(0));
379 return MakeLibCall(GetFPLibCall(N->getValueType(0),
380 RTLIB::SIN_F32,
381 RTLIB::SIN_F64,
382 RTLIB::SIN_F80,
383 RTLIB::SIN_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000384 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000385}
386
387SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
388 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
389 SDValue Op = GetSoftenedFloat(N->getOperand(0));
390 return MakeLibCall(GetFPLibCall(N->getValueType(0),
391 RTLIB::SQRT_F32,
392 RTLIB::SQRT_F64,
393 RTLIB::SQRT_F80,
394 RTLIB::SQRT_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000395 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000396}
397
Dan Gohman475871a2008-07-27 21:46:04 +0000398SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000399 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
Dan Gohman475871a2008-07-27 21:46:04 +0000400 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
Duncan Sands06f0aff2008-10-31 14:06:52 +0000401 GetSoftenedFloat(N->getOperand(1)) };
Duncan Sands37bcda32008-04-18 20:56:03 +0000402 return MakeLibCall(GetFPLibCall(N->getValueType(0),
403 RTLIB::SUB_F32,
404 RTLIB::SUB_F64,
405 RTLIB::SUB_F80,
406 RTLIB::SUB_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000407 NVT, Ops, 2, false, N->getDebugLoc());
Duncan Sands37bcda32008-04-18 20:56:03 +0000408}
409
Duncan Sands51d83fd2008-11-18 09:15:03 +0000410SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
411 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
412 SDValue Op = GetSoftenedFloat(N->getOperand(0));
413 return MakeLibCall(GetFPLibCall(N->getValueType(0),
414 RTLIB::TRUNC_F32,
415 RTLIB::TRUNC_F64,
416 RTLIB::TRUNC_F80,
417 RTLIB::TRUNC_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000418 NVT, &Op, 1, false, N->getDebugLoc());
Duncan Sands51d83fd2008-11-18 09:15:03 +0000419}
420
Dan Gohman475871a2008-07-27 21:46:04 +0000421SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
Duncan Sands37bcda32008-04-18 20:56:03 +0000422 LoadSDNode *L = cast<LoadSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000423 MVT VT = N->getValueType(0);
424 MVT NVT = TLI.getTypeToTransformTo(VT);
Dale Johannesen91b49b92009-01-31 00:43:08 +0000425 DebugLoc dl = N->getDebugLoc();
Duncan Sands37bcda32008-04-18 20:56:03 +0000426
Dan Gohman475871a2008-07-27 21:46:04 +0000427 SDValue NewL;
Duncan Sands452911c2008-07-09 11:15:31 +0000428 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
Dale Johannesen91b49b92009-01-31 00:43:08 +0000429 NewL = DAG.getLoad(L->getAddressingMode(), dl, L->getExtensionType(),
Duncan Sands452911c2008-07-09 11:15:31 +0000430 NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
431 L->getSrcValue(), L->getSrcValueOffset(), NVT,
432 L->isVolatile(), L->getAlignment());
433 // Legalized the chain result - switch anything that used the old chain to
434 // use the new one.
Dan Gohman475871a2008-07-27 21:46:04 +0000435 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
Duncan Sands452911c2008-07-09 11:15:31 +0000436 return NewL;
437 }
Duncan Sands37bcda32008-04-18 20:56:03 +0000438
439 // Do a non-extending load followed by FP_EXTEND.
Dale Johannesen91b49b92009-01-31 00:43:08 +0000440 NewL = DAG.getLoad(L->getAddressingMode(), dl, ISD::NON_EXTLOAD,
Duncan Sands452911c2008-07-09 11:15:31 +0000441 L->getMemoryVT(), L->getChain(),
442 L->getBasePtr(), L->getOffset(),
443 L->getSrcValue(), L->getSrcValueOffset(),
444 L->getMemoryVT(),
445 L->isVolatile(), L->getAlignment());
446 // Legalized the chain result - switch anything that used the old chain to
447 // use the new one.
Dan Gohman475871a2008-07-27 21:46:04 +0000448 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000449 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
Duncan Sands37bcda32008-04-18 20:56:03 +0000450}
451
Dan Gohman475871a2008-07-27 21:46:04 +0000452SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
453 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
454 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000455 return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
456 LHS.getValueType(), N->getOperand(0),LHS,RHS);
Duncan Sands7ba11c52008-07-08 20:03:24 +0000457}
458
Dan Gohman475871a2008-07-27 21:46:04 +0000459SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
460 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
461 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000462 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
463 LHS.getValueType(), N->getOperand(0),
Duncan Sands7ba11c52008-07-08 20:03:24 +0000464 N->getOperand(1), LHS, RHS, N->getOperand(4));
465}
466
Richard Pennington278f83d2009-02-21 19:11:18 +0000467SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
468 SDValue Chain = N->getOperand(0); // Get the chain.
469 SDValue Ptr = N->getOperand(1); // Get the pointer.
470 MVT VT = N->getValueType(0);
471 MVT NVT = TLI.getTypeToTransformTo(VT);
472 DebugLoc dl = N->getDebugLoc();
473
474 SDValue NewVAARG;
475 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2));
476
477 // Legalized the chain result - switch anything that used the old chain to
478 // use the new one.
479 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
480 return NewVAARG;
481}
482
Duncan Sandsf5092242008-11-10 17:36:26 +0000483SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
484 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
485 MVT SVT = N->getOperand(0).getValueType();
Duncan Sandsb5508e42008-07-09 12:07:22 +0000486 MVT RVT = N->getValueType(0);
Duncan Sandsf5092242008-11-10 17:36:26 +0000487 MVT NVT = MVT();
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000488 DebugLoc dl = N->getDebugLoc();
Duncan Sandsb5508e42008-07-09 12:07:22 +0000489
Duncan Sandsf5092242008-11-10 17:36:26 +0000490 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
491 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
492 // match. Look for an appropriate libcall.
493 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
494 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
495 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
496 NVT = (MVT::SimpleValueType)t;
497 // The source needs to big enough to hold the operand.
498 if (NVT.bitsGE(SVT))
499 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
500 }
501 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
502
503 // Sign/zero extend the argument if the libcall takes a larger type.
Dale Johannesen91b49b92009-01-31 00:43:08 +0000504 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
Duncan Sandsf5092242008-11-10 17:36:26 +0000505 NVT, N->getOperand(0));
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000506 return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false, dl);
Duncan Sands14ea39c2008-03-27 20:23:40 +0000507}
508
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000509
510//===----------------------------------------------------------------------===//
511// Operand Float to Integer Conversion..
512//===----------------------------------------------------------------------===//
513
Duncan Sands4fc4fd62008-06-20 17:49:55 +0000514bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
515 DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000516 cerr << "\n");
Dan Gohman475871a2008-07-27 21:46:04 +0000517 SDValue Res = SDValue();
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000518
Duncan Sands7ba11c52008-07-08 20:03:24 +0000519 switch (N->getOpcode()) {
520 default:
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000521#ifndef NDEBUG
Duncan Sands7ba11c52008-07-08 20:03:24 +0000522 cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
523 N->dump(&DAG); cerr << "\n";
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000524#endif
Duncan Sands7ba11c52008-07-08 20:03:24 +0000525 assert(0 && "Do not know how to soften this operator's operand!");
526 abort();
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000527
Duncan Sands7ba11c52008-07-08 20:03:24 +0000528 case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
Duncan Sands05c397d2008-07-16 11:41:33 +0000529 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000530 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
Duncan Sands05c397d2008-07-16 11:41:33 +0000531 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
532 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
533 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
534 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
535 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000536 }
537
538 // If the result is null, the sub-method took care of registering results etc.
Gabor Greifba36cb52008-08-28 21:40:38 +0000539 if (!Res.getNode()) return false;
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000540
Duncan Sands47d9dcc2008-12-09 21:33:20 +0000541 // If the result is N, the sub-method updated N in place. Tell the legalizer
542 // core about this.
543 if (Res.getNode() == N)
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000544 return true;
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000545
546 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
547 "Invalid operand expansion");
548
Dan Gohman475871a2008-07-27 21:46:04 +0000549 ReplaceValueWith(SDValue(N, 0), Res);
Duncan Sandsd8742ee2008-03-12 21:27:04 +0000550 return false;
551}
552
Duncan Sands11ac7972008-06-25 16:34:21 +0000553/// SoftenSetCCOperands - Soften the operands of a comparison. This code is
554/// shared among BR_CC, SELECT_CC, and SETCC handlers.
Dan Gohman475871a2008-07-27 21:46:04 +0000555void DAGTypeLegalizer::SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000556 ISD::CondCode &CCCode, DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000557 SDValue LHSInt = GetSoftenedFloat(NewLHS);
558 SDValue RHSInt = GetSoftenedFloat(NewRHS);
Duncan Sands7ba11c52008-07-08 20:03:24 +0000559 MVT VT = NewLHS.getValueType();
Duncan Sands11ac7972008-06-25 16:34:21 +0000560
561 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
562
563 // Expand into one or more soft-fp libcall(s).
Evan Chengee4dc162008-06-30 22:33:56 +0000564 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Duncan Sands11ac7972008-06-25 16:34:21 +0000565 switch (CCCode) {
566 case ISD::SETEQ:
567 case ISD::SETOEQ:
568 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
569 break;
570 case ISD::SETNE:
571 case ISD::SETUNE:
572 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
573 break;
574 case ISD::SETGE:
575 case ISD::SETOGE:
576 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
577 break;
578 case ISD::SETLT:
579 case ISD::SETOLT:
580 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
581 break;
582 case ISD::SETLE:
583 case ISD::SETOLE:
584 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
585 break;
586 case ISD::SETGT:
587 case ISD::SETOGT:
588 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
589 break;
590 case ISD::SETUO:
591 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
592 break;
593 case ISD::SETO:
Duncan Sands3a2eb292008-07-08 20:00:05 +0000594 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Duncan Sands11ac7972008-06-25 16:34:21 +0000595 break;
596 default:
597 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
598 switch (CCCode) {
599 case ISD::SETONE:
600 // SETONE = SETOLT | SETOGT
601 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
602 // Fallthrough
603 case ISD::SETUGT:
604 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
605 break;
606 case ISD::SETUGE:
607 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
608 break;
609 case ISD::SETULT:
610 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
611 break;
612 case ISD::SETULE:
613 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
614 break;
615 case ISD::SETUEQ:
616 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
617 break;
618 default: assert(false && "Do not know how to soften this setcc!");
619 }
620 }
621
Duncan Sandsddc7aa32008-07-09 08:07:41 +0000622 MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue Ops[2] = { LHSInt, RHSInt };
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000624 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
Duncan Sandsddc7aa32008-07-09 08:07:41 +0000625 NewRHS = DAG.getConstant(0, RetVT);
Duncan Sands7ba11c52008-07-08 20:03:24 +0000626 CCCode = TLI.getCmpLibcallCC(LC1);
Duncan Sands11ac7972008-06-25 16:34:21 +0000627 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Dale Johannesen91b49b92009-01-31 00:43:08 +0000628 SDValue Tmp = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT),
Duncan Sands7ba11c52008-07-08 20:03:24 +0000629 NewLHS, NewRHS, DAG.getCondCode(CCCode));
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000630 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
Dale Johannesen91b49b92009-01-31 00:43:08 +0000631 NewLHS = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT), NewLHS,
Duncan Sands11ac7972008-06-25 16:34:21 +0000632 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000633 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000634 NewRHS = SDValue();
Duncan Sands11ac7972008-06-25 16:34:21 +0000635 }
636}
637
Dan Gohman475871a2008-07-27 21:46:04 +0000638SDValue DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
Dale Johannesen91b49b92009-01-31 00:43:08 +0000639 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), N->getValueType(0),
Duncan Sands4fc4fd62008-06-20 17:49:55 +0000640 GetSoftenedFloat(N->getOperand(0)));
Duncan Sands69b01e92008-06-17 14:27:01 +0000641}
642
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000643SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
644 MVT SVT = N->getOperand(0).getValueType();
645 MVT RVT = N->getValueType(0);
646
647 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
648 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
649
650 SDValue Op = GetSoftenedFloat(N->getOperand(0));
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000651 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000652}
653
Dan Gohman475871a2008-07-27 21:46:04 +0000654SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
655 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
Duncan Sands11ac7972008-06-25 16:34:21 +0000656 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000657 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
Duncan Sands11ac7972008-06-25 16:34:21 +0000658
659 // If SoftenSetCCOperands returned a scalar, we need to compare the result
660 // against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +0000661 if (NewRHS.getNode() == 0) {
Duncan Sands11ac7972008-06-25 16:34:21 +0000662 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
663 CCCode = ISD::SETNE;
664 }
665
666 // Update N to have the operands specified.
Dan Gohman475871a2008-07-27 21:46:04 +0000667 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
Duncan Sands11ac7972008-06-25 16:34:21 +0000668 DAG.getCondCode(CCCode), NewLHS, NewRHS,
669 N->getOperand(4));
670}
671
Dan Gohman475871a2008-07-27 21:46:04 +0000672SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
Duncan Sandsf8568b42008-07-09 11:13:46 +0000673 MVT RVT = N->getValueType(0);
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000674 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
Duncan Sandsf8568b42008-07-09 11:13:46 +0000675 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
Dan Gohman475871a2008-07-27 21:46:04 +0000676 SDValue Op = GetSoftenedFloat(N->getOperand(0));
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000677 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
Duncan Sandsf8568b42008-07-09 11:13:46 +0000678}
679
Dan Gohman475871a2008-07-27 21:46:04 +0000680SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
Duncan Sandsf8568b42008-07-09 11:13:46 +0000681 MVT RVT = N->getValueType(0);
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000682 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
Duncan Sandsf8568b42008-07-09 11:13:46 +0000683 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
Dan Gohman475871a2008-07-27 21:46:04 +0000684 SDValue Op = GetSoftenedFloat(N->getOperand(0));
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000685 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
Duncan Sandsf8568b42008-07-09 11:13:46 +0000686}
687
Dan Gohman475871a2008-07-27 21:46:04 +0000688SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
689 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
Duncan Sands11ac7972008-06-25 16:34:21 +0000690 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000691 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
Duncan Sands11ac7972008-06-25 16:34:21 +0000692
693 // If SoftenSetCCOperands returned a scalar, we need to compare the result
694 // against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +0000695 if (NewRHS.getNode() == 0) {
Duncan Sands11ac7972008-06-25 16:34:21 +0000696 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
697 CCCode = ISD::SETNE;
698 }
699
700 // Update N to have the operands specified.
Dan Gohman475871a2008-07-27 21:46:04 +0000701 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
Duncan Sands11ac7972008-06-25 16:34:21 +0000702 N->getOperand(2), N->getOperand(3),
703 DAG.getCondCode(CCCode));
704}
705
Dan Gohman475871a2008-07-27 21:46:04 +0000706SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
707 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
Duncan Sands11ac7972008-06-25 16:34:21 +0000708 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000709 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
Duncan Sands11ac7972008-06-25 16:34:21 +0000710
711 // If SoftenSetCCOperands returned a scalar, use it.
Gabor Greifba36cb52008-08-28 21:40:38 +0000712 if (NewRHS.getNode() == 0) {
Duncan Sands11ac7972008-06-25 16:34:21 +0000713 assert(NewLHS.getValueType() == N->getValueType(0) &&
714 "Unexpected setcc expansion!");
715 return NewLHS;
716 }
717
718 // Otherwise, update N to have the operands specified.
Dan Gohman475871a2008-07-27 21:46:04 +0000719 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
Duncan Sands11ac7972008-06-25 16:34:21 +0000720 DAG.getCondCode(CCCode));
721}
722
Dan Gohman475871a2008-07-27 21:46:04 +0000723SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
Duncan Sands990f0322008-07-07 00:08:12 +0000724 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
725 assert(OpNo == 1 && "Can only soften the stored value!");
726 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000727 SDValue Val = ST->getValue();
Dale Johannesen91b49b92009-01-31 00:43:08 +0000728 DebugLoc dl = N->getDebugLoc();
Duncan Sands990f0322008-07-07 00:08:12 +0000729
730 if (ST->isTruncatingStore())
731 // Do an FP_ROUND followed by a non-truncating store.
Dale Johannesen91b49b92009-01-31 00:43:08 +0000732 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
Duncan Sands990f0322008-07-07 00:08:12 +0000733 Val, DAG.getIntPtrConstant(0)));
734 else
735 Val = GetSoftenedFloat(Val);
736
Dale Johannesen91b49b92009-01-31 00:43:08 +0000737 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
Duncan Sands990f0322008-07-07 00:08:12 +0000738 ST->getSrcValue(), ST->getSrcValueOffset(),
739 ST->isVolatile(), ST->getAlignment());
740}
741
Duncan Sands69b01e92008-06-17 14:27:01 +0000742
743//===----------------------------------------------------------------------===//
744// Float Result Expansion
745//===----------------------------------------------------------------------===//
746
747/// ExpandFloatResult - This method is called when the specified result of the
748/// specified node is found to need expansion. At this point, the node may also
749/// have invalid operands or may have other results that need promotion, we just
750/// know that (at least) one result needs expansion.
751void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
752 DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
Dan Gohman475871a2008-07-27 21:46:04 +0000753 SDValue Lo, Hi;
754 Lo = Hi = SDValue();
Duncan Sands69b01e92008-06-17 14:27:01 +0000755
756 // See if the target wants to custom expand this node.
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000757 if (CustomLowerResults(N, N->getValueType(ResNo), true))
Duncan Sands1607f052008-12-01 11:39:25 +0000758 return;
Duncan Sands69b01e92008-06-17 14:27:01 +0000759
760 switch (N->getOpcode()) {
761 default:
762#ifndef NDEBUG
763 cerr << "ExpandFloatResult #" << ResNo << ": ";
764 N->dump(&DAG); cerr << "\n";
765#endif
766 assert(0 && "Do not know how to expand the result of this operator!");
767 abort();
Duncan Sands78cd6492008-06-20 18:40:50 +0000768
769 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
770 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
771 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
772 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
773
774 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
775 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
Duncan Sands4a307ec2008-06-23 15:08:15 +0000776 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
Duncan Sands78cd6492008-06-20 18:40:50 +0000777 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
Duncan Sands21c29722008-10-29 14:25:28 +0000778 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
Duncan Sandsa1ace762008-06-21 17:00:47 +0000779
Duncan Sands041cde22008-06-25 20:24:48 +0000780 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
Duncan Sands295a7cd2008-07-15 15:02:44 +0000781 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
Duncan Sands041cde22008-06-25 20:24:48 +0000782 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
Duncan Sands06f0aff2008-10-31 14:06:52 +0000783 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
784 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
Duncan Sands041cde22008-06-25 20:24:48 +0000785 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
Duncan Sands06f0aff2008-10-31 14:06:52 +0000786 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
787 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
788 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
789 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
790 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
791 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
Duncan Sands041cde22008-06-25 20:24:48 +0000792 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
Duncan Sands06f0aff2008-10-31 14:06:52 +0000793 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
Duncan Sands79ada102008-07-17 17:35:14 +0000794 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
795 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
Duncan Sands06f0aff2008-10-31 14:06:52 +0000796 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
797 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
798 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
Duncan Sands51d83fd2008-11-18 09:15:03 +0000799 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
Duncan Sands06f0aff2008-10-31 14:06:52 +0000800 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
Duncan Sands041cde22008-06-25 20:24:48 +0000801 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
Duncan Sands06f0aff2008-10-31 14:06:52 +0000802 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
Duncan Sands041cde22008-06-25 20:24:48 +0000803 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
804 case ISD::SINT_TO_FP:
805 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
Duncan Sands69b01e92008-06-17 14:27:01 +0000806 }
807
808 // If Lo/Hi is null, the sub-method took care of registering results etc.
Gabor Greifba36cb52008-08-28 21:40:38 +0000809 if (Lo.getNode())
Dan Gohman475871a2008-07-27 21:46:04 +0000810 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
Duncan Sands69b01e92008-06-17 14:27:01 +0000811}
812
Dan Gohman475871a2008-07-27 21:46:04 +0000813void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
814 SDValue &Hi) {
Duncan Sands041cde22008-06-25 20:24:48 +0000815 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
816 assert(NVT.getSizeInBits() == integerPartWidth &&
817 "Do not know how to expand this float constant!");
Dale Johannesen7111b022008-10-09 18:53:47 +0000818 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
Duncan Sands041cde22008-06-25 20:24:48 +0000819 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
820 &C.getRawData()[1])), NVT);
821 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
822 &C.getRawData()[0])), NVT);
823}
824
Dan Gohman475871a2008-07-27 21:46:04 +0000825void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
826 SDValue &Hi) {
Duncan Sands295a7cd2008-07-15 15:02:44 +0000827 assert(N->getValueType(0) == MVT::ppcf128 &&
828 "Logic only correct for ppcf128!");
Dale Johannesen91b49b92009-01-31 00:43:08 +0000829 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +0000830 SDValue Tmp;
Duncan Sands295a7cd2008-07-15 15:02:44 +0000831 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
Dale Johannesen91b49b92009-01-31 00:43:08 +0000832 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
Duncan Sands295a7cd2008-07-15 15:02:44 +0000833 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
Dale Johannesen91b49b92009-01-31 00:43:08 +0000834 Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
835 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
Duncan Sands295a7cd2008-07-15 15:02:44 +0000836 DAG.getCondCode(ISD::SETEQ));
837}
838
Duncan Sands06f0aff2008-10-31 14:06:52 +0000839void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
840 SDValue &Hi) {
841 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
842 RTLIB::ADD_F32, RTLIB::ADD_F64,
843 RTLIB::ADD_F80, RTLIB::ADD_PPCF128),
844 N, false);
845 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
846 "Call lowered wrongly!");
847 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
848}
849
850void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
851 SDValue &Lo, SDValue &Hi) {
852 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
853 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
854 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128),
855 N, false);
856 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
857 "Call lowered wrongly!");
858 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
859}
860
861void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
862 SDValue &Lo, SDValue &Hi) {
863 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
864 RTLIB::COS_F32, RTLIB::COS_F64,
865 RTLIB::COS_F80, RTLIB::COS_PPCF128),
866 N, false);
867 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
868 "Call lowered wrongly!");
869 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
870}
871
Dan Gohman475871a2008-07-27 21:46:04 +0000872void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
873 SDValue &Hi) {
874 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
875 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
Duncan Sands06f0aff2008-10-31 14:06:52 +0000876 RTLIB::DIV_F32,
877 RTLIB::DIV_F64,
878 RTLIB::DIV_F80,
879 RTLIB::DIV_PPCF128),
Duncan Sands7fb08582009-02-02 19:46:41 +0000880 N->getValueType(0), Ops, 2, false,
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000881 N->getDebugLoc());
Duncan Sands06f0aff2008-10-31 14:06:52 +0000882 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
883 "Call lowered wrongly!");
884 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
885}
886
887void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
888 SDValue &Lo, SDValue &Hi) {
889 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
890 RTLIB::EXP_F32, RTLIB::EXP_F64,
891 RTLIB::EXP_F80, RTLIB::EXP_PPCF128),
892 N, false);
893 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
894 "Call lowered wrongly!");
895 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
896}
897
898void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
899 SDValue &Lo, SDValue &Hi) {
900 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
901 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
902 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128),
903 N, false);
904 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
905 "Call lowered wrongly!");
906 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
907}
908
909void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
910 SDValue &Lo, SDValue &Hi) {
911 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
912 RTLIB::FLOOR_F32,RTLIB::FLOOR_F64,
913 RTLIB::FLOOR_F80,RTLIB::FLOOR_PPCF128),
914 N, false);
915 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
916 "Call lowered wrongly!");
917 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
918}
919
920void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
921 SDValue &Lo, SDValue &Hi) {
922 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
923 RTLIB::LOG_F32, RTLIB::LOG_F64,
924 RTLIB::LOG_F80, RTLIB::LOG_PPCF128),
925 N, false);
926 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
927 "Call lowered wrongly!");
928 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
929}
930
931void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
932 SDValue &Lo, SDValue &Hi) {
933 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
934 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
935 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128),
936 N, false);
937 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
938 "Call lowered wrongly!");
939 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
940}
941
942void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
943 SDValue &Lo, SDValue &Hi) {
944 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
945 RTLIB::LOG10_F32,RTLIB::LOG10_F64,
946 RTLIB::LOG10_F80,RTLIB::LOG10_PPCF128),
947 N, false);
Duncan Sands86813ce2008-10-29 06:31:03 +0000948 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
949 "Call lowered wrongly!");
Duncan Sands041cde22008-06-25 20:24:48 +0000950 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
951}
952
Dan Gohman475871a2008-07-27 21:46:04 +0000953void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
954 SDValue &Hi) {
955 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
956 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
Duncan Sands06f0aff2008-10-31 14:06:52 +0000957 RTLIB::MUL_F32,
958 RTLIB::MUL_F64,
959 RTLIB::MUL_F80,
960 RTLIB::MUL_PPCF128),
Duncan Sands7fb08582009-02-02 19:46:41 +0000961 N->getValueType(0), Ops, 2, false,
Dale Johannesenc8fc99d2009-01-31 00:11:23 +0000962 N->getDebugLoc());
Duncan Sands06f0aff2008-10-31 14:06:52 +0000963 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
964 "Call lowered wrongly!");
965 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
966}
967
968void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
969 SDValue &Lo, SDValue &Hi) {
970 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
971 RTLIB::NEARBYINT_F32,
972 RTLIB::NEARBYINT_F64,
973 RTLIB::NEARBYINT_F80,
974 RTLIB::NEARBYINT_PPCF128),
975 N, false);
Duncan Sands86813ce2008-10-29 06:31:03 +0000976 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
977 "Call lowered wrongly!");
Duncan Sands041cde22008-06-25 20:24:48 +0000978 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
979}
980
Dan Gohman475871a2008-07-27 21:46:04 +0000981void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
982 SDValue &Hi) {
Dale Johannesen91b49b92009-01-31 00:43:08 +0000983 DebugLoc dl = N->getDebugLoc();
Duncan Sands79ada102008-07-17 17:35:14 +0000984 GetExpandedFloat(N->getOperand(0), Lo, Hi);
Dale Johannesen91b49b92009-01-31 00:43:08 +0000985 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
986 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
Duncan Sands79ada102008-07-17 17:35:14 +0000987}
988
Dan Gohman475871a2008-07-27 21:46:04 +0000989void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
990 SDValue &Hi) {
Duncan Sands79ada102008-07-17 17:35:14 +0000991 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
Dale Johannesen91b49b92009-01-31 00:43:08 +0000992 Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
Duncan Sands79ada102008-07-17 17:35:14 +0000993 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
994}
995
Duncan Sands06f0aff2008-10-31 14:06:52 +0000996void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
997 SDValue &Lo, SDValue &Hi) {
998 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
999 RTLIB::POW_F32, RTLIB::POW_F64,
1000 RTLIB::POW_F80, RTLIB::POW_PPCF128),
1001 N, false);
1002 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1003 "Call lowered wrongly!");
1004 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1005}
1006
1007void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1008 SDValue &Lo, SDValue &Hi) {
1009 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1010 RTLIB::POWI_F32, RTLIB::POWI_F64,
1011 RTLIB::POWI_F80, RTLIB::POWI_PPCF128),
1012 N, false);
1013 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1014 "Call lowered wrongly!");
1015 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1016}
1017
1018void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1019 SDValue &Lo, SDValue &Hi) {
1020 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1021 RTLIB::RINT_F32, RTLIB::RINT_F64,
1022 RTLIB::RINT_F80, RTLIB::RINT_PPCF128),
1023 N, false);
1024 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1025 "Call lowered wrongly!");
1026 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1027}
1028
1029void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1030 SDValue &Lo, SDValue &Hi) {
1031 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1032 RTLIB::SIN_F32, RTLIB::SIN_F64,
1033 RTLIB::SIN_F80, RTLIB::SIN_PPCF128),
1034 N, false);
1035 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1036 "Call lowered wrongly!");
1037 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1038}
1039
1040void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1041 SDValue &Lo, SDValue &Hi) {
1042 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1043 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1044 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128),
1045 N, false);
1046 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1047 "Call lowered wrongly!");
1048 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1049}
1050
Dan Gohman475871a2008-07-27 21:46:04 +00001051void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1052 SDValue &Hi) {
1053 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1054 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
Duncan Sands06f0aff2008-10-31 14:06:52 +00001055 RTLIB::SUB_F32,
1056 RTLIB::SUB_F64,
1057 RTLIB::SUB_F80,
1058 RTLIB::SUB_PPCF128),
Dale Johannesenc8fc99d2009-01-31 00:11:23 +00001059 N->getValueType(0), Ops, 2, false,
1060 N->getDebugLoc());
Duncan Sands06f0aff2008-10-31 14:06:52 +00001061 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1062 "Call lowered wrongly!");
1063 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1064}
1065
1066void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1067 SDValue &Lo, SDValue &Hi) {
1068 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1069 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1070 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128),
1071 N, false);
Duncan Sands86813ce2008-10-29 06:31:03 +00001072 assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1073 "Call lowered wrongly!");
Duncan Sands041cde22008-06-25 20:24:48 +00001074 Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1075}
1076
Dan Gohman475871a2008-07-27 21:46:04 +00001077void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1078 SDValue &Hi) {
Duncan Sandsab09b7e2008-06-23 14:19:45 +00001079 if (ISD::isNormalLoad(N)) {
1080 ExpandRes_NormalLoad(N, Lo, Hi);
Duncan Sandsa1ace762008-06-21 17:00:47 +00001081 return;
1082 }
1083
1084 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1085 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001086 SDValue Chain = LD->getChain();
1087 SDValue Ptr = LD->getBasePtr();
Dale Johannesen91b49b92009-01-31 00:43:08 +00001088 DebugLoc dl = N->getDebugLoc();
Duncan Sandsa1ace762008-06-21 17:00:47 +00001089
1090 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
1091 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1092 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1093
Dale Johannesen91b49b92009-01-31 00:43:08 +00001094 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
Duncan Sandsa1ace762008-06-21 17:00:47 +00001095 LD->getSrcValue(), LD->getSrcValueOffset(),
1096 LD->getMemoryVT(),
1097 LD->isVolatile(), LD->getAlignment());
1098
1099 // Remember the chain.
Dale Johannesen283c6962008-11-03 20:47:45 +00001100 Chain = Hi.getValue(1);
Duncan Sandsa1ace762008-06-21 17:00:47 +00001101
Dale Johannesen283c6962008-11-03 20:47:45 +00001102 // The low part is zero.
1103 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
Duncan Sandsa1ace762008-06-21 17:00:47 +00001104
1105 // Modified the chain - switch anything that used the old chain to use the
1106 // new one.
Dan Gohman475871a2008-07-27 21:46:04 +00001107 ReplaceValueWith(SDValue(LD, 1), Chain);
Duncan Sandsa1ace762008-06-21 17:00:47 +00001108}
1109
Dan Gohman475871a2008-07-27 21:46:04 +00001110void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1111 SDValue &Hi) {
Duncan Sands041cde22008-06-25 20:24:48 +00001112 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1113 MVT VT = N->getValueType(0);
1114 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00001115 SDValue Src = N->getOperand(0);
Duncan Sands041cde22008-06-25 20:24:48 +00001116 MVT SrcVT = Src.getValueType();
Dale Johannesen7e298ed2008-11-12 18:38:44 +00001117 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
Dale Johannesenc8fc99d2009-01-31 00:11:23 +00001118 DebugLoc dl = N->getDebugLoc();
Duncan Sands041cde22008-06-25 20:24:48 +00001119
1120 // First do an SINT_TO_FP, whether the original was signed or unsigned.
Dale Johannesen7e298ed2008-11-12 18:38:44 +00001121 // When promoting partial word types to i32 we must honor the signedness,
1122 // though.
Duncan Sands041cde22008-06-25 20:24:48 +00001123 if (SrcVT.bitsLE(MVT::i32)) {
1124 // The integer can be represented exactly in an f64.
Dale Johannesen91b49b92009-01-31 00:43:08 +00001125 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
Dale Johannesen7e298ed2008-11-12 18:38:44 +00001126 MVT::i32, Src);
Duncan Sands041cde22008-06-25 20:24:48 +00001127 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
Dale Johannesen91b49b92009-01-31 00:43:08 +00001128 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
Duncan Sands041cde22008-06-25 20:24:48 +00001129 } else {
1130 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1131 if (SrcVT.bitsLE(MVT::i64)) {
Dale Johannesen91b49b92009-01-31 00:43:08 +00001132 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
Dale Johannesen7e298ed2008-11-12 18:38:44 +00001133 MVT::i64, Src);
Duncan Sands041cde22008-06-25 20:24:48 +00001134 LC = RTLIB::SINTTOFP_I64_PPCF128;
1135 } else if (SrcVT.bitsLE(MVT::i128)) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001136 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
Duncan Sands041cde22008-06-25 20:24:48 +00001137 LC = RTLIB::SINTTOFP_I128_PPCF128;
1138 }
1139 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1140
Dale Johannesenc8fc99d2009-01-31 00:11:23 +00001141 Hi = MakeLibCall(LC, VT, &Src, 1, true, dl);
Duncan Sands86813ce2008-10-29 06:31:03 +00001142 assert(Hi.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1143 "Call lowered wrongly!");
Duncan Sands041cde22008-06-25 20:24:48 +00001144 Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
1145 }
1146
Dale Johannesen7e298ed2008-11-12 18:38:44 +00001147 if (isSigned)
Duncan Sands041cde22008-06-25 20:24:48 +00001148 return;
1149
1150 // Unsigned - fix up the SINT_TO_FP value just calculated.
Dale Johannesen91b49b92009-01-31 00:43:08 +00001151 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Duncan Sands041cde22008-06-25 20:24:48 +00001152 SrcVT = Src.getValueType();
1153
1154 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1155 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1156 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1157 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1158 const uint64_t *Parts = 0;
1159
1160 switch (SrcVT.getSimpleVT()) {
1161 default:
1162 assert(false && "Unsupported UINT_TO_FP!");
1163 case MVT::i32:
1164 Parts = TwoE32;
Dale Johannesenbc3c4292008-11-07 19:11:43 +00001165 break;
Duncan Sands041cde22008-06-25 20:24:48 +00001166 case MVT::i64:
1167 Parts = TwoE64;
Dale Johannesenbc3c4292008-11-07 19:11:43 +00001168 break;
Duncan Sands041cde22008-06-25 20:24:48 +00001169 case MVT::i128:
1170 Parts = TwoE128;
Dale Johannesenbc3c4292008-11-07 19:11:43 +00001171 break;
Duncan Sands041cde22008-06-25 20:24:48 +00001172 }
1173
Dale Johannesen91b49b92009-01-31 00:43:08 +00001174 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
Duncan Sands041cde22008-06-25 20:24:48 +00001175 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
1176 MVT::ppcf128));
Duncan Sands7fb08582009-02-02 19:46:41 +00001177 Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
Dale Johannesen91b49b92009-01-31 00:43:08 +00001178 Lo, Hi, DAG.getCondCode(ISD::SETLT));
1179 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Lo, DAG.getIntPtrConstant(1));
1180 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Lo, DAG.getIntPtrConstant(0));
Duncan Sands041cde22008-06-25 20:24:48 +00001181}
1182
Duncan Sands69b01e92008-06-17 14:27:01 +00001183
1184//===----------------------------------------------------------------------===//
1185// Float Operand Expansion
1186//===----------------------------------------------------------------------===//
1187
1188/// ExpandFloatOperand - This method is called when the specified operand of the
1189/// specified node is found to need expansion. At this point, all of the result
1190/// types of the node are known to be legal, but other operands of the node may
1191/// need promotion or expansion as well as the specified one.
1192bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1193 DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
Dan Gohman475871a2008-07-27 21:46:04 +00001194 SDValue Res = SDValue();
Duncan Sands69b01e92008-06-17 14:27:01 +00001195
1196 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1197 == TargetLowering::Custom)
Duncan Sandsee4c6192008-10-23 19:34:23 +00001198 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
Duncan Sands69b01e92008-06-17 14:27:01 +00001199
Gabor Greifba36cb52008-08-28 21:40:38 +00001200 if (Res.getNode() == 0) {
Duncan Sands69b01e92008-06-17 14:27:01 +00001201 switch (N->getOpcode()) {
1202 default:
1203 #ifndef NDEBUG
1204 cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
1205 N->dump(&DAG); cerr << "\n";
1206 #endif
1207 assert(0 && "Do not know how to expand this operator's operand!");
1208 abort();
Duncan Sands78cd6492008-06-20 18:40:50 +00001209
1210 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
1211 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1212 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
Duncan Sandsa1ace762008-06-21 17:00:47 +00001213
Duncan Sands05c397d2008-07-16 11:41:33 +00001214 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
Duncan Sands041cde22008-06-25 20:24:48 +00001215 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1216 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1217 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
Duncan Sands05c397d2008-07-16 11:41:33 +00001218 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1219 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1220 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1221 OpNo); break;
Duncan Sands69b01e92008-06-17 14:27:01 +00001222 }
1223 }
1224
1225 // If the result is null, the sub-method took care of registering results etc.
Gabor Greifba36cb52008-08-28 21:40:38 +00001226 if (!Res.getNode()) return false;
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001227
1228 // If the result is N, the sub-method updated N in place. Tell the legalizer
1229 // core about this.
1230 if (Res.getNode() == N)
Duncan Sands69b01e92008-06-17 14:27:01 +00001231 return true;
Duncan Sands69b01e92008-06-17 14:27:01 +00001232
1233 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1234 "Invalid operand expansion");
1235
Dan Gohman475871a2008-07-27 21:46:04 +00001236 ReplaceValueWith(SDValue(N, 0), Res);
Duncan Sands69b01e92008-06-17 14:27:01 +00001237 return false;
Duncan Sandsd8742ee2008-03-12 21:27:04 +00001238}
Duncan Sandsa1ace762008-06-21 17:00:47 +00001239
Duncan Sands11ac7972008-06-25 16:34:21 +00001240/// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1241/// is shared among BR_CC, SELECT_CC, and SETCC handlers.
Dan Gohman475871a2008-07-27 21:46:04 +00001242void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1243 SDValue &NewRHS,
Dale Johannesen91b49b92009-01-31 00:43:08 +00001244 ISD::CondCode &CCCode,
1245 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001246 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Duncan Sands11ac7972008-06-25 16:34:21 +00001247 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1248 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1249
1250 MVT VT = NewLHS.getValueType();
1251 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
1252
1253 // FIXME: This generated code sucks. We want to generate
Dale Johannesen283c6962008-11-03 20:47:45 +00001254 // FCMPU crN, hi1, hi2
Duncan Sands11ac7972008-06-25 16:34:21 +00001255 // BNE crN, L:
Dale Johannesen283c6962008-11-03 20:47:45 +00001256 // FCMPU crN, lo1, lo2
Duncan Sands11ac7972008-06-25 16:34:21 +00001257 // The following can be improved, but not that much.
Dan Gohman475871a2008-07-27 21:46:04 +00001258 SDValue Tmp1, Tmp2, Tmp3;
Dale Johannesen91b49b92009-01-31 00:43:08 +00001259 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001260 LHSHi, RHSHi, ISD::SETOEQ);
Dale Johannesen91b49b92009-01-31 00:43:08 +00001261 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001262 LHSLo, RHSLo, CCCode);
Dale Johannesen91b49b92009-01-31 00:43:08 +00001263 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1264 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001265 LHSHi, RHSHi, ISD::SETUNE);
Dale Johannesen91b49b92009-01-31 00:43:08 +00001266 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001267 LHSHi, RHSHi, CCCode);
Dale Johannesen91b49b92009-01-31 00:43:08 +00001268 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1269 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001270 NewRHS = SDValue(); // LHS is the result, not a compare.
Duncan Sands11ac7972008-06-25 16:34:21 +00001271}
1272
Dan Gohman475871a2008-07-27 21:46:04 +00001273SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1274 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
Duncan Sands11ac7972008-06-25 16:34:21 +00001275 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dale Johannesen91b49b92009-01-31 00:43:08 +00001276 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
Duncan Sands11ac7972008-06-25 16:34:21 +00001277
1278 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1279 // against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001280 if (NewRHS.getNode() == 0) {
Duncan Sands11ac7972008-06-25 16:34:21 +00001281 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1282 CCCode = ISD::SETNE;
1283 }
1284
1285 // Update N to have the operands specified.
Dan Gohman475871a2008-07-27 21:46:04 +00001286 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
Duncan Sands11ac7972008-06-25 16:34:21 +00001287 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1288 N->getOperand(4));
1289}
1290
Dan Gohman475871a2008-07-27 21:46:04 +00001291SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
Duncan Sands05c397d2008-07-16 11:41:33 +00001292 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1293 "Logic only correct for ppcf128!");
Dan Gohman475871a2008-07-27 21:46:04 +00001294 SDValue Lo, Hi;
Duncan Sands05c397d2008-07-16 11:41:33 +00001295 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1296 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen91b49b92009-01-31 00:43:08 +00001297 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
1298 N->getValueType(0), Hi, N->getOperand(1));
Duncan Sands05c397d2008-07-16 11:41:33 +00001299}
1300
Dan Gohman475871a2008-07-27 21:46:04 +00001301SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
Duncan Sandsb2ff8852008-07-17 02:36:29 +00001302 MVT RVT = N->getValueType(0);
Dale Johannesenc8fc99d2009-01-31 00:11:23 +00001303 DebugLoc dl = N->getDebugLoc();
Duncan Sands7fb08582009-02-02 19:46:41 +00001304
Duncan Sands57760d92008-10-28 15:00:32 +00001305 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1306 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1307 if (RVT == MVT::i32) {
1308 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1309 "Logic only correct for ppcf128!");
Dale Johannesen91b49b92009-01-31 00:43:08 +00001310 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
Duncan Sands57760d92008-10-28 15:00:32 +00001311 N->getOperand(0), DAG.getValueType(MVT::f64));
Duncan Sands7fb08582009-02-02 19:46:41 +00001312 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
Dale Johannesen91b49b92009-01-31 00:43:08 +00001313 DAG.getIntPtrConstant(1));
1314 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
Duncan Sands57760d92008-10-28 15:00:32 +00001315 }
1316
Duncan Sandsb2ff8852008-07-17 02:36:29 +00001317 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1318 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
Dale Johannesenc8fc99d2009-01-31 00:11:23 +00001319 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false, dl);
Duncan Sands05c397d2008-07-16 11:41:33 +00001320}
1321
Dan Gohman475871a2008-07-27 21:46:04 +00001322SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
Duncan Sandsb2ff8852008-07-17 02:36:29 +00001323 MVT RVT = N->getValueType(0);
Dale Johannesenc8fc99d2009-01-31 00:11:23 +00001324 DebugLoc dl = N->getDebugLoc();
Duncan Sands57760d92008-10-28 15:00:32 +00001325
1326 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1327 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1328 if (RVT == MVT::i32) {
1329 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1330 "Logic only correct for ppcf128!");
1331 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1332 APFloat APF = APFloat(APInt(128, 2, TwoE31));
1333 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1334 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1335 // FIXME: generated code sucks.
Dale Johannesen91b49b92009-01-31 00:43:08 +00001336 return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
1337 DAG.getNode(ISD::ADD, dl, MVT::i32,
1338 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1339 DAG.getNode(ISD::FSUB, dl,
Duncan Sands57760d92008-10-28 15:00:32 +00001340 MVT::ppcf128,
1341 N->getOperand(0),
1342 Tmp)),
1343 DAG.getConstant(0x80000000, MVT::i32)),
Dale Johannesen91b49b92009-01-31 00:43:08 +00001344 DAG.getNode(ISD::FP_TO_SINT, dl,
1345 MVT::i32, N->getOperand(0)),
Duncan Sands57760d92008-10-28 15:00:32 +00001346 DAG.getCondCode(ISD::SETGE));
1347 }
1348
Duncan Sandsb2ff8852008-07-17 02:36:29 +00001349 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1350 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
Dale Johannesenc8fc99d2009-01-31 00:11:23 +00001351 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false, dl);
Duncan Sands05c397d2008-07-16 11:41:33 +00001352}
1353
Dan Gohman475871a2008-07-27 21:46:04 +00001354SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1355 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
Duncan Sands11ac7972008-06-25 16:34:21 +00001356 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
Dale Johannesen91b49b92009-01-31 00:43:08 +00001357 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
Duncan Sands11ac7972008-06-25 16:34:21 +00001358
1359 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1360 // against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001361 if (NewRHS.getNode() == 0) {
Duncan Sands11ac7972008-06-25 16:34:21 +00001362 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1363 CCCode = ISD::SETNE;
1364 }
1365
1366 // Update N to have the operands specified.
Dan Gohman475871a2008-07-27 21:46:04 +00001367 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
Duncan Sands11ac7972008-06-25 16:34:21 +00001368 N->getOperand(2), N->getOperand(3),
1369 DAG.getCondCode(CCCode));
1370}
1371
Dan Gohman475871a2008-07-27 21:46:04 +00001372SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1373 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
Duncan Sands11ac7972008-06-25 16:34:21 +00001374 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
Dale Johannesen91b49b92009-01-31 00:43:08 +00001375 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
Duncan Sands11ac7972008-06-25 16:34:21 +00001376
1377 // If ExpandSetCCOperands returned a scalar, use it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001378 if (NewRHS.getNode() == 0) {
Duncan Sands11ac7972008-06-25 16:34:21 +00001379 assert(NewLHS.getValueType() == N->getValueType(0) &&
1380 "Unexpected setcc expansion!");
1381 return NewLHS;
1382 }
1383
1384 // Otherwise, update N to have the operands specified.
Dan Gohman475871a2008-07-27 21:46:04 +00001385 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
Duncan Sands11ac7972008-06-25 16:34:21 +00001386 DAG.getCondCode(CCCode));
1387}
1388
Dan Gohman475871a2008-07-27 21:46:04 +00001389SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
Duncan Sandsab09b7e2008-06-23 14:19:45 +00001390 if (ISD::isNormalStore(N))
1391 return ExpandOp_NormalStore(N, OpNo);
Duncan Sandsa1ace762008-06-21 17:00:47 +00001392
1393 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1394 assert(OpNo == 1 && "Can only expand the stored value so far");
1395 StoreSDNode *ST = cast<StoreSDNode>(N);
1396
Dan Gohman475871a2008-07-27 21:46:04 +00001397 SDValue Chain = ST->getChain();
1398 SDValue Ptr = ST->getBasePtr();
Duncan Sandsa1ace762008-06-21 17:00:47 +00001399
1400 MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
1401 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1402 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1403
Dan Gohman475871a2008-07-27 21:46:04 +00001404 SDValue Lo, Hi;
Duncan Sandsa1ace762008-06-21 17:00:47 +00001405 GetExpandedOp(ST->getValue(), Lo, Hi);
1406
Dale Johannesen91b49b92009-01-31 00:43:08 +00001407 return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
Duncan Sandsa1ace762008-06-21 17:00:47 +00001408 ST->getSrcValue(), ST->getSrcValueOffset(),
1409 ST->getMemoryVT(),
1410 ST->isVolatile(), ST->getAlignment());
1411}