Rather than having a different custom legalization
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeFloatTypes.cpp
1 //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements float type expansion and softening for LegalizeTypes.
11 // Softening is the act of turning a computation in an illegal floating point
12 // type into a computation in an integer type of the same size; also known as
13 // "soft float".  For example, turning f32 arithmetic into operations using i32.
14 // The resulting integer value is the same as what you would get by performing
15 // the floating point operation and bitcasting the result to the integer type.
16 // Expansion is the act of changing a computation in an illegal type to be a
17 // computation in two identical registers of a smaller type.  For example,
18 // implementing ppcf128 arithmetic in two f64 registers.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "LegalizeTypes.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 using namespace llvm;
27
28 /// GetFPLibCall - Return the right libcall for the given floating point type.
29 static RTLIB::Libcall GetFPLibCall(MVT VT,
30                                    RTLIB::Libcall Call_F32,
31                                    RTLIB::Libcall Call_F64,
32                                    RTLIB::Libcall Call_F80,
33                                    RTLIB::Libcall Call_PPCF128) {
34   return
35     VT == MVT::f32 ? Call_F32 :
36     VT == MVT::f64 ? Call_F64 :
37     VT == MVT::f80 ? Call_F80 :
38     VT == MVT::ppcf128 ? Call_PPCF128 :
39     RTLIB::UNKNOWN_LIBCALL;
40 }
41
42 //===----------------------------------------------------------------------===//
43 //  Result Float to Integer Conversion.
44 //===----------------------------------------------------------------------===//
45
46 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
47   DEBUG(cerr << "Soften float result " << ResNo << ": "; N->dump(&DAG);
48         cerr << "\n");
49   SDOperand R = SDOperand();
50
51   // See if the target wants to custom expand this node.
52   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
53       TargetLowering::Custom) {
54     // If the target wants to, allow it to lower this itself.
55     if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
56       // Everything that once used N now uses P.  We are guaranteed that the
57       // result value types of N and the result value types of P match.
58       ReplaceNodeWith(N, P);
59       return;
60     }
61   }
62
63   switch (N->getOpcode()) {
64   default:
65 #ifndef NDEBUG
66     cerr << "SoftenFloatResult #" << ResNo << ": ";
67     N->dump(&DAG); cerr << "\n";
68 #endif
69     assert(0 && "Do not know how to convert the result of this operator!");
70     abort();
71
72     case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
73     case ISD::BUILD_PAIR:  R = SoftenFloatRes_BUILD_PAIR(N); break;
74     case ISD::ConstantFP:
75       R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
76       break;
77     case ISD::FCOPYSIGN:   R = SoftenFloatRes_FCOPYSIGN(N); break;
78     case ISD::LOAD:        R = SoftenFloatRes_LOAD(N); break;
79     case ISD::SINT_TO_FP:
80     case ISD::UINT_TO_FP:  R = SoftenFloatRes_XINT_TO_FP(N); break;
81
82     case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
83     case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
84     case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
85   }
86
87   // If R is null, the sub-method took care of registering the result.
88   if (R.Val)
89     SetSoftenedFloat(SDOperand(N, ResNo), R);
90 }
91
92 SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
93   return BitConvertToInteger(N->getOperand(0));
94 }
95
96 SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
97   // Convert the inputs to integers, and build a new pair out of them.
98   return DAG.getNode(ISD::BUILD_PAIR,
99                      TLI.getTypeToTransformTo(N->getValueType(0)),
100                      BitConvertToInteger(N->getOperand(0)),
101                      BitConvertToInteger(N->getOperand(1)));
102 }
103
104 SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
105   return DAG.getConstant(N->getValueAPF().convertToAPInt(),
106                          TLI.getTypeToTransformTo(N->getValueType(0)));
107 }
108
109 SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
110   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
111   SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
112                        GetSoftenedFloat(N->getOperand(1)) };
113   return MakeLibCall(GetFPLibCall(N->getValueType(0),
114                                   RTLIB::ADD_F32,
115                                   RTLIB::ADD_F64,
116                                   RTLIB::ADD_F80,
117                                   RTLIB::ADD_PPCF128),
118                      NVT, Ops, 2, false);
119 }
120
121 SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
122   SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
123   SDOperand RHS = BitConvertToInteger(N->getOperand(1));
124
125   MVT LVT = LHS.getValueType();
126   MVT RVT = RHS.getValueType();
127
128   unsigned LSize = LVT.getSizeInBits();
129   unsigned RSize = RVT.getSizeInBits();
130
131   // First get the sign bit of second operand.
132   SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
133                                   DAG.getConstant(RSize - 1,
134                                                   TLI.getShiftAmountTy()));
135   SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
136
137   // Shift right or sign-extend it if the two operands have different types.
138   int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
139   if (SizeDiff > 0) {
140     SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
141                           DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
142     SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
143   } else if (SizeDiff < 0) {
144     SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
145     SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
146                           DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
147   }
148
149   // Clear the sign bit of the first operand.
150   SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
151                                DAG.getConstant(LSize - 1,
152                                                TLI.getShiftAmountTy()));
153   Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
154   LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
155
156   // Or the value with the sign bit.
157   return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
158 }
159
160 SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
161   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
162   SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
163                        GetSoftenedFloat(N->getOperand(1)) };
164   return MakeLibCall(GetFPLibCall(N->getValueType(0),
165                                   RTLIB::MUL_F32,
166                                   RTLIB::MUL_F64,
167                                   RTLIB::MUL_F80,
168                                   RTLIB::MUL_PPCF128),
169                      NVT, Ops, 2, false);
170 }
171
172 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
173   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
174   SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
175                        GetSoftenedFloat(N->getOperand(1)) };
176   return MakeLibCall(GetFPLibCall(N->getValueType(0),
177                                   RTLIB::SUB_F32,
178                                   RTLIB::SUB_F64,
179                                   RTLIB::SUB_F80,
180                                   RTLIB::SUB_PPCF128),
181                      NVT, Ops, 2, false);
182 }
183
184 SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
185   LoadSDNode *L = cast<LoadSDNode>(N);
186   MVT VT = N->getValueType(0);
187   MVT NVT = TLI.getTypeToTransformTo(VT);
188
189   if (L->getExtensionType() == ISD::NON_EXTLOAD)
190      return DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
191                         NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
192                         L->getSrcValue(), L->getSrcValueOffset(), NVT,
193                         L->isVolatile(), L->getAlignment());
194
195   // Do a non-extending load followed by FP_EXTEND.
196   SDOperand NL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
197                              L->getMemoryVT(), L->getChain(),
198                              L->getBasePtr(), L->getOffset(),
199                              L->getSrcValue(), L->getSrcValueOffset(),
200                              L->getMemoryVT(),
201                              L->isVolatile(), L->getAlignment());
202   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NL));
203 }
204
205 SDOperand DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
206   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
207   MVT DestVT = N->getValueType(0);
208   SDOperand Op = N->getOperand(0);
209
210   if (Op.getValueType() == MVT::i32) {
211     // simple 32-bit [signed|unsigned] integer to float/double expansion
212
213     // Get the stack frame index of a 8 byte buffer.
214     SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
215
216     // word offset constant for Hi/Lo address computation
217     SDOperand Offset =
218       DAG.getConstant(MVT(MVT::i32).getSizeInBits() / 8,
219                       TLI.getPointerTy());
220     // set up Hi and Lo (into buffer) address based on endian
221     SDOperand Hi = StackSlot;
222     SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, Offset);
223     if (TLI.isLittleEndian())
224       std::swap(Hi, Lo);
225
226     // if signed map to unsigned space
227     SDOperand OpMapped;
228     if (isSigned) {
229       // constant used to invert sign bit (signed to unsigned mapping)
230       SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
231       OpMapped = DAG.getNode(ISD::XOR, MVT::i32, Op, SignBit);
232     } else {
233       OpMapped = Op;
234     }
235     // store the lo of the constructed double - based on integer input
236     SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
237                                     OpMapped, Lo, NULL, 0);
238     // initial hi portion of constructed double
239     SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
240     // store the hi of the constructed double - biased exponent
241     SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
242     // load the constructed double
243     SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
244     // FP constant to bias correct the final result
245     SDOperand Bias = DAG.getConstantFP(isSigned ?
246                                             BitsToDouble(0x4330000080000000ULL)
247                                           : BitsToDouble(0x4330000000000000ULL),
248                                      MVT::f64);
249     // subtract the bias
250     SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
251     // final result
252     SDOperand Result;
253     // handle final rounding
254     if (DestVT == MVT::f64) {
255       // do nothing
256       Result = Sub;
257     } else if (DestVT.bitsLT(MVT::f64)) {
258       Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
259                            DAG.getIntPtrConstant(0));
260     } else if (DestVT.bitsGT(MVT::f64)) {
261       Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
262     }
263     return BitConvertToInteger(Result);
264   }
265   assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
266   SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op);
267
268   SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op), Op,
269                                    DAG.getConstant(0, Op.getValueType()),
270                                    ISD::SETLT);
271   SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
272   SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
273                                     SignSet, Four, Zero);
274
275   // If the sign bit of the integer is set, the large number will be treated
276   // as a negative number.  To counteract this, the dynamic code adds an
277   // offset depending on the data type.
278   uint64_t FF;
279   switch (Op.getValueType().getSimpleVT()) {
280   default: assert(0 && "Unsupported integer type!");
281   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
282   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
283   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
284   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
285   }
286   if (TLI.isLittleEndian()) FF <<= 32;
287   static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
288
289   SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
290   CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
291   SDOperand FudgeInReg;
292   if (DestVT == MVT::f32)
293     FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
294                              PseudoSourceValue::getConstantPool(), 0);
295   else {
296     FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestVT,
297                                 DAG.getEntryNode(), CPIdx,
298                                 PseudoSourceValue::getConstantPool(), 0,
299                                 MVT::f32);
300   }
301
302   return BitConvertToInteger(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
303 }
304
305
306 //===----------------------------------------------------------------------===//
307 //  Operand Float to Integer Conversion..
308 //===----------------------------------------------------------------------===//
309
310 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
311   DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
312         cerr << "\n");
313   SDOperand Res(0, 0);
314
315   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
316       == TargetLowering::Custom)
317     Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
318
319   if (Res.Val == 0) {
320     switch (N->getOpcode()) {
321     default:
322 #ifndef NDEBUG
323       cerr << "SoftenFloatOperand 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 = SoftenFloatOp_BIT_CONVERT(N); break;
330
331     case ISD::BR_CC:     Res = SoftenFloatOp_BR_CC(N); break;
332     case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
333     case ISD::SETCC:     Res = SoftenFloatOp_SETCC(N); break;
334     }
335   }
336
337   // If the result is null, the sub-method took care of registering results etc.
338   if (!Res.Val) return false;
339
340   // If the result is N, the sub-method updated N in place.  Check to see if any
341   // operands are new, and if so, mark them.
342   if (Res.Val == N) {
343     // Mark N as new and remark N and its operands.  This allows us to correctly
344     // revisit N if it needs another step of promotion and allows us to visit
345     // any new operands to N.
346     ReanalyzeNode(N);
347     return true;
348   }
349
350   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
351          "Invalid operand expansion");
352
353   ReplaceValueWith(SDOperand(N, 0), Res);
354   return false;
355 }
356
357 /// SoftenSetCCOperands - Soften the operands of a comparison.  This code is
358 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
359 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
360                                            ISD::CondCode &CCCode) {
361   SDOperand LHSInt = GetSoftenedFloat(NewLHS);
362   SDOperand RHSInt = GetSoftenedFloat(NewRHS);
363   MVT VT  = NewLHS.getValueType();
364   MVT NVT = LHSInt.getValueType();
365
366   assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
367
368   // Expand into one or more soft-fp libcall(s).
369   RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
370   switch (CCCode) {
371   case ISD::SETEQ:
372   case ISD::SETOEQ:
373     LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
374     break;
375   case ISD::SETNE:
376   case ISD::SETUNE:
377     LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
378     break;
379   case ISD::SETGE:
380   case ISD::SETOGE:
381     LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
382     break;
383   case ISD::SETLT:
384   case ISD::SETOLT:
385     LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
386     break;
387   case ISD::SETLE:
388   case ISD::SETOLE:
389     LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
390     break;
391   case ISD::SETGT:
392   case ISD::SETOGT:
393     LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
394     break;
395   case ISD::SETUO:
396     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
397     break;
398   case ISD::SETO:
399     break;
400   default:
401     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
402     switch (CCCode) {
403     case ISD::SETONE:
404       // SETONE = SETOLT | SETOGT
405       LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
406       // Fallthrough
407     case ISD::SETUGT:
408       LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
409       break;
410     case ISD::SETUGE:
411       LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
412       break;
413     case ISD::SETULT:
414       LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
415       break;
416     case ISD::SETULE:
417       LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
418       break;
419     case ISD::SETUEQ:
420       LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
421       break;
422     default: assert(false && "Do not know how to soften this setcc!");
423     }
424   }
425
426   SDOperand Ops[2] = { LHSInt, RHSInt };
427   NewLHS = MakeLibCall(LC1, NVT, Ops, 2, false/*sign irrelevant*/);
428   NewRHS = DAG.getConstant(0, NVT);
429   if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
430     SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
431                                 NewLHS, NewRHS,
432                                 DAG.getCondCode(TLI.getCmpLibcallCC(LC1)));
433     NewLHS = MakeLibCall(LC2, NVT, Ops, 2, false/*sign irrelevant*/);
434     NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
435                          NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
436     NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
437     NewRHS = SDOperand();
438   }
439 }
440
441 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
442   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
443                      GetSoftenedFloat(N->getOperand(0)));
444 }
445
446 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
447   SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
448   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
449   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
450
451   // If SoftenSetCCOperands returned a scalar, we need to compare the result
452   // against zero to select between true and false values.
453   if (NewRHS.Val == 0) {
454     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
455     CCCode = ISD::SETNE;
456   }
457
458   // Update N to have the operands specified.
459   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
460                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
461                                 N->getOperand(4));
462 }
463
464 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
465   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
466   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
467   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
468
469   // If SoftenSetCCOperands returned a scalar, we need to compare the result
470   // against zero to select between true and false values.
471   if (NewRHS.Val == 0) {
472     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
473     CCCode = ISD::SETNE;
474   }
475
476   // Update N to have the operands specified.
477   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
478                                 N->getOperand(2), N->getOperand(3),
479                                 DAG.getCondCode(CCCode));
480 }
481
482 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
483   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
484   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
485   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
486
487   // If SoftenSetCCOperands returned a scalar, use it.
488   if (NewRHS.Val == 0) {
489     assert(NewLHS.getValueType() == N->getValueType(0) &&
490            "Unexpected setcc expansion!");
491     return NewLHS;
492   }
493
494   // Otherwise, update N to have the operands specified.
495   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
496                                 DAG.getCondCode(CCCode));
497 }
498
499
500 //===----------------------------------------------------------------------===//
501 //  Float Result Expansion
502 //===----------------------------------------------------------------------===//
503
504 /// ExpandFloatResult - This method is called when the specified result of the
505 /// specified node is found to need expansion.  At this point, the node may also
506 /// have invalid operands or may have other results that need promotion, we just
507 /// know that (at least) one result needs expansion.
508 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
509   DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
510   SDOperand Lo, Hi;
511   Lo = Hi = SDOperand();
512
513   // See if the target wants to custom expand this node.
514   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
515       TargetLowering::Custom) {
516     // If the target wants to, allow it to lower this itself.
517     if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
518       // Everything that once used N now uses P.  We are guaranteed that the
519       // result value types of N and the result value types of P match.
520       ReplaceNodeWith(N, P);
521       return;
522     }
523   }
524
525   switch (N->getOpcode()) {
526   default:
527 #ifndef NDEBUG
528     cerr << "ExpandFloatResult #" << ResNo << ": ";
529     N->dump(&DAG); cerr << "\n";
530 #endif
531     assert(0 && "Do not know how to expand the result of this operator!");
532     abort();
533
534   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
535   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
536   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
537   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
538
539   case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
540   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
541   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
542   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
543
544   case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
545   case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
546   case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
547   case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
548   case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
549   case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
550   case ISD::SINT_TO_FP:
551   case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
552   }
553
554   // If Lo/Hi is null, the sub-method took care of registering results etc.
555   if (Lo.Val)
556     SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
557 }
558
559 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
560                                                  SDOperand &Hi) {
561   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
562   assert(NVT.getSizeInBits() == integerPartWidth &&
563          "Do not know how to expand this float constant!");
564   APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
565   Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
566                                        &C.getRawData()[1])), NVT);
567   Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
568                                        &C.getRawData()[0])), NVT);
569 }
570
571 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
572                                            SDOperand &Hi) {
573   SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
574   SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
575                                             RTLIB::ADD_F32,
576                                             RTLIB::ADD_F64,
577                                             RTLIB::ADD_F80,
578                                             RTLIB::ADD_PPCF128),
579                                N->getValueType(0), Ops, 2,
580                                false);
581   assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
582   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
583 }
584
585 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
586                                            SDOperand &Hi) {
587   SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
588   SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
589                                             RTLIB::DIV_F32,
590                                             RTLIB::DIV_F64,
591                                             RTLIB::DIV_F80,
592                                             RTLIB::DIV_PPCF128),
593                                N->getValueType(0), Ops, 2,
594                                false);
595   assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
596   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
597 }
598
599 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
600                                            SDOperand &Hi) {
601   SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
602   SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
603                                             RTLIB::MUL_F32,
604                                             RTLIB::MUL_F64,
605                                             RTLIB::MUL_F80,
606                                             RTLIB::MUL_PPCF128),
607                                N->getValueType(0), Ops, 2,
608                                false);
609   assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
610   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
611 }
612
613 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
614                                            SDOperand &Hi) {
615   SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
616   SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
617                                             RTLIB::SUB_F32,
618                                             RTLIB::SUB_F64,
619                                             RTLIB::SUB_F80,
620                                             RTLIB::SUB_PPCF128),
621                                N->getValueType(0), Ops, 2,
622                                false);
623   assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
624   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
625 }
626
627 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
628                                            SDOperand &Hi) {
629   if (ISD::isNormalLoad(N)) {
630     ExpandRes_NormalLoad(N, Lo, Hi);
631     return;
632   }
633
634   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
635   LoadSDNode *LD = cast<LoadSDNode>(N);
636   SDOperand Chain = LD->getChain();
637   SDOperand Ptr = LD->getBasePtr();
638
639   MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
640   assert(NVT.isByteSized() && "Expanded type not byte sized!");
641   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
642
643   Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
644                       LD->getSrcValue(), LD->getSrcValueOffset(),
645                       LD->getMemoryVT(),
646                       LD->isVolatile(), LD->getAlignment());
647
648   // Remember the chain.
649   Chain = Lo.getValue(1);
650
651   // The high part is undefined.
652   Hi = DAG.getNode(ISD::UNDEF, NVT);
653
654   // Modified the chain - switch anything that used the old chain to use the
655   // new one.
656   ReplaceValueWith(SDOperand(LD, 1), Chain);
657 }
658
659 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
660                                                  SDOperand &Hi) {
661   assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
662   MVT VT = N->getValueType(0);
663   MVT NVT = TLI.getTypeToTransformTo(VT);
664   SDOperand Src = N->getOperand(0);
665   MVT SrcVT = Src.getValueType();
666
667   // First do an SINT_TO_FP, whether the original was signed or unsigned.
668   if (SrcVT.bitsLE(MVT::i32)) {
669     // The integer can be represented exactly in an f64.
670     Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
671     Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
672     Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
673   } else {
674     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
675     if (SrcVT.bitsLE(MVT::i64)) {
676       Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
677       LC = RTLIB::SINTTOFP_I64_PPCF128;
678     } else if (SrcVT.bitsLE(MVT::i128)) {
679       Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
680       LC = RTLIB::SINTTOFP_I128_PPCF128;
681     }
682     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
683
684     Hi = MakeLibCall(LC, VT, &Src, 1, true);
685     assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
686     Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
687   }
688
689   if (N->getOpcode() == ISD::SINT_TO_FP)
690     return;
691
692   // Unsigned - fix up the SINT_TO_FP value just calculated.
693   Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
694   SrcVT = Src.getValueType();
695
696   // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
697   static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
698   static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
699   static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
700   const uint64_t *Parts = 0;
701
702   switch (SrcVT.getSimpleVT()) {
703   default:
704     assert(false && "Unsupported UINT_TO_FP!");
705   case MVT::i32:
706     Parts = TwoE32;
707   case MVT::i64:
708     Parts = TwoE64;
709   case MVT::i128:
710     Parts = TwoE128;
711   }
712
713   Lo = DAG.getNode(ISD::FADD, VT, Hi,
714                    DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
715                                      MVT::ppcf128));
716   Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
717                    DAG.getCondCode(ISD::SETLT));
718   Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
719                    DAG.getConstant(1, TLI.getPointerTy()));
720   Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
721                    DAG.getConstant(0, TLI.getPointerTy()));
722 }
723
724
725 //===----------------------------------------------------------------------===//
726 //  Float Operand Expansion
727 //===----------------------------------------------------------------------===//
728
729 /// ExpandFloatOperand - This method is called when the specified operand of the
730 /// specified node is found to need expansion.  At this point, all of the result
731 /// types of the node are known to be legal, but other operands of the node may
732 /// need promotion or expansion as well as the specified one.
733 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
734   DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
735   SDOperand Res(0, 0);
736
737   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
738       == TargetLowering::Custom)
739     Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
740
741   if (Res.Val == 0) {
742     switch (N->getOpcode()) {
743     default:
744   #ifndef NDEBUG
745       cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
746       N->dump(&DAG); cerr << "\n";
747   #endif
748       assert(0 && "Do not know how to expand this operator's operand!");
749       abort();
750
751     case ISD::BIT_CONVERT:     Res = ExpandOp_BIT_CONVERT(N); break;
752     case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
753     case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
754
755     case ISD::BR_CC:     Res = ExpandFloatOp_BR_CC(N); break;
756     case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
757     case ISD::SETCC:     Res = ExpandFloatOp_SETCC(N); break;
758
759     case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
760     case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
761     case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
762
763     case ISD::STORE:
764       Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
765       break;
766     }
767   }
768
769   // If the result is null, the sub-method took care of registering results etc.
770   if (!Res.Val) return false;
771   // If the result is N, the sub-method updated N in place.  Check to see if any
772   // operands are new, and if so, mark them.
773   if (Res.Val == N) {
774     // Mark N as new and remark N and its operands.  This allows us to correctly
775     // revisit N if it needs another step of expansion and allows us to visit
776     // any new operands to N.
777     ReanalyzeNode(N);
778     return true;
779   }
780
781   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
782          "Invalid operand expansion");
783
784   ReplaceValueWith(SDOperand(N, 0), Res);
785   return false;
786 }
787
788 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
789 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
790 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
791                                                 SDOperand &NewRHS,
792                                                 ISD::CondCode &CCCode) {
793   SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
794   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
795   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
796
797   MVT VT = NewLHS.getValueType();
798   assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
799
800   // FIXME:  This generated code sucks.  We want to generate
801   //         FCMP crN, hi1, hi2
802   //         BNE crN, L:
803   //         FCMP crN, lo1, lo2
804   // The following can be improved, but not that much.
805   SDOperand Tmp1, Tmp2, Tmp3;
806   Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
807   Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
808   Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
809   Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
810   Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
811   Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
812   NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
813   NewRHS = SDOperand();   // LHS is the result, not a compare.
814 }
815
816 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
817   SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
818   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
819   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
820
821   // If ExpandSetCCOperands returned a scalar, we need to compare the result
822   // against zero to select between true and false values.
823   if (NewRHS.Val == 0) {
824     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
825     CCCode = ISD::SETNE;
826   }
827
828   // Update N to have the operands specified.
829   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
830                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
831                                 N->getOperand(4));
832 }
833
834 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
835   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
836   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
837   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
838
839   // If ExpandSetCCOperands returned a scalar, we need to compare the result
840   // against zero to select between true and false values.
841   if (NewRHS.Val == 0) {
842     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
843     CCCode = ISD::SETNE;
844   }
845
846   // Update N to have the operands specified.
847   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
848                                 N->getOperand(2), N->getOperand(3),
849                                 DAG.getCondCode(CCCode));
850 }
851
852 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
853   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
854   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
855   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
856
857   // If ExpandSetCCOperands returned a scalar, use it.
858   if (NewRHS.Val == 0) {
859     assert(NewLHS.getValueType() == N->getValueType(0) &&
860            "Unexpected setcc expansion!");
861     return NewLHS;
862   }
863
864   // Otherwise, update N to have the operands specified.
865   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
866                                 DAG.getCondCode(CCCode));
867 }
868
869 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
870   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
871          "Unsupported FP_TO_UINT!");
872
873   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
874   switch (N->getValueType(0).getSimpleVT()) {
875   default:
876     assert(false && "Unsupported FP_TO_UINT!");
877   case MVT::i32:
878     LC = RTLIB::FPTOUINT_PPCF128_I32;
879     break;
880   case MVT::i64:
881     LC = RTLIB::FPTOUINT_PPCF128_I64;
882     break;
883   case MVT::i128:
884     LC = RTLIB::FPTOUINT_PPCF128_I128;
885     break;
886   }
887
888   return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
889 }
890
891 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
892   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
893          "Unsupported FP_TO_SINT!");
894
895   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
896   switch (N->getValueType(0).getSimpleVT()) {
897   default:
898     assert(false && "Unsupported FP_TO_SINT!");
899   case MVT::i32:
900     LC = RTLIB::FPTOSINT_PPCF128_I32;
901   case MVT::i64:
902     LC = RTLIB::FPTOSINT_PPCF128_I64;
903     break;
904   case MVT::i128:
905     LC = RTLIB::FPTOSINT_PPCF128_I64;
906     break;
907   }
908
909   return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
910 }
911
912 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
913   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
914          "Logic only correct for ppcf128!");
915   SDOperand Lo, Hi;
916   GetExpandedFloat(N->getOperand(0), Lo, Hi);
917   // Round it the rest of the way (e.g. to f32) if needed.
918   return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
919 }
920
921 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
922   if (ISD::isNormalStore(N))
923     return ExpandOp_NormalStore(N, OpNo);
924
925   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
926   assert(OpNo == 1 && "Can only expand the stored value so far");
927   StoreSDNode *ST = cast<StoreSDNode>(N);
928
929   SDOperand Chain = ST->getChain();
930   SDOperand Ptr = ST->getBasePtr();
931
932   MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
933   assert(NVT.isByteSized() && "Expanded type not byte sized!");
934   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
935
936   SDOperand Lo, Hi;
937   GetExpandedOp(ST->getValue(), Lo, Hi);
938
939   return DAG.getTruncStore(Chain, Lo, Ptr,
940                            ST->getSrcValue(), ST->getSrcValueOffset(),
941                            ST->getMemoryVT(),
942                            ST->isVolatile(), ST->getAlignment());
943 }