Add/complete support for integer and float
[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   // FIXME: Custom lowering for float-to-int?
52 #if 0
53   // See if the target wants to custom convert this node to an integer.
54   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) ==
55       TargetLowering::Custom) {
56     // If the target wants to, allow it to lower this itself.
57     if (SDNode *P = TLI.FloatToIntOperationResult(N, DAG)) {
58       // Everything that once used N now uses P.  We are guaranteed that the
59       // result value types of N and the result value types of P match.
60       ReplaceNodeWith(N, P);
61       return;
62     }
63   }
64 #endif
65
66   switch (N->getOpcode()) {
67   default:
68 #ifndef NDEBUG
69     cerr << "SoftenFloatResult #" << ResNo << ": ";
70     N->dump(&DAG); cerr << "\n";
71 #endif
72     assert(0 && "Do not know how to convert the result of this operator!");
73     abort();
74
75     case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
76     case ISD::BUILD_PAIR:  R = SoftenFloatRes_BUILD_PAIR(N); break;
77     case ISD::ConstantFP:
78       R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
79       break;
80     case ISD::FCOPYSIGN:   R = SoftenFloatRes_FCOPYSIGN(N); break;
81     case ISD::LOAD:        R = SoftenFloatRes_LOAD(N); break;
82     case ISD::SINT_TO_FP:
83     case ISD::UINT_TO_FP:  R = SoftenFloatRes_XINT_TO_FP(N); break;
84
85     case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
86     case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
87     case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
88   }
89
90   // If R is null, the sub-method took care of registering the result.
91   if (R.Val)
92     SetSoftenedFloat(SDOperand(N, ResNo), R);
93 }
94
95 SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
96   return BitConvertToInteger(N->getOperand(0));
97 }
98
99 SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
100   // Convert the inputs to integers, and build a new pair out of them.
101   return DAG.getNode(ISD::BUILD_PAIR,
102                      TLI.getTypeToTransformTo(N->getValueType(0)),
103                      BitConvertToInteger(N->getOperand(0)),
104                      BitConvertToInteger(N->getOperand(1)));
105 }
106
107 SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
108   return DAG.getConstant(N->getValueAPF().convertToAPInt(),
109                          TLI.getTypeToTransformTo(N->getValueType(0)));
110 }
111
112 SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
113   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
114   SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
115                        GetSoftenedFloat(N->getOperand(1)) };
116   return MakeLibCall(GetFPLibCall(N->getValueType(0),
117                                   RTLIB::ADD_F32,
118                                   RTLIB::ADD_F64,
119                                   RTLIB::ADD_F80,
120                                   RTLIB::ADD_PPCF128),
121                      NVT, Ops, 2, false/*sign irrelevant*/);
122 }
123
124 SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
125   SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
126   SDOperand RHS = BitConvertToInteger(N->getOperand(1));
127
128   MVT LVT = LHS.getValueType();
129   MVT RVT = RHS.getValueType();
130
131   unsigned LSize = LVT.getSizeInBits();
132   unsigned RSize = RVT.getSizeInBits();
133
134   // First get the sign bit of second operand.
135   SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
136                                   DAG.getConstant(RSize - 1,
137                                                   TLI.getShiftAmountTy()));
138   SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
139
140   // Shift right or sign-extend it if the two operands have different types.
141   int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
142   if (SizeDiff > 0) {
143     SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
144                           DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
145     SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
146   } else if (SizeDiff < 0) {
147     SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
148     SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
149                           DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
150   }
151
152   // Clear the sign bit of the first operand.
153   SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
154                                DAG.getConstant(LSize - 1,
155                                                TLI.getShiftAmountTy()));
156   Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
157   LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
158
159   // Or the value with the sign bit.
160   return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
161 }
162
163 SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
164   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
165   SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
166                        GetSoftenedFloat(N->getOperand(1)) };
167   return MakeLibCall(GetFPLibCall(N->getValueType(0),
168                                   RTLIB::MUL_F32,
169                                   RTLIB::MUL_F64,
170                                   RTLIB::MUL_F80,
171                                   RTLIB::MUL_PPCF128),
172                      NVT, Ops, 2, false/*sign irrelevant*/);
173 }
174
175 SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
176   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
177   SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
178                        GetSoftenedFloat(N->getOperand(1)) };
179   return MakeLibCall(GetFPLibCall(N->getValueType(0),
180                                   RTLIB::SUB_F32,
181                                   RTLIB::SUB_F64,
182                                   RTLIB::SUB_F80,
183                                   RTLIB::SUB_PPCF128),
184                      NVT, Ops, 2, false/*sign irrelevant*/);
185 }
186
187 SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
188   LoadSDNode *L = cast<LoadSDNode>(N);
189   MVT VT = N->getValueType(0);
190   MVT NVT = TLI.getTypeToTransformTo(VT);
191
192   if (L->getExtensionType() == ISD::NON_EXTLOAD)
193      return DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
194                         NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
195                         L->getSrcValue(), L->getSrcValueOffset(), NVT,
196                         L->isVolatile(), L->getAlignment());
197
198   // Do a non-extending load followed by FP_EXTEND.
199   SDOperand NL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
200                              L->getMemoryVT(), L->getChain(),
201                              L->getBasePtr(), L->getOffset(),
202                              L->getSrcValue(), L->getSrcValueOffset(),
203                              L->getMemoryVT(),
204                              L->isVolatile(), L->getAlignment());
205   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NL));
206 }
207
208 SDOperand DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
209   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
210   MVT DestVT = N->getValueType(0);
211   SDOperand Op = N->getOperand(0);
212
213   if (Op.getValueType() == MVT::i32) {
214     // simple 32-bit [signed|unsigned] integer to float/double expansion
215
216     // Get the stack frame index of a 8 byte buffer.
217     SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
218
219     // word offset constant for Hi/Lo address computation
220     SDOperand Offset =
221       DAG.getConstant(MVT(MVT::i32).getSizeInBits() / 8,
222                       TLI.getPointerTy());
223     // set up Hi and Lo (into buffer) address based on endian
224     SDOperand Hi = StackSlot;
225     SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, Offset);
226     if (TLI.isLittleEndian())
227       std::swap(Hi, Lo);
228
229     // if signed map to unsigned space
230     SDOperand OpMapped;
231     if (isSigned) {
232       // constant used to invert sign bit (signed to unsigned mapping)
233       SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
234       OpMapped = DAG.getNode(ISD::XOR, MVT::i32, Op, SignBit);
235     } else {
236       OpMapped = Op;
237     }
238     // store the lo of the constructed double - based on integer input
239     SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
240                                     OpMapped, Lo, NULL, 0);
241     // initial hi portion of constructed double
242     SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
243     // store the hi of the constructed double - biased exponent
244     SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
245     // load the constructed double
246     SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
247     // FP constant to bias correct the final result
248     SDOperand Bias = DAG.getConstantFP(isSigned ?
249                                             BitsToDouble(0x4330000080000000ULL)
250                                           : BitsToDouble(0x4330000000000000ULL),
251                                      MVT::f64);
252     // subtract the bias
253     SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
254     // final result
255     SDOperand Result;
256     // handle final rounding
257     if (DestVT == MVT::f64) {
258       // do nothing
259       Result = Sub;
260     } else if (DestVT.bitsLT(MVT::f64)) {
261       Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
262                            DAG.getIntPtrConstant(0));
263     } else if (DestVT.bitsGT(MVT::f64)) {
264       Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
265     }
266     return BitConvertToInteger(Result);
267   }
268   assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
269   SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op);
270
271   SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op), Op,
272                                    DAG.getConstant(0, Op.getValueType()),
273                                    ISD::SETLT);
274   SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
275   SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
276                                     SignSet, Four, Zero);
277
278   // If the sign bit of the integer is set, the large number will be treated
279   // as a negative number.  To counteract this, the dynamic code adds an
280   // offset depending on the data type.
281   uint64_t FF;
282   switch (Op.getValueType().getSimpleVT()) {
283   default: assert(0 && "Unsupported integer type!");
284   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
285   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
286   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
287   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
288   }
289   if (TLI.isLittleEndian()) FF <<= 32;
290   static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
291
292   SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
293   CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
294   SDOperand FudgeInReg;
295   if (DestVT == MVT::f32)
296     FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
297                              PseudoSourceValue::getConstantPool(), 0);
298   else {
299     FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestVT,
300                                 DAG.getEntryNode(), CPIdx,
301                                 PseudoSourceValue::getConstantPool(), 0,
302                                 MVT::f32);
303   }
304
305   return BitConvertToInteger(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
306 }
307
308
309 //===----------------------------------------------------------------------===//
310 //  Operand Float to Integer Conversion..
311 //===----------------------------------------------------------------------===//
312
313 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
314   DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
315         cerr << "\n");
316   SDOperand Res(0, 0);
317
318   // FIXME: Custom lowering for float-to-int?
319 #if 0
320   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
321       == TargetLowering::Custom)
322     Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
323 #endif
324
325   if (Res.Val == 0) {
326     switch (N->getOpcode()) {
327     default:
328 #ifndef NDEBUG
329       cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
330       N->dump(&DAG); cerr << "\n";
331 #endif
332       assert(0 && "Do not know how to convert this operator's operand!");
333       abort();
334
335     case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
336
337     case ISD::BR_CC:     Res = SoftenFloatOp_BR_CC(N); break;
338     case ISD::SELECT_CC: Res = SoftenFloatOp_BR_CC(N); break;
339     case ISD::SETCC:     Res = SoftenFloatOp_BR_CC(N); break;
340     }
341   }
342
343   // If the result is null, the sub-method took care of registering results etc.
344   if (!Res.Val) return false;
345
346   // If the result is N, the sub-method updated N in place.  Check to see if any
347   // operands are new, and if so, mark them.
348   if (Res.Val == N) {
349     // Mark N as new and remark N and its operands.  This allows us to correctly
350     // revisit N if it needs another step of promotion and allows us to visit
351     // any new operands to N.
352     ReanalyzeNode(N);
353     return true;
354   }
355
356   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
357          "Invalid operand expansion");
358
359   ReplaceValueWith(SDOperand(N, 0), Res);
360   return false;
361 }
362
363 /// SoftenSetCCOperands - Soften the operands of a comparison.  This code is
364 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
365 void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
366                                            ISD::CondCode &CCCode) {
367   SDOperand LHSInt = GetSoftenedFloat(NewLHS);
368   SDOperand RHSInt = GetSoftenedFloat(NewRHS);
369   MVT VT  = NewLHS.getValueType();
370   MVT NVT = LHSInt.getValueType();
371
372   assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
373
374   // Expand into one or more soft-fp libcall(s).
375   RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
376   switch (CCCode) {
377   case ISD::SETEQ:
378   case ISD::SETOEQ:
379     LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
380     break;
381   case ISD::SETNE:
382   case ISD::SETUNE:
383     LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
384     break;
385   case ISD::SETGE:
386   case ISD::SETOGE:
387     LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
388     break;
389   case ISD::SETLT:
390   case ISD::SETOLT:
391     LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
392     break;
393   case ISD::SETLE:
394   case ISD::SETOLE:
395     LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
396     break;
397   case ISD::SETGT:
398   case ISD::SETOGT:
399     LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
400     break;
401   case ISD::SETUO:
402     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
403     break;
404   case ISD::SETO:
405     break;
406   default:
407     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
408     switch (CCCode) {
409     case ISD::SETONE:
410       // SETONE = SETOLT | SETOGT
411       LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
412       // Fallthrough
413     case ISD::SETUGT:
414       LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
415       break;
416     case ISD::SETUGE:
417       LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
418       break;
419     case ISD::SETULT:
420       LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
421       break;
422     case ISD::SETULE:
423       LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
424       break;
425     case ISD::SETUEQ:
426       LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
427       break;
428     default: assert(false && "Do not know how to soften this setcc!");
429     }
430   }
431
432   SDOperand Ops[2] = { LHSInt, RHSInt };
433   NewLHS = MakeLibCall(LC1, NVT, Ops, 2, false/*sign irrelevant*/);
434   NewRHS = DAG.getConstant(0, NVT);
435   if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
436     SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
437                                 NewLHS, NewRHS,
438                                 DAG.getCondCode(TLI.getCmpLibcallCC(LC1)));
439     NewLHS = MakeLibCall(LC2, NVT, Ops, 2, false/*sign irrelevant*/);
440     NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
441                          NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
442     NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
443     NewRHS = SDOperand();
444   }
445 }
446
447 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
448   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
449                      GetSoftenedFloat(N->getOperand(0)));
450 }
451
452 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
453   SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
454   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
455   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
456
457   // If SoftenSetCCOperands returned a scalar, we need to compare the result
458   // against zero to select between true and false values.
459   if (NewRHS.Val == 0) {
460     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
461     CCCode = ISD::SETNE;
462   }
463
464   // Update N to have the operands specified.
465   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
466                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
467                                 N->getOperand(4));
468 }
469
470 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
471   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
472   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
473   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
474
475   // If SoftenSetCCOperands returned a scalar, we need to compare the result
476   // against zero to select between true and false values.
477   if (NewRHS.Val == 0) {
478     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
479     CCCode = ISD::SETNE;
480   }
481
482   // Update N to have the operands specified.
483   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
484                                 N->getOperand(2), N->getOperand(3),
485                                 DAG.getCondCode(CCCode));
486 }
487
488 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
489   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
490   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
491   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
492
493   // If SoftenSetCCOperands returned a scalar, use it.
494   if (NewRHS.Val == 0) {
495     assert(NewLHS.getValueType() == N->getValueType(0) &&
496            "Unexpected setcc expansion!");
497     return NewLHS;
498   }
499
500   // Otherwise, update N to have the operands specified.
501   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
502                                 DAG.getCondCode(CCCode));
503 }
504
505
506 //===----------------------------------------------------------------------===//
507 //  Float Result Expansion
508 //===----------------------------------------------------------------------===//
509
510 /// ExpandFloatResult - This method is called when the specified result of the
511 /// specified node is found to need expansion.  At this point, the node may also
512 /// have invalid operands or may have other results that need promotion, we just
513 /// know that (at least) one result needs expansion.
514 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
515   DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
516   SDOperand Lo, Hi;
517   Lo = Hi = SDOperand();
518
519   // See if the target wants to custom expand this node.
520   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) ==
521           TargetLowering::Custom) {
522     // If the target wants to, allow it to lower this itself.
523     if (SDNode *P = TLI.ExpandOperationResult(N, DAG)) {
524       // Everything that once used N now uses P.  We are guaranteed that the
525       // result value types of N and the result value types of P match.
526       ReplaceNodeWith(N, P);
527       return;
528     }
529   }
530
531   switch (N->getOpcode()) {
532   default:
533 #ifndef NDEBUG
534     cerr << "ExpandFloatResult #" << ResNo << ": ";
535     N->dump(&DAG); cerr << "\n";
536 #endif
537     assert(0 && "Do not know how to expand the result of this operator!");
538     abort();
539
540   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
541   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
542   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
543   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
544
545   case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
546   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
547   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
548   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
549
550   case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
551   }
552
553   // If Lo/Hi is null, the sub-method took care of registering results etc.
554   if (Lo.Val)
555     SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
556 }
557
558 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
559                                            SDOperand &Hi) {
560   if (ISD::isNormalLoad(N)) {
561     ExpandRes_NormalLoad(N, Lo, Hi);
562     return;
563   }
564
565   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
566   LoadSDNode *LD = cast<LoadSDNode>(N);
567   SDOperand Chain = LD->getChain();
568   SDOperand Ptr = LD->getBasePtr();
569
570   MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
571   assert(NVT.isByteSized() && "Expanded type not byte sized!");
572   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
573
574   Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
575                       LD->getSrcValue(), LD->getSrcValueOffset(),
576                       LD->getMemoryVT(),
577                       LD->isVolatile(), LD->getAlignment());
578
579   // Remember the chain.
580   Chain = Lo.getValue(1);
581
582   // The high part is undefined.
583   Hi = DAG.getNode(ISD::UNDEF, NVT);
584
585   // Modified the chain - switch anything that used the old chain to use the
586   // new one.
587   ReplaceValueWith(SDOperand(LD, 1), Chain);
588 }
589
590
591 //===----------------------------------------------------------------------===//
592 //  Float Operand Expansion
593 //===----------------------------------------------------------------------===//
594
595 /// ExpandFloatOperand - This method is called when the specified operand of the
596 /// specified node is found to need expansion.  At this point, all of the result
597 /// types of the node are known to be legal, but other operands of the node may
598 /// need promotion or expansion as well as the specified one.
599 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
600   DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
601   SDOperand Res(0, 0);
602
603   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
604       == TargetLowering::Custom)
605     Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
606
607   if (Res.Val == 0) {
608     switch (N->getOpcode()) {
609     default:
610   #ifndef NDEBUG
611       cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
612       N->dump(&DAG); cerr << "\n";
613   #endif
614       assert(0 && "Do not know how to expand this operator's operand!");
615       abort();
616
617     case ISD::BIT_CONVERT:     Res = ExpandOp_BIT_CONVERT(N); break;
618     case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
619     case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
620
621     case ISD::BR_CC:     Res = ExpandFloatOp_BR_CC(N); break;
622     case ISD::SELECT_CC: Res = ExpandFloatOp_BR_CC(N); break;
623     case ISD::SETCC:     Res = ExpandFloatOp_BR_CC(N); break;
624
625     case ISD::STORE:
626       Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
627       break;
628     }
629   }
630
631   // If the result is null, the sub-method took care of registering results etc.
632   if (!Res.Val) return false;
633   // If the result is N, the sub-method updated N in place.  Check to see if any
634   // operands are new, and if so, mark them.
635   if (Res.Val == N) {
636     // Mark N as new and remark N and its operands.  This allows us to correctly
637     // revisit N if it needs another step of expansion and allows us to visit
638     // any new operands to N.
639     ReanalyzeNode(N);
640     return true;
641   }
642
643   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
644          "Invalid operand expansion");
645
646   ReplaceValueWith(SDOperand(N, 0), Res);
647   return false;
648 }
649
650 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
651 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
652 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
653                                                 SDOperand &NewRHS,
654                                                 ISD::CondCode &CCCode) {
655   SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
656   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
657   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
658
659   MVT VT = NewLHS.getValueType();
660   assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
661
662   // FIXME:  This generated code sucks.  We want to generate
663   //         FCMP crN, hi1, hi2
664   //         BNE crN, L:
665   //         FCMP crN, lo1, lo2
666   // The following can be improved, but not that much.
667   SDOperand Tmp1, Tmp2, Tmp3;
668   Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
669   Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
670   Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
671   Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
672   Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
673   Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
674   NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
675   NewRHS = SDOperand();   // LHS is the result, not a compare.
676 }
677
678 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
679   SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
680   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
681   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
682
683   // If ExpandSetCCOperands returned a scalar, we need to compare the result
684   // against zero to select between true and false values.
685   if (NewRHS.Val == 0) {
686     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
687     CCCode = ISD::SETNE;
688   }
689
690   // Update N to have the operands specified.
691   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
692                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
693                                 N->getOperand(4));
694 }
695
696 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
697   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
698   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
699   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
700
701   // If ExpandSetCCOperands returned a scalar, we need to compare the result
702   // against zero to select between true and false values.
703   if (NewRHS.Val == 0) {
704     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
705     CCCode = ISD::SETNE;
706   }
707
708   // Update N to have the operands specified.
709   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
710                                 N->getOperand(2), N->getOperand(3),
711                                 DAG.getCondCode(CCCode));
712 }
713
714 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
715   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
716   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
717   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
718
719   // If ExpandSetCCOperands returned a scalar, use it.
720   if (NewRHS.Val == 0) {
721     assert(NewLHS.getValueType() == N->getValueType(0) &&
722            "Unexpected setcc expansion!");
723     return NewLHS;
724   }
725
726   // Otherwise, update N to have the operands specified.
727   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
728                                 DAG.getCondCode(CCCode));
729 }
730
731 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
732   if (ISD::isNormalStore(N))
733     return ExpandOp_NormalStore(N, OpNo);
734
735   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
736   assert(OpNo == 1 && "Can only expand the stored value so far");
737   StoreSDNode *ST = cast<StoreSDNode>(N);
738
739   SDOperand Chain = ST->getChain();
740   SDOperand Ptr = ST->getBasePtr();
741
742   MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
743   assert(NVT.isByteSized() && "Expanded type not byte sized!");
744   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
745
746   SDOperand Lo, Hi;
747   GetExpandedOp(ST->getValue(), Lo, Hi);
748
749   return DAG.getTruncStore(Chain, Lo, Ptr,
750                            ST->getSrcValue(), ST->getSrcValueOffset(),
751                            ST->getMemoryVT(),
752                            ST->isVolatile(), ST->getAlignment());
753 }