Expand small memmovs using inline code. Set the X86 threshold for expanding
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeTypesFloatToInt.cpp
1 //===-- LegalizeTypesFloatToInt.cpp - LegalizeTypes float to int support --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements float to integer conversion for LegalizeTypes.  This
11 // is the act of turning a computation in an invalid floating point type into
12 // a computation in an integer type of the same size.  For example, turning
13 // f32 arithmetic into operations using i32.  Also known as "soft float".
14 // The result is equivalent to bitcasting the float value to the integer type.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "llvm/CodeGen/PseudoSourceValue.h"
19 #include "llvm/DerivedTypes.h"
20 #include "LegalizeTypes.h"
21 using namespace llvm;
22
23 /// GetFPLibCall - Return the right libcall for the given floating point type.
24 static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
25                                    RTLIB::Libcall Call_F32,
26                                    RTLIB::Libcall Call_F64,
27                                    RTLIB::Libcall Call_F80,
28                                    RTLIB::Libcall Call_PPCF128) {
29   return
30     VT == MVT::f32 ? Call_F32 :
31     VT == MVT::f64 ? Call_F64 :
32     VT == MVT::f80 ? Call_F80 :
33     VT == MVT::ppcf128 ? Call_PPCF128 :
34     RTLIB::UNKNOWN_LIBCALL;
35 }
36
37 //===----------------------------------------------------------------------===//
38 //  Result Float to Integer Conversion.
39 //===----------------------------------------------------------------------===//
40
41 void DAGTypeLegalizer::FloatToIntResult(SDNode *N, unsigned ResNo) {
42   DEBUG(cerr << "FloatToInt node result " << ResNo << ": "; N->dump(&DAG);
43         cerr << "\n");
44   SDOperand R = SDOperand();
45
46   // FIXME: Custom lowering for float-to-int?
47 #if 0
48   // See if the target wants to custom convert this node to an integer.
49   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) ==
50       TargetLowering::Custom) {
51     // If the target wants to, allow it to lower this itself.
52     if (SDNode *P = TLI.FloatToIntOperationResult(N, DAG)) {
53       // Everything that once used N now uses P.  We are guaranteed that the
54       // result value types of N and the result value types of P match.
55       ReplaceNodeWith(N, P);
56       return;
57     }
58   }
59 #endif
60
61   switch (N->getOpcode()) {
62   default:
63 #ifndef NDEBUG
64     cerr << "FloatToIntResult #" << ResNo << ": ";
65     N->dump(&DAG); cerr << "\n";
66 #endif
67     assert(0 && "Do not know how to convert the result of this operator!");
68     abort();
69
70     case ISD::BIT_CONVERT: R = FloatToIntRes_BIT_CONVERT(N); break;
71     case ISD::BUILD_PAIR:  R = FloatToIntRes_BUILD_PAIR(N); break;
72     case ISD::ConstantFP:
73       R = FloatToIntRes_ConstantFP(cast<ConstantFPSDNode>(N));
74       break;
75     case ISD::FCOPYSIGN:   R = FloatToIntRes_FCOPYSIGN(N); break;
76     case ISD::LOAD:        R = FloatToIntRes_LOAD(N); break;
77     case ISD::SINT_TO_FP:
78     case ISD::UINT_TO_FP:  R = FloatToIntRes_XINT_TO_FP(N); break;
79
80     case ISD::FADD: R = FloatToIntRes_FADD(N); break;
81     case ISD::FMUL: R = FloatToIntRes_FMUL(N); break;
82     case ISD::FSUB: R = FloatToIntRes_FSUB(N); break;
83   }
84
85   // If R is null, the sub-method took care of registering the result.
86   if (R.Val)
87     SetIntegerOp(SDOperand(N, ResNo), R);
88 }
89
90 SDOperand DAGTypeLegalizer::FloatToIntRes_BIT_CONVERT(SDNode *N) {
91   return BitConvertToInteger(N->getOperand(0));
92 }
93
94 SDOperand DAGTypeLegalizer::FloatToIntRes_BUILD_PAIR(SDNode *N) {
95   // Convert the inputs to integers, and build a new pair out of them.
96   return DAG.getNode(ISD::BUILD_PAIR,
97                      TLI.getTypeToTransformTo(N->getValueType(0)),
98                      BitConvertToInteger(N->getOperand(0)),
99                      BitConvertToInteger(N->getOperand(1)));
100 }
101
102 SDOperand DAGTypeLegalizer::FloatToIntRes_ConstantFP(ConstantFPSDNode *N) {
103   return DAG.getConstant(N->getValueAPF().convertToAPInt(),
104                          TLI.getTypeToTransformTo(N->getValueType(0)));
105 }
106
107 SDOperand DAGTypeLegalizer::FloatToIntRes_FADD(SDNode *N) {
108   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
109   SDOperand Ops[2] = { GetIntegerOp(N->getOperand(0)),
110                        GetIntegerOp(N->getOperand(1)) };
111   return MakeLibCall(GetFPLibCall(N->getValueType(0),
112                                   RTLIB::ADD_F32,
113                                   RTLIB::ADD_F64,
114                                   RTLIB::ADD_F80,
115                                   RTLIB::ADD_PPCF128),
116                      NVT, Ops, 2, false/*sign irrelevant*/);
117 }
118
119 SDOperand DAGTypeLegalizer::FloatToIntRes_FCOPYSIGN(SDNode *N) {
120   SDOperand LHS = GetIntegerOp(N->getOperand(0));
121   SDOperand RHS = BitConvertToInteger(N->getOperand(1));
122
123   MVT::ValueType LVT = LHS.getValueType();
124   MVT::ValueType RVT = RHS.getValueType();
125
126   unsigned LSize = MVT::getSizeInBits(LVT);
127   unsigned RSize = MVT::getSizeInBits(RVT);
128
129   // First get the sign bit of second operand.
130   SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
131                                   DAG.getConstant(RSize - 1,
132                                                   TLI.getShiftAmountTy()));
133   SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
134
135   // Shift right or sign-extend it if the two operands have different types.
136   int SizeDiff = MVT::getSizeInBits(RVT) - MVT::getSizeInBits(LVT);
137   if (SizeDiff > 0) {
138     SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
139                           DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
140     SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
141   } else if (SizeDiff < 0) {
142     SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
143     SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
144                           DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
145   }
146
147   // Clear the sign bit of the first operand.
148   SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
149                                DAG.getConstant(LSize - 1,
150                                                TLI.getShiftAmountTy()));
151   Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
152   LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
153
154   // Or the value with the sign bit.
155   return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
156 }
157
158 SDOperand DAGTypeLegalizer::FloatToIntRes_FMUL(SDNode *N) {
159   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
160   SDOperand Ops[2] = { GetIntegerOp(N->getOperand(0)),
161                        GetIntegerOp(N->getOperand(1)) };
162   return MakeLibCall(GetFPLibCall(N->getValueType(0),
163                                   RTLIB::MUL_F32,
164                                   RTLIB::MUL_F64,
165                                   RTLIB::MUL_F80,
166                                   RTLIB::MUL_PPCF128),
167                      NVT, Ops, 2, false/*sign irrelevant*/);
168 }
169
170 SDOperand DAGTypeLegalizer::FloatToIntRes_FSUB(SDNode *N) {
171   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
172   SDOperand Ops[2] = { GetIntegerOp(N->getOperand(0)),
173                        GetIntegerOp(N->getOperand(1)) };
174   return MakeLibCall(GetFPLibCall(N->getValueType(0),
175                                   RTLIB::SUB_F32,
176                                   RTLIB::SUB_F64,
177                                   RTLIB::SUB_F80,
178                                   RTLIB::SUB_PPCF128),
179                      NVT, Ops, 2, false/*sign irrelevant*/);
180 }
181
182 SDOperand DAGTypeLegalizer::FloatToIntRes_LOAD(SDNode *N) {
183   LoadSDNode *L = cast<LoadSDNode>(N);
184   MVT::ValueType VT = N->getValueType(0);
185   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
186
187   if (L->getExtensionType() == ISD::NON_EXTLOAD)
188      return DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
189                         NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
190                         L->getSrcValue(), L->getSrcValueOffset(), NVT,
191                         L->isVolatile(), L->getAlignment());
192
193   // Do a non-extending load followed by FP_EXTEND.
194   SDOperand NL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
195                              L->getMemoryVT(), L->getChain(),
196                              L->getBasePtr(), L->getOffset(),
197                              L->getSrcValue(), L->getSrcValueOffset(),
198                              L->getMemoryVT(),
199                              L->isVolatile(), L->getAlignment());
200   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NL));
201 }
202
203 SDOperand DAGTypeLegalizer::FloatToIntRes_XINT_TO_FP(SDNode *N) {
204   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
205   MVT::ValueType DestVT = N->getValueType(0);
206   SDOperand Op = N->getOperand(0);
207
208   if (Op.getValueType() == MVT::i32) {
209     // simple 32-bit [signed|unsigned] integer to float/double expansion
210
211     // Get the stack frame index of a 8 byte buffer.
212     SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
213
214     // word offset constant for Hi/Lo address computation
215     SDOperand Offset = DAG.getConstant(MVT::getSizeInBits(MVT::i32) / 8,
216                                        TLI.getPointerTy());
217     // set up Hi and Lo (into buffer) address based on endian
218     SDOperand Hi = StackSlot;
219     SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, Offset);
220     if (TLI.isLittleEndian())
221       std::swap(Hi, Lo);
222
223     // if signed map to unsigned space
224     SDOperand OpMapped;
225     if (isSigned) {
226       // constant used to invert sign bit (signed to unsigned mapping)
227       SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
228       OpMapped = DAG.getNode(ISD::XOR, MVT::i32, Op, SignBit);
229     } else {
230       OpMapped = Op;
231     }
232     // store the lo of the constructed double - based on integer input
233     SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
234                                     OpMapped, Lo, NULL, 0);
235     // initial hi portion of constructed double
236     SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
237     // store the hi of the constructed double - biased exponent
238     SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
239     // load the constructed double
240     SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
241     // FP constant to bias correct the final result
242     SDOperand Bias = DAG.getConstantFP(isSigned ?
243                                             BitsToDouble(0x4330000080000000ULL)
244                                           : BitsToDouble(0x4330000000000000ULL),
245                                      MVT::f64);
246     // subtract the bias
247     SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
248     // final result
249     SDOperand Result;
250     // handle final rounding
251     if (DestVT == MVT::f64) {
252       // do nothing
253       Result = Sub;
254     } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
255       Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
256                            DAG.getIntPtrConstant(0));
257     } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
258       Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
259     }
260     return BitConvertToInteger(Result);
261   }
262   assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
263   SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op);
264
265   SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op), Op,
266                                    DAG.getConstant(0, Op.getValueType()),
267                                    ISD::SETLT);
268   SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
269   SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
270                                     SignSet, Four, Zero);
271
272   // If the sign bit of the integer is set, the large number will be treated
273   // as a negative number.  To counteract this, the dynamic code adds an
274   // offset depending on the data type.
275   uint64_t FF;
276   switch (Op.getValueType()) {
277   default: assert(0 && "Unsupported integer type!");
278   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
279   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
280   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
281   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
282   }
283   if (TLI.isLittleEndian()) FF <<= 32;
284   static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
285
286   SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
287   CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
288   SDOperand FudgeInReg;
289   if (DestVT == MVT::f32)
290     FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
291                              PseudoSourceValue::getConstantPool(), 0);
292   else {
293     FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestVT,
294                                 DAG.getEntryNode(), CPIdx,
295                                 PseudoSourceValue::getConstantPool(), 0,
296                                 MVT::f32);
297   }
298
299   return BitConvertToInteger(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
300 }
301
302
303 //===----------------------------------------------------------------------===//
304 //  Operand Float to Integer Conversion..
305 //===----------------------------------------------------------------------===//
306
307 bool DAGTypeLegalizer::FloatToIntOperand(SDNode *N, unsigned OpNo) {
308   DEBUG(cerr << "FloatToInt node operand " << OpNo << ": "; N->dump(&DAG);
309         cerr << "\n");
310   SDOperand Res(0, 0);
311
312   // FIXME: Custom lowering for float-to-int?
313 #if 0
314   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
315       == TargetLowering::Custom)
316     Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
317 #endif
318
319   if (Res.Val == 0) {
320     switch (N->getOpcode()) {
321     default:
322 #ifndef NDEBUG
323       cerr << "FloatToIntOperand Op #" << OpNo << ": ";
324       N->dump(&DAG); cerr << "\n";
325 #endif
326       assert(0 && "Do not know how to convert this operator's operand!");
327       abort();
328
329       case ISD::BIT_CONVERT: Res = FloatToIntOp_BIT_CONVERT(N); break;
330     }
331   }
332
333   // If the result is null, the sub-method took care of registering results etc.
334   if (!Res.Val) return false;
335
336   // If the result is N, the sub-method updated N in place.  Check to see if any
337   // operands are new, and if so, mark them.
338   if (Res.Val == N) {
339     // Mark N as new and remark N and its operands.  This allows us to correctly
340     // revisit N if it needs another step of promotion and allows us to visit
341     // any new operands to N.
342     ReanalyzeNode(N);
343     return true;
344   }
345
346   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
347          "Invalid operand expansion");
348
349   ReplaceValueWith(SDOperand(N, 0), Res);
350   return false;
351 }
352
353 SDOperand DAGTypeLegalizer::FloatToIntOp_BIT_CONVERT(SDNode *N) {
354   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
355                      GetIntegerOp(N->getOperand(0)));
356 }