Mark FP_ROUND for converting NEON v2f64 to v2f32 as expand. Add a missing
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeVectorOps.cpp
1 //===-- LegalizeVectorOps.cpp - Implement SelectionDAG::LegalizeVectors ---===//
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 the SelectionDAG::LegalizeVectors method.
11 //
12 // The vector legalizer looks for vector operations which might need to be
13 // scalarized and legalizes them. This is a separate step from Legalize because
14 // scalarizing can introduce illegal types.  For example, suppose we have an
15 // ISD::SDIV of type v2i64 on x86-32.  The type is legal (for example, addition
16 // on a v2i64 is legal), but ISD::SDIV isn't legal, so we have to unroll the
17 // operation, which introduces nodes with the illegal type i64 which must be
18 // expanded.  Similarly, suppose we have an ISD::SRA of type v16i8 on PowerPC;
19 // the operation must be unrolled, which introduces nodes with the illegal
20 // type i8 which must be promoted.
21 //
22 // This does not legalize vector manipulations like ISD::BUILD_VECTOR,
23 // or operations that happen to take a vector which are custom-lowered;
24 // the legalization for such operations never produces nodes
25 // with illegal types, so it's okay to put off legalizing them until
26 // SelectionDAG::Legalize runs.
27 //
28 //===----------------------------------------------------------------------===//
29
30 #include "llvm/CodeGen/SelectionDAG.h"
31 #include "llvm/Target/TargetLowering.h"
32 using namespace llvm;
33
34 namespace {
35 class VectorLegalizer {
36   SelectionDAG& DAG;
37   const TargetLowering &TLI;
38   bool Changed; // Keep track of whether anything changed
39
40   /// LegalizedNodes - For nodes that are of legal width, and that have more
41   /// than one use, this map indicates what regularized operand to use.  This
42   /// allows us to avoid legalizing the same thing more than once.
43   DenseMap<SDValue, SDValue> LegalizedNodes;
44
45   // Adds a node to the translation cache
46   void AddLegalizedOperand(SDValue From, SDValue To) {
47     LegalizedNodes.insert(std::make_pair(From, To));
48     // If someone requests legalization of the new node, return itself.
49     if (From != To)
50       LegalizedNodes.insert(std::make_pair(To, To));
51   }
52
53   // Legalizes the given node
54   SDValue LegalizeOp(SDValue Op);
55   // Assuming the node is legal, "legalize" the results
56   SDValue TranslateLegalizeResults(SDValue Op, SDValue Result);
57   // Implements unrolling a VSETCC.
58   SDValue UnrollVSETCC(SDValue Op);
59   // Implements expansion for FNEG; falls back to UnrollVectorOp if FSUB
60   // isn't legal.
61   // Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
62   // SINT_TO_FLOAT and SHR on vectors isn't legal.
63   SDValue ExpandUINT_TO_FLOAT(SDValue Op);
64   // Implement vselect in terms of XOR, AND, OR when blend is not supported
65   // by the target.
66   SDValue ExpandVSELECT(SDValue Op);
67   SDValue ExpandSELECT(SDValue Op);
68   SDValue ExpandLoad(SDValue Op);
69   SDValue ExpandStore(SDValue Op);
70   SDValue ExpandFNEG(SDValue Op);
71   // Implements vector promotion; this is essentially just bitcasting the
72   // operands to a different type and bitcasting the result back to the
73   // original type.
74   SDValue PromoteVectorOp(SDValue Op);
75   // Implements [SU]INT_TO_FP vector promotion; this is a [zs]ext of the input
76   // operand to the next size up.
77   SDValue PromoteVectorOpINT_TO_FP(SDValue Op);
78
79   public:
80   bool Run();
81   VectorLegalizer(SelectionDAG& dag) :
82       DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
83 };
84
85 bool VectorLegalizer::Run() {
86   // The legalize process is inherently a bottom-up recursive process (users
87   // legalize their uses before themselves).  Given infinite stack space, we
88   // could just start legalizing on the root and traverse the whole graph.  In
89   // practice however, this causes us to run out of stack space on large basic
90   // blocks.  To avoid this problem, compute an ordering of the nodes where each
91   // node is only legalized after all of its operands are legalized.
92   DAG.AssignTopologicalOrder();
93   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
94        E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
95     LegalizeOp(SDValue(I, 0));
96
97   // Finally, it's possible the root changed.  Get the new root.
98   SDValue OldRoot = DAG.getRoot();
99   assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
100   DAG.setRoot(LegalizedNodes[OldRoot]);
101
102   LegalizedNodes.clear();
103
104   // Remove dead nodes now.
105   DAG.RemoveDeadNodes();
106
107   return Changed;
108 }
109
110 SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
111   // Generic legalization: just pass the operand through.
112   for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
113     AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
114   return Result.getValue(Op.getResNo());
115 }
116
117 SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
118   // Note that LegalizeOp may be reentered even from single-use nodes, which
119   // means that we always must cache transformed nodes.
120   DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
121   if (I != LegalizedNodes.end()) return I->second;
122
123   SDNode* Node = Op.getNode();
124
125   // Legalize the operands
126   SmallVector<SDValue, 8> Ops;
127   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
128     Ops.push_back(LegalizeOp(Node->getOperand(i)));
129
130   SDValue Result =
131     SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops.data(), Ops.size()), 0);
132
133   if (Op.getOpcode() == ISD::LOAD) {
134     LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
135     ISD::LoadExtType ExtType = LD->getExtensionType();
136     if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) {
137       if (TLI.isLoadExtLegal(LD->getExtensionType(), LD->getMemoryVT()))
138         return TranslateLegalizeResults(Op, Result);
139       Changed = true;
140       return LegalizeOp(ExpandLoad(Op));
141     }
142   } else if (Op.getOpcode() == ISD::STORE) {
143     StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
144     EVT StVT = ST->getMemoryVT();
145     EVT ValVT = ST->getValue().getValueType();
146     if (StVT.isVector() && ST->isTruncatingStore())
147       switch (TLI.getTruncStoreAction(ValVT, StVT)) {
148       default: llvm_unreachable("This action is not supported yet!");
149       case TargetLowering::Legal:
150         return TranslateLegalizeResults(Op, Result);
151       case TargetLowering::Custom:
152         Changed = true;
153         return LegalizeOp(TLI.LowerOperation(Result, DAG));
154       case TargetLowering::Expand:
155         Changed = true;
156         return LegalizeOp(ExpandStore(Op));
157       }
158   }
159
160   bool HasVectorValue = false;
161   for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
162        J != E;
163        ++J)
164     HasVectorValue |= J->isVector();
165   if (!HasVectorValue)
166     return TranslateLegalizeResults(Op, Result);
167
168   EVT QueryType;
169   switch (Op.getOpcode()) {
170   default:
171     return TranslateLegalizeResults(Op, Result);
172   case ISD::ADD:
173   case ISD::SUB:
174   case ISD::MUL:
175   case ISD::SDIV:
176   case ISD::UDIV:
177   case ISD::SREM:
178   case ISD::UREM:
179   case ISD::FADD:
180   case ISD::FSUB:
181   case ISD::FMUL:
182   case ISD::FDIV:
183   case ISD::FREM:
184   case ISD::AND:
185   case ISD::OR:
186   case ISD::XOR:
187   case ISD::SHL:
188   case ISD::SRA:
189   case ISD::SRL:
190   case ISD::ROTL:
191   case ISD::ROTR:
192   case ISD::CTLZ:
193   case ISD::CTTZ:
194   case ISD::CTLZ_ZERO_UNDEF:
195   case ISD::CTTZ_ZERO_UNDEF:
196   case ISD::CTPOP:
197   case ISD::SELECT:
198   case ISD::VSELECT:
199   case ISD::SELECT_CC:
200   case ISD::SETCC:
201   case ISD::ZERO_EXTEND:
202   case ISD::ANY_EXTEND:
203   case ISD::TRUNCATE:
204   case ISD::SIGN_EXTEND:
205   case ISD::FP_TO_SINT:
206   case ISD::FP_TO_UINT:
207   case ISD::FNEG:
208   case ISD::FABS:
209   case ISD::FSQRT:
210   case ISD::FSIN:
211   case ISD::FCOS:
212   case ISD::FPOWI:
213   case ISD::FPOW:
214   case ISD::FLOG:
215   case ISD::FLOG2:
216   case ISD::FLOG10:
217   case ISD::FEXP:
218   case ISD::FEXP2:
219   case ISD::FCEIL:
220   case ISD::FTRUNC:
221   case ISD::FRINT:
222   case ISD::FNEARBYINT:
223   case ISD::FFLOOR:
224   case ISD::FP_ROUND:
225   case ISD::FMA:
226   case ISD::SIGN_EXTEND_INREG:
227     QueryType = Node->getValueType(0);
228     break;
229   case ISD::FP_ROUND_INREG:
230     QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
231     break;
232   case ISD::SINT_TO_FP:
233   case ISD::UINT_TO_FP:
234     QueryType = Node->getOperand(0).getValueType();
235     break;
236   }
237
238   switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
239   case TargetLowering::Promote:
240     switch (Op.getOpcode()) {
241     default:
242       // "Promote" the operation by bitcasting
243       Result = PromoteVectorOp(Op);
244       Changed = true;
245       break;
246     case ISD::SINT_TO_FP:
247     case ISD::UINT_TO_FP:
248       // "Promote" the operation by extending the operand.
249       Result = PromoteVectorOpINT_TO_FP(Op);
250       Changed = true;
251       break;
252     }
253     break;
254   case TargetLowering::Legal: break;
255   case TargetLowering::Custom: {
256     SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
257     if (Tmp1.getNode()) {
258       Result = Tmp1;
259       break;
260     }
261     // FALL THROUGH
262   }
263   case TargetLowering::Expand:
264     if (Node->getOpcode() == ISD::VSELECT)
265       Result = ExpandVSELECT(Op);
266     else if (Node->getOpcode() == ISD::SELECT)
267       Result = ExpandSELECT(Op);
268     else if (Node->getOpcode() == ISD::UINT_TO_FP)
269       Result = ExpandUINT_TO_FLOAT(Op);
270     else if (Node->getOpcode() == ISD::FNEG)
271       Result = ExpandFNEG(Op);
272     else if (Node->getOpcode() == ISD::SETCC)
273       Result = UnrollVSETCC(Op);
274     else
275       Result = DAG.UnrollVectorOp(Op.getNode());
276     break;
277   }
278
279   // Make sure that the generated code is itself legal.
280   if (Result != Op) {
281     Result = LegalizeOp(Result);
282     Changed = true;
283   }
284
285   // Note that LegalizeOp may be reentered even from single-use nodes, which
286   // means that we always must cache transformed nodes.
287   AddLegalizedOperand(Op, Result);
288   return Result;
289 }
290
291 SDValue VectorLegalizer::PromoteVectorOp(SDValue Op) {
292   // Vector "promotion" is basically just bitcasting and doing the operation
293   // in a different type.  For example, x86 promotes ISD::AND on v2i32 to
294   // v1i64.
295   EVT VT = Op.getValueType();
296   assert(Op.getNode()->getNumValues() == 1 &&
297          "Can't promote a vector with multiple results!");
298   EVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
299   DebugLoc dl = Op.getDebugLoc();
300   SmallVector<SDValue, 4> Operands(Op.getNumOperands());
301
302   for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
303     if (Op.getOperand(j).getValueType().isVector())
304       Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
305     else
306       Operands[j] = Op.getOperand(j);
307   }
308
309   Op = DAG.getNode(Op.getOpcode(), dl, NVT, &Operands[0], Operands.size());
310
311   return DAG.getNode(ISD::BITCAST, dl, VT, Op);
312 }
313
314 SDValue VectorLegalizer::PromoteVectorOpINT_TO_FP(SDValue Op) {
315   // INT_TO_FP operations may require the input operand be promoted even
316   // when the type is otherwise legal.
317   EVT VT = Op.getOperand(0).getValueType();
318   assert(Op.getNode()->getNumValues() == 1 &&
319          "Can't promote a vector with multiple results!");
320
321   // Normal getTypeToPromoteTo() doesn't work here, as that will promote
322   // by widening the vector w/ the same element width and twice the number
323   // of elements. We want the other way around, the same number of elements,
324   // each twice the width.
325   //
326   // Increase the bitwidth of the element to the next pow-of-two
327   // (which is greater than 8 bits).
328   unsigned NumElts = VT.getVectorNumElements();
329   EVT EltVT = VT.getVectorElementType();
330   EltVT = EVT::getIntegerVT(*DAG.getContext(), 2 * EltVT.getSizeInBits());
331   assert(EltVT.isSimple() && "Promoting to a non-simple vector type!");
332
333   // Build a new vector type and check if it is legal.
334   MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
335
336   DebugLoc dl = Op.getDebugLoc();
337   SmallVector<SDValue, 4> Operands(Op.getNumOperands());
338
339   unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
340     ISD::SIGN_EXTEND;
341   for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
342     if (Op.getOperand(j).getValueType().isVector())
343       Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
344     else
345       Operands[j] = Op.getOperand(j);
346   }
347
348   return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), &Operands[0],
349                      Operands.size());
350 }
351
352
353 SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
354   DebugLoc dl = Op.getDebugLoc();
355   LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
356   SDValue Chain = LD->getChain();
357   SDValue BasePTR = LD->getBasePtr();
358   EVT SrcVT = LD->getMemoryVT();
359   ISD::LoadExtType ExtType = LD->getExtensionType();
360
361   SmallVector<SDValue, 8> LoadVals;
362   SmallVector<SDValue, 8> LoadChains;
363   unsigned NumElem = SrcVT.getVectorNumElements();
364   unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
365
366   for (unsigned Idx=0; Idx<NumElem; Idx++) {
367     SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl,
368               Op.getNode()->getValueType(0).getScalarType(),
369               Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride),
370               SrcVT.getScalarType(),
371               LD->isVolatile(), LD->isNonTemporal(),
372               LD->getAlignment());
373
374     BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
375                        DAG.getIntPtrConstant(Stride));
376
377      LoadVals.push_back(ScalarLoad.getValue(0));
378      LoadChains.push_back(ScalarLoad.getValue(1));
379   }
380
381   SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
382             &LoadChains[0], LoadChains.size());
383   SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
384             Op.getNode()->getValueType(0), &LoadVals[0], LoadVals.size());
385
386   AddLegalizedOperand(Op.getValue(0), Value);
387   AddLegalizedOperand(Op.getValue(1), NewChain);
388
389   return (Op.getResNo() ? NewChain : Value);
390 }
391
392 SDValue VectorLegalizer::ExpandStore(SDValue Op) {
393   DebugLoc dl = Op.getDebugLoc();
394   StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
395   SDValue Chain = ST->getChain();
396   SDValue BasePTR = ST->getBasePtr();
397   SDValue Value = ST->getValue();
398   EVT StVT = ST->getMemoryVT();
399
400   unsigned Alignment = ST->getAlignment();
401   bool isVolatile = ST->isVolatile();
402   bool isNonTemporal = ST->isNonTemporal();
403
404   unsigned NumElem = StVT.getVectorNumElements();
405   // The type of the data we want to save
406   EVT RegVT = Value.getValueType();
407   EVT RegSclVT = RegVT.getScalarType();
408   // The type of data as saved in memory.
409   EVT MemSclVT = StVT.getScalarType();
410
411   // Cast floats into integers
412   unsigned ScalarSize = MemSclVT.getSizeInBits();
413
414   // Round odd types to the next pow of two.
415   if (!isPowerOf2_32(ScalarSize))
416     ScalarSize = NextPowerOf2(ScalarSize);
417
418   // Store Stride in bytes
419   unsigned Stride = ScalarSize/8;
420   // Extract each of the elements from the original vector
421   // and save them into memory individually.
422   SmallVector<SDValue, 8> Stores;
423   for (unsigned Idx = 0; Idx < NumElem; Idx++) {
424     SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
425                RegSclVT, Value, DAG.getIntPtrConstant(Idx));
426
427     // This scalar TruncStore may be illegal, but we legalize it later.
428     SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
429                ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
430                isVolatile, isNonTemporal, Alignment);
431
432     BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
433                                 DAG.getIntPtrConstant(Stride));
434
435     Stores.push_back(Store);
436   }
437   SDValue TF =  DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
438                             &Stores[0], Stores.size());
439   AddLegalizedOperand(Op, TF);
440   return TF;
441 }
442
443 SDValue VectorLegalizer::ExpandSELECT(SDValue Op) {
444   // Lower a select instruction where the condition is a scalar and the
445   // operands are vectors. Lower this select to VSELECT and implement it
446   // using XOR AND OR. The selector bit is broadcasted. 
447   EVT VT = Op.getValueType();
448   DebugLoc DL = Op.getDebugLoc();
449
450   SDValue Mask = Op.getOperand(0);
451   SDValue Op1 = Op.getOperand(1);
452   SDValue Op2 = Op.getOperand(2);
453
454   assert(VT.isVector() && !Mask.getValueType().isVector()
455          && Op1.getValueType() == Op2.getValueType() && "Invalid type");
456
457   unsigned NumElem = VT.getVectorNumElements();
458
459   // If we can't even use the basic vector operations of
460   // AND,OR,XOR, we will have to scalarize the op.
461   // Notice that the operation may be 'promoted' which means that it is
462   // 'bitcasted' to another type which is handled.
463   // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
464   if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
465       TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
466       TLI.getOperationAction(ISD::OR,  VT) == TargetLowering::Expand ||
467       TLI.getOperationAction(ISD::BUILD_VECTOR,  VT) == TargetLowering::Expand)
468     return DAG.UnrollVectorOp(Op.getNode());
469
470   // Generate a mask operand.
471   EVT MaskTy = TLI.getSetCCResultType(VT);
472   assert(MaskTy.isVector() && "Invalid CC type");
473   assert(MaskTy.getSizeInBits() == Op1.getValueType().getSizeInBits()
474          && "Invalid mask size");
475
476   // What is the size of each element in the vector mask.
477   EVT BitTy = MaskTy.getScalarType();
478
479   Mask = DAG.getNode(ISD::SELECT, DL, BitTy, Mask,
480           DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), BitTy),
481           DAG.getConstant(0, BitTy));
482
483   // Broadcast the mask so that the entire vector is all-one or all zero.
484   SmallVector<SDValue, 8> Ops(NumElem, Mask);
485   Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, &Ops[0], Ops.size());
486
487   // Bitcast the operands to be the same type as the mask.
488   // This is needed when we select between FP types because
489   // the mask is a vector of integers.
490   Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
491   Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
492
493   SDValue AllOnes = DAG.getConstant(
494             APInt::getAllOnesValue(BitTy.getSizeInBits()), MaskTy);
495   SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes);
496
497   Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
498   Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
499   SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
500   return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
501 }
502
503 SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
504   // Implement VSELECT in terms of XOR, AND, OR
505   // on platforms which do not support blend natively.
506   EVT VT =  Op.getOperand(0).getValueType();
507   DebugLoc DL = Op.getDebugLoc();
508
509   SDValue Mask = Op.getOperand(0);
510   SDValue Op1 = Op.getOperand(1);
511   SDValue Op2 = Op.getOperand(2);
512
513   // If we can't even use the basic vector operations of
514   // AND,OR,XOR, we will have to scalarize the op.
515   // Notice that the operation may be 'promoted' which means that it is
516   // 'bitcasted' to another type which is handled.
517   // This operation also isn't safe with AND, OR, XOR when the boolean
518   // type is 0/1 as we need an all ones vector constant to mask with.
519   // FIXME: Sign extend 1 to all ones if thats legal on the target.
520   if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
521       TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
522       TLI.getOperationAction(ISD::OR,  VT) == TargetLowering::Expand ||
523       TLI.getBooleanContents(true) !=
524       TargetLowering::ZeroOrNegativeOneBooleanContent)
525     return DAG.UnrollVectorOp(Op.getNode());
526
527   assert(VT.getSizeInBits() == Op1.getValueType().getSizeInBits()
528          && "Invalid mask size");
529   // Bitcast the operands to be the same type as the mask.
530   // This is needed when we select between FP types because
531   // the mask is a vector of integers.
532   Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
533   Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
534
535   SDValue AllOnes = DAG.getConstant(
536     APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT);
537   SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
538
539   Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
540   Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
541   SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
542   return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
543 }
544
545 SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
546   EVT VT = Op.getOperand(0).getValueType();
547   DebugLoc DL = Op.getDebugLoc();
548
549   // Make sure that the SINT_TO_FP and SRL instructions are available.
550   if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
551       TLI.getOperationAction(ISD::SRL,        VT) == TargetLowering::Expand)
552     return DAG.UnrollVectorOp(Op.getNode());
553
554  EVT SVT = VT.getScalarType();
555   assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
556       "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
557
558   unsigned BW = SVT.getSizeInBits();
559   SDValue HalfWord = DAG.getConstant(BW/2, VT);
560
561   // Constants to clear the upper part of the word.
562   // Notice that we can also use SHL+SHR, but using a constant is slightly
563   // faster on x86.
564   uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
565   SDValue HalfWordMask = DAG.getConstant(HWMask, VT);
566
567   // Two to the power of half-word-size.
568   SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType());
569
570   // Clear upper part of LO, lower HI
571   SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
572   SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
573
574   // Convert hi and lo to floats
575   // Convert the hi part back to the upper values
576   SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
577           fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
578   SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
579
580   // Add the two halves
581   return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
582 }
583
584
585 SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
586   if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
587     SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType());
588     return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
589                        Zero, Op.getOperand(0));
590   }
591   return DAG.UnrollVectorOp(Op.getNode());
592 }
593
594 SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
595   EVT VT = Op.getValueType();
596   unsigned NumElems = VT.getVectorNumElements();
597   EVT EltVT = VT.getVectorElementType();
598   SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
599   EVT TmpEltVT = LHS.getValueType().getVectorElementType();
600   DebugLoc dl = Op.getDebugLoc();
601   SmallVector<SDValue, 8> Ops(NumElems);
602   for (unsigned i = 0; i < NumElems; ++i) {
603     SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
604                                   DAG.getIntPtrConstant(i));
605     SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
606                                   DAG.getIntPtrConstant(i));
607     Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
608                          LHSElem, RHSElem, CC);
609     Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i],
610                          DAG.getConstant(APInt::getAllOnesValue
611                                          (EltVT.getSizeInBits()), EltVT),
612                          DAG.getConstant(0, EltVT));
613   }
614   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
615 }
616
617 }
618
619 bool SelectionDAG::LegalizeVectors() {
620   return VectorLegalizer(*this).Run();
621 }