4d60211426108c35c0325779ce76bb5f7efd920a
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeVectorTypes.cpp
1 //===------- LegalizeVectorTypes.cpp - Legalization of vector 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 performs vector type splitting and scalarization for LegalizeTypes.
11 // Scalarization is the act of changing a computation in an illegal one-element
12 // vector type to be a computation in its scalar element type.  For example,
13 // implementing <1 x f32> arithmetic in a scalar f32 register.  This is needed
14 // as a base case when scalarizing vector arithmetic like <4 x f32>, which
15 // eventually decomposes to scalars if the target doesn't support v4f32 or v2f32
16 // types.
17 // Splitting is the act of changing a computation in an invalid vector type to
18 // be a computation in multiple vectors of a smaller type.  For example,
19 // implementing <128 x f32> operations in terms of two <64 x f32> operations.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #include "LegalizeTypes.h"
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 //  Result Vector Scalarization: <1 x ty> -> ty.
28 //===----------------------------------------------------------------------===//
29
30 void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
31   DEBUG(cerr << "Scalarize node result " << ResNo << ": "; N->dump(&DAG);
32         cerr << "\n");
33   SDValue R = SDValue();
34
35   switch (N->getOpcode()) {
36   default:
37 #ifndef NDEBUG
38     cerr << "ScalarizeVectorResult #" << ResNo << ": ";
39     N->dump(&DAG); cerr << "\n";
40 #endif
41     assert(0 && "Do not know how to scalarize the result of this operator!");
42     abort();
43
44   case ISD::BIT_CONVERT:       R = ScalarizeVecRes_BIT_CONVERT(N); break;
45   case ISD::BUILD_VECTOR:      R = N->getOperand(0); break;
46   case ISD::CONVERT_RNDSAT:    R = ScalarizeVecRes_CONVERT_RNDSAT(N); break;
47   case ISD::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
48   case ISD::FPOWI:             R = ScalarizeVecRes_FPOWI(N); break;
49   case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
50   case ISD::LOAD:           R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
51   case ISD::SCALAR_TO_VECTOR:  R = ScalarizeVecRes_SCALAR_TO_VECTOR(N); break;
52   case ISD::SELECT:            R = ScalarizeVecRes_SELECT(N); break;
53   case ISD::SELECT_CC:         R = ScalarizeVecRes_SELECT_CC(N); break;
54   case ISD::UNDEF:             R = ScalarizeVecRes_UNDEF(N); break;
55   case ISD::VECTOR_SHUFFLE:    R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
56   case ISD::VSETCC:            R = ScalarizeVecRes_VSETCC(N); break;
57
58   case ISD::CTLZ:
59   case ISD::CTPOP:
60   case ISD::CTTZ:
61   case ISD::FABS:
62   case ISD::FCOS:
63   case ISD::FNEG:
64   case ISD::FP_TO_SINT:
65   case ISD::FP_TO_UINT:
66   case ISD::FSIN:
67   case ISD::FSQRT:
68   case ISD::FTRUNC:
69   case ISD::FFLOOR:
70   case ISD::FCEIL:
71   case ISD::FRINT:
72   case ISD::FNEARBYINT:
73   case ISD::SINT_TO_FP:
74   case ISD::TRUNCATE:
75   case ISD::UINT_TO_FP: R = ScalarizeVecRes_UnaryOp(N); break;
76
77   case ISD::ADD:
78   case ISD::AND:
79   case ISD::FADD:
80   case ISD::FDIV:
81   case ISD::FMUL:
82   case ISD::FPOW:
83   case ISD::FREM:
84   case ISD::FSUB:
85   case ISD::MUL:
86   case ISD::OR:
87   case ISD::SDIV:
88   case ISD::SREM:
89   case ISD::SUB:
90   case ISD::UDIV:
91   case ISD::UREM:
92   case ISD::XOR:  R = ScalarizeVecRes_BinOp(N); break;
93   }
94
95   // If R is null, the sub-method took care of registering the result.
96   if (R.getNode())
97     SetScalarizedVector(SDValue(N, ResNo), R);
98 }
99
100 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
101   SDValue LHS = GetScalarizedVector(N->getOperand(0));
102   SDValue RHS = GetScalarizedVector(N->getOperand(1));
103   return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
104 }
105
106 SDValue DAGTypeLegalizer::ScalarizeVecRes_BIT_CONVERT(SDNode *N) {
107   MVT NewVT = N->getValueType(0).getVectorElementType();
108   return DAG.getNode(ISD::BIT_CONVERT, NewVT, N->getOperand(0));
109 }
110
111 SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_RNDSAT(SDNode *N) {
112   MVT NewVT = N->getValueType(0).getVectorElementType();
113   SDValue Op0 = GetScalarizedVector(N->getOperand(0));
114   return DAG.getConvertRndSat(NewVT, Op0, DAG.getValueType(NewVT),
115                               DAG.getValueType(Op0.getValueType()),
116                               N->getOperand(3),
117                               N->getOperand(4),
118                               cast<CvtRndSatSDNode>(N)->getCvtCode());
119 }
120
121 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
122   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
123                      N->getValueType(0).getVectorElementType(),
124                      N->getOperand(0), N->getOperand(1));
125 }
126
127 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
128   SDValue Op = GetScalarizedVector(N->getOperand(0));
129   return DAG.getNode(ISD::FPOWI, Op.getValueType(), Op, N->getOperand(1));
130 }
131
132 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
133   // The value to insert may have a wider type than the vector element type,
134   // so be sure to truncate it to the element type if necessary.
135   SDValue Op = N->getOperand(1);
136   MVT EltVT = N->getValueType(0).getVectorElementType();
137   if (Op.getValueType() != EltVT)
138     // FIXME: Can this happen for floating point types?
139     Op = DAG.getNode(ISD::TRUNCATE, EltVT, Op);
140   return Op;
141 }
142
143 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
144   assert(N->isUnindexed() && "Indexed vector load?");
145
146   SDValue Result = DAG.getLoad(ISD::UNINDEXED, N->getExtensionType(),
147                                N->getValueType(0).getVectorElementType(),
148                                N->getChain(), N->getBasePtr(),
149                                DAG.getNode(ISD::UNDEF,
150                                            N->getBasePtr().getValueType()),
151                                N->getSrcValue(), N->getSrcValueOffset(),
152                                N->getMemoryVT().getVectorElementType(),
153                                N->isVolatile(), N->getAlignment());
154
155   // Legalized the chain result - switch anything that used the old chain to
156   // use the new one.
157   ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
158   return Result;
159 }
160
161 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
162   // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
163   MVT DestVT = N->getValueType(0).getVectorElementType();
164   SDValue Op = GetScalarizedVector(N->getOperand(0));
165   return DAG.getNode(N->getOpcode(), DestVT, Op);
166 }
167
168 SDValue DAGTypeLegalizer::ScalarizeVecRes_SCALAR_TO_VECTOR(SDNode *N) {
169   return N->getOperand(0);
170 }
171
172 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
173   SDValue LHS = GetScalarizedVector(N->getOperand(1));
174   return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0), LHS,
175                      GetScalarizedVector(N->getOperand(2)));
176 }
177
178 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
179   SDValue LHS = GetScalarizedVector(N->getOperand(2));
180   return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(),
181                      N->getOperand(0), N->getOperand(1),
182                      LHS, GetScalarizedVector(N->getOperand(3)),
183                      N->getOperand(4));
184 }
185
186 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
187   return DAG.getNode(ISD::UNDEF, N->getValueType(0).getVectorElementType());
188 }
189
190 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
191   // Figure out if the scalar is the LHS or RHS and return it.
192   SDValue Arg = N->getOperand(2).getOperand(0);
193   if (Arg.getOpcode() == ISD::UNDEF)
194     return DAG.getNode(ISD::UNDEF, N->getValueType(0).getVectorElementType());
195   unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
196   return GetScalarizedVector(N->getOperand(Op));
197 }
198
199 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSETCC(SDNode *N) {
200   SDValue LHS = GetScalarizedVector(N->getOperand(0));
201   SDValue RHS = GetScalarizedVector(N->getOperand(1));
202   MVT NVT = N->getValueType(0).getVectorElementType();
203   MVT SVT = TLI.getSetCCResultType(LHS);
204
205   // Turn it into a scalar SETCC.
206   SDValue Res = DAG.getNode(ISD::SETCC, SVT, LHS, RHS, N->getOperand(2));
207
208   // VSETCC always returns a sign-extended value, while SETCC may not.  The
209   // SETCC result type may not match the vector element type.  Correct these.
210   if (NVT.bitsLE(SVT)) {
211     // The SETCC result type is bigger than the vector element type.
212     // Ensure the SETCC result is sign-extended.
213     if (TLI.getBooleanContents() !=
214         TargetLowering::ZeroOrNegativeOneBooleanContent)
215       Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, SVT, Res,
216                         DAG.getValueType(MVT::i1));
217     // Truncate to the final type.
218     return DAG.getNode(ISD::TRUNCATE, NVT, Res);
219   } else {
220     // The SETCC result type is smaller than the vector element type.
221     // If the SetCC result is not sign-extended, chop it down to MVT::i1.
222     if (TLI.getBooleanContents() !=
223         TargetLowering::ZeroOrNegativeOneBooleanContent)
224       Res = DAG.getNode(ISD::TRUNCATE, MVT::i1, Res);
225     // Sign extend to the final type.
226     return DAG.getNode(ISD::SIGN_EXTEND, NVT, Res);
227   }
228 }
229
230
231 //===----------------------------------------------------------------------===//
232 //  Operand Vector Scalarization <1 x ty> -> ty.
233 //===----------------------------------------------------------------------===//
234
235 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
236   DEBUG(cerr << "Scalarize node operand " << OpNo << ": "; N->dump(&DAG);
237         cerr << "\n");
238   SDValue Res = SDValue();
239
240   if (Res.getNode() == 0) {
241     switch (N->getOpcode()) {
242     default:
243 #ifndef NDEBUG
244       cerr << "ScalarizeVectorOperand Op #" << OpNo << ": ";
245       N->dump(&DAG); cerr << "\n";
246 #endif
247       assert(0 && "Do not know how to scalarize this operator's operand!");
248       abort();
249
250     case ISD::BIT_CONVERT:
251       Res = ScalarizeVecOp_BIT_CONVERT(N); break;
252
253     case ISD::CONCAT_VECTORS:
254       Res = ScalarizeVecOp_CONCAT_VECTORS(N); break;
255
256     case ISD::EXTRACT_VECTOR_ELT:
257       Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N); break;
258
259     case ISD::STORE:
260       Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo); break;
261     }
262   }
263
264   // If the result is null, the sub-method took care of registering results etc.
265   if (!Res.getNode()) return false;
266
267   // If the result is N, the sub-method updated N in place.  Check to see if any
268   // operands are new, and if so, mark them.
269   if (Res.getNode() == N) {
270     // Mark N as new and remark N and its operands.  This allows us to correctly
271     // revisit N if it needs another step of promotion and allows us to visit
272     // any new operands to N.
273     ReanalyzeNode(N);
274     return true;
275   }
276
277   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
278          "Invalid operand expansion");
279
280   ReplaceValueWith(SDValue(N, 0), Res);
281   return false;
282 }
283
284 /// ScalarizeVecOp_BIT_CONVERT - If the value to convert is a vector that needs
285 /// to be scalarized, it must be <1 x ty>.  Convert the element instead.
286 SDValue DAGTypeLegalizer::ScalarizeVecOp_BIT_CONVERT(SDNode *N) {
287   SDValue Elt = GetScalarizedVector(N->getOperand(0));
288   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Elt);
289 }
290
291 /// ScalarizeVecOp_CONCAT_VECTORS - The vectors to concatenate have length one -
292 /// use a BUILD_VECTOR instead.
293 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
294   SmallVector<SDValue, 8> Ops(N->getNumOperands());
295   for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
296     Ops[i] = GetScalarizedVector(N->getOperand(i));
297   return DAG.getNode(ISD::BUILD_VECTOR, N->getValueType(0),
298                      &Ops[0], Ops.size());
299 }
300
301 /// ScalarizeVecOp_EXTRACT_VECTOR_ELT - If the input is a vector that needs to
302 /// be scalarized, it must be <1 x ty>, so just return the element, ignoring the
303 /// index.
304 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
305   return GetScalarizedVector(N->getOperand(0));
306 }
307
308 /// ScalarizeVecOp_STORE - If the value to store is a vector that needs to be
309 /// scalarized, it must be <1 x ty>.  Just store the element.
310 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
311   assert(N->isUnindexed() && "Indexed store of one-element vector?");
312   assert(OpNo == 1 && "Do not know how to scalarize this operand!");
313
314   if (N->isTruncatingStore())
315     return DAG.getTruncStore(N->getChain(),
316                              GetScalarizedVector(N->getOperand(1)),
317                              N->getBasePtr(),
318                              N->getSrcValue(), N->getSrcValueOffset(),
319                              N->getMemoryVT().getVectorElementType(),
320                              N->isVolatile(), N->getAlignment());
321
322   return DAG.getStore(N->getChain(), GetScalarizedVector(N->getOperand(1)),
323                       N->getBasePtr(), N->getSrcValue(), N->getSrcValueOffset(),
324                       N->isVolatile(), N->getAlignment());
325 }
326
327
328 //===----------------------------------------------------------------------===//
329 //  Result Vector Splitting
330 //===----------------------------------------------------------------------===//
331
332 /// SplitVectorResult - This method is called when the specified result of the
333 /// specified node is found to need vector splitting.  At this point, the node
334 /// may also have invalid operands or may have other results that need
335 /// legalization, we just know that (at least) one result needs vector
336 /// splitting.
337 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
338   DEBUG(cerr << "Split node result: "; N->dump(&DAG); cerr << "\n");
339   SDValue Lo, Hi;
340
341   switch (N->getOpcode()) {
342   default:
343 #ifndef NDEBUG
344     cerr << "SplitVectorResult #" << ResNo << ": ";
345     N->dump(&DAG); cerr << "\n";
346 #endif
347     assert(0 && "Do not know how to split the result of this operator!");
348     abort();
349
350   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
351   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
352   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
353   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
354
355   case ISD::BIT_CONVERT:       SplitVecRes_BIT_CONVERT(N, Lo, Hi); break;
356   case ISD::BUILD_VECTOR:      SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
357   case ISD::CONCAT_VECTORS:    SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
358   case ISD::CONVERT_RNDSAT:    SplitVecRes_CONVERT_RNDSAT(N, Lo, Hi); break;
359   case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
360   case ISD::FPOWI:             SplitVecRes_FPOWI(N, Lo, Hi); break;
361   case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
362   case ISD::SCALAR_TO_VECTOR:  SplitVecRes_SCALAR_TO_VECTOR(N, Lo, Hi); break;
363   case ISD::LOAD:           SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);break;
364   case ISD::VECTOR_SHUFFLE:    SplitVecRes_VECTOR_SHUFFLE(N, Lo, Hi); break;
365   case ISD::VSETCC:            SplitVecRes_VSETCC(N, Lo, Hi); break;
366
367   case ISD::CTTZ:
368   case ISD::CTLZ:
369   case ISD::CTPOP:
370   case ISD::FNEG:
371   case ISD::FABS:
372   case ISD::FSQRT:
373   case ISD::FSIN:
374   case ISD::FCOS:
375   case ISD::FTRUNC:
376   case ISD::FFLOOR:
377   case ISD::FCEIL:
378   case ISD::FRINT:
379   case ISD::FNEARBYINT:
380   case ISD::FP_TO_SINT:
381   case ISD::FP_TO_UINT:
382   case ISD::SINT_TO_FP:
383   case ISD::TRUNCATE:
384   case ISD::UINT_TO_FP: SplitVecRes_UnaryOp(N, Lo, Hi); break;
385
386   case ISD::ADD:
387   case ISD::SUB:
388   case ISD::MUL:
389   case ISD::FADD:
390   case ISD::FSUB:
391   case ISD::FMUL:
392   case ISD::SDIV:
393   case ISD::UDIV:
394   case ISD::FDIV:
395   case ISD::FPOW:
396   case ISD::AND:
397   case ISD::OR:
398   case ISD::XOR:
399   case ISD::UREM:
400   case ISD::SREM:
401   case ISD::FREM: SplitVecRes_BinOp(N, Lo, Hi); break;
402   }
403
404   // If Lo/Hi is null, the sub-method took care of registering results etc.
405   if (Lo.getNode())
406     SetSplitVector(SDValue(N, ResNo), Lo, Hi);
407 }
408
409 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
410                                          SDValue &Hi) {
411   SDValue LHSLo, LHSHi;
412   GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
413   SDValue RHSLo, RHSHi;
414   GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
415
416   Lo = DAG.getNode(N->getOpcode(), LHSLo.getValueType(), LHSLo, RHSLo);
417   Hi = DAG.getNode(N->getOpcode(), LHSHi.getValueType(), LHSHi, RHSHi);
418 }
419
420 void DAGTypeLegalizer::SplitVecRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
421                                                SDValue &Hi) {
422   // We know the result is a vector.  The input may be either a vector or a
423   // scalar value.
424   MVT LoVT, HiVT;
425   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
426
427   SDValue InOp = N->getOperand(0);
428   MVT InVT = InOp.getValueType();
429
430   // Handle some special cases efficiently.
431   switch (getTypeAction(InVT)) {
432   default:
433     assert(false && "Unknown type action!");
434   case Legal:
435   case PromoteInteger:
436   case SoftenFloat:
437   case ScalarizeVector:
438     break;
439   case ExpandInteger:
440   case ExpandFloat:
441     // A scalar to vector conversion, where the scalar needs expansion.
442     // If the vector is being split in two then we can just convert the
443     // expanded pieces.
444     if (LoVT == HiVT) {
445       GetExpandedOp(InOp, Lo, Hi);
446       if (TLI.isBigEndian())
447         std::swap(Lo, Hi);
448       Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
449       Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
450       return;
451     }
452     break;
453   case SplitVector:
454     // If the input is a vector that needs to be split, convert each split
455     // piece of the input now.
456     GetSplitVector(InOp, Lo, Hi);
457     Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
458     Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
459     return;
460   }
461
462   // In the general case, convert the input to an integer and split it by hand.
463   MVT LoIntVT = MVT::getIntegerVT(LoVT.getSizeInBits());
464   MVT HiIntVT = MVT::getIntegerVT(HiVT.getSizeInBits());
465   if (TLI.isBigEndian())
466     std::swap(LoIntVT, HiIntVT);
467
468   SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
469
470   if (TLI.isBigEndian())
471     std::swap(Lo, Hi);
472   Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
473   Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
474 }
475
476 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
477                                                 SDValue &Hi) {
478   MVT LoVT, HiVT;
479   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
480   unsigned LoNumElts = LoVT.getVectorNumElements();
481   SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
482   Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &LoOps[0], LoOps.size());
483
484   SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
485   Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &HiOps[0], HiOps.size());
486 }
487
488 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
489                                                   SDValue &Hi) {
490   assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
491   unsigned NumSubvectors = N->getNumOperands() / 2;
492   if (NumSubvectors == 1) {
493     Lo = N->getOperand(0);
494     Hi = N->getOperand(1);
495     return;
496   }
497
498   MVT LoVT, HiVT;
499   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
500
501   SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
502   Lo = DAG.getNode(ISD::CONCAT_VECTORS, LoVT, &LoOps[0], LoOps.size());
503
504   SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
505   Hi = DAG.getNode(ISD::CONCAT_VECTORS, HiVT, &HiOps[0], HiOps.size());
506 }
507
508 void DAGTypeLegalizer::SplitVecRes_CONVERT_RNDSAT(SDNode *N, SDValue &Lo,
509                                                   SDValue &Hi) {
510   MVT LoVT, HiVT;
511   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
512   SDValue VLo, VHi;
513   GetSplitVector(N->getOperand(0), VLo, VHi);
514   SDValue DTyOpLo =  DAG.getValueType(LoVT);
515   SDValue DTyOpHi =  DAG.getValueType(HiVT);
516   SDValue STyOpLo =  DAG.getValueType(VLo.getValueType());
517   SDValue STyOpHi =  DAG.getValueType(VHi.getValueType());
518
519   SDValue RndOp = N->getOperand(3);
520   SDValue SatOp = N->getOperand(4);
521   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
522
523   Lo = DAG.getConvertRndSat(LoVT, VLo, DTyOpLo, STyOpLo, RndOp, SatOp, CvtCode);
524   Hi = DAG.getConvertRndSat(HiVT, VHi, DTyOpHi, STyOpHi, RndOp, SatOp, CvtCode);
525 }
526
527 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
528                                                      SDValue &Hi) {
529   SDValue Vec = N->getOperand(0);
530   SDValue Idx = N->getOperand(1);
531   MVT IdxVT = Idx.getValueType();
532
533   MVT LoVT, HiVT;
534   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
535   // The indices are not guaranteed to be a multiple of the new vector
536   // size unless the original vector type was split in two.
537   assert(LoVT == HiVT && "Non power-of-two vectors not supported!");
538
539   Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, LoVT, Vec, Idx);
540   Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
541                     DAG.getConstant(LoVT.getVectorNumElements(), IdxVT));
542   Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, HiVT, Vec, Idx);
543 }
544
545 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
546                                          SDValue &Hi) {
547   GetSplitVector(N->getOperand(0), Lo, Hi);
548   Lo = DAG.getNode(ISD::FPOWI, Lo.getValueType(), Lo, N->getOperand(1));
549   Hi = DAG.getNode(ISD::FPOWI, Hi.getValueType(), Hi, N->getOperand(1));
550 }
551
552 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
553                                                      SDValue &Hi) {
554   SDValue Vec = N->getOperand(0);
555   SDValue Elt = N->getOperand(1);
556   SDValue Idx = N->getOperand(2);
557   GetSplitVector(Vec, Lo, Hi);
558
559   if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
560     unsigned IdxVal = CIdx->getZExtValue();
561     unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
562     if (IdxVal < LoNumElts)
563       Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, Lo.getValueType(), Lo, Elt, Idx);
564     else
565       Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, Hi.getValueType(), Hi, Elt,
566                        DAG.getIntPtrConstant(IdxVal - LoNumElts));
567     return;
568   }
569
570   // Spill the vector to the stack.
571   MVT VecVT = Vec.getValueType();
572   MVT EltVT = VecVT.getVectorElementType();
573   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
574   SDValue Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
575
576   // Store the new element.  This may be larger than the vector element type,
577   // so use a truncating store.
578   SDValue EltPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
579   Store = DAG.getTruncStore(Store, Elt, EltPtr, NULL, 0, EltVT);
580
581   // Reload the vector from the stack.
582   SDValue Load = DAG.getLoad(VecVT, Store, StackPtr, NULL, 0);
583
584   // Split it.
585   SplitVecRes_LOAD(cast<LoadSDNode>(Load.getNode()), Lo, Hi);
586 }
587
588 void DAGTypeLegalizer::SplitVecRes_SCALAR_TO_VECTOR(SDNode *N, SDValue &Lo,
589                                                     SDValue &Hi) {
590   MVT LoVT, HiVT;
591   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
592   Lo = DAG.getNode(ISD::SCALAR_TO_VECTOR, LoVT, N->getOperand(0));
593   Hi = DAG.getNode(ISD::UNDEF, HiVT);
594 }
595
596 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
597                                         SDValue &Hi) {
598   assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
599   MVT LoVT, HiVT;
600   GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
601
602   ISD::LoadExtType ExtType = LD->getExtensionType();
603   SDValue Ch = LD->getChain();
604   SDValue Ptr = LD->getBasePtr();
605   SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
606   const Value *SV = LD->getSrcValue();
607   int SVOffset = LD->getSrcValueOffset();
608   MVT MemoryVT = LD->getMemoryVT();
609   unsigned Alignment = LD->getAlignment();
610   bool isVolatile = LD->isVolatile();
611
612   MVT LoMemVT, HiMemVT;
613   GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
614
615   Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, Ch, Ptr, Offset,
616                    SV, SVOffset, LoMemVT, isVolatile, Alignment);
617
618   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
619   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
620                     DAG.getIntPtrConstant(IncrementSize));
621   SVOffset += IncrementSize;
622   Alignment = MinAlign(Alignment, IncrementSize);
623   Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, Ch, Ptr, Offset,
624                    SV, SVOffset, HiMemVT, isVolatile, Alignment);
625
626   // Build a factor node to remember that this load is independent of the
627   // other one.
628   Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
629                    Hi.getValue(1));
630
631   // Legalized the chain result - switch anything that used the old chain to
632   // use the new one.
633   ReplaceValueWith(SDValue(LD, 1), Ch);
634 }
635
636 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
637                                            SDValue &Hi) {
638   // Get the dest types - they may not match the input types, e.g. int_to_fp.
639   MVT LoVT, HiVT;
640   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
641
642   // Split the input.
643   MVT InVT = N->getOperand(0).getValueType();
644   switch (getTypeAction(InVT)) {
645   default: assert(0 && "Unexpected type action!");
646   case Legal: {
647     assert(LoVT == HiVT && "Legal non-power-of-two vector type?");
648     MVT InNVT = MVT::getVectorVT(InVT.getVectorElementType(),
649                                  LoVT.getVectorNumElements());
650     Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, InNVT, N->getOperand(0),
651                      DAG.getIntPtrConstant(0));
652     Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, InNVT, N->getOperand(0),
653                      DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
654     break;
655   }
656   case SplitVector:
657     GetSplitVector(N->getOperand(0), Lo, Hi);
658     break;
659   }
660
661   Lo = DAG.getNode(N->getOpcode(), LoVT, Lo);
662   Hi = DAG.getNode(N->getOpcode(), HiVT, Hi);
663 }
664
665 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(SDNode *N, SDValue &Lo,
666                                                   SDValue &Hi) {
667   // The low and high parts of the original input give four input vectors.
668   SDValue Inputs[4];
669   GetSplitVector(N->getOperand(0), Inputs[0], Inputs[1]);
670   GetSplitVector(N->getOperand(1), Inputs[2], Inputs[3]);
671   MVT NewVT = Inputs[0].getValueType();
672   unsigned NewElts = NewVT.getVectorNumElements();
673   assert(NewVT == Inputs[1].getValueType() &&
674          "Non power-of-two vectors not supported!");
675
676   // If Lo or Hi uses elements from at most two of the four input vectors, then
677   // express it as a vector shuffle of those two inputs.  Otherwise extract the
678   // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR.
679   SDValue Mask = N->getOperand(2);
680   MVT IdxVT = Mask.getValueType().getVectorElementType();
681   SmallVector<SDValue, 16> Ops;
682   Ops.reserve(NewElts);
683   for (unsigned High = 0; High < 2; ++High) {
684     SDValue &Output = High ? Hi : Lo;
685
686     // Build a shuffle mask for the output, discovering on the fly which
687     // input vectors to use as shuffle operands (recorded in InputUsed).
688     // If building a suitable shuffle vector proves too hard, then bail
689     // out with useBuildVector set.
690     unsigned InputUsed[2] = { -1U, -1U }; // Not yet discovered.
691     unsigned FirstMaskIdx = High * NewElts;
692     bool useBuildVector = false;
693     for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
694       SDValue Arg = Mask.getOperand(FirstMaskIdx + MaskOffset);
695
696       // The mask element.  This indexes into the input.
697       unsigned Idx = Arg.getOpcode() == ISD::UNDEF ?
698         -1U : cast<ConstantSDNode>(Arg)->getZExtValue();
699
700       // The input vector this mask element indexes into.
701       unsigned Input = Idx / NewElts;
702
703       if (Input >= array_lengthof(Inputs)) {
704         // The mask element does not index into any input vector.
705         Ops.push_back(DAG.getNode(ISD::UNDEF, IdxVT));
706         continue;
707       }
708
709       // Turn the index into an offset from the start of the input vector.
710       Idx -= Input * NewElts;
711
712       // Find or create a shuffle vector operand to hold this input.
713       unsigned OpNo;
714       for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
715         if (InputUsed[OpNo] == Input) {
716           // This input vector is already an operand.
717           break;
718         } else if (InputUsed[OpNo] == -1U) {
719           // Create a new operand for this input vector.
720           InputUsed[OpNo] = Input;
721           break;
722         }
723       }
724
725       if (OpNo >= array_lengthof(InputUsed)) {
726         // More than two input vectors used!  Give up on trying to create a
727         // shuffle vector.  Insert all elements into a BUILD_VECTOR instead.
728         useBuildVector = true;
729         break;
730       }
731
732       // Add the mask index for the new shuffle vector.
733       Ops.push_back(DAG.getConstant(Idx + OpNo * NewElts, IdxVT));
734     }
735
736     if (useBuildVector) {
737       MVT EltVT = NewVT.getVectorElementType();
738       Ops.clear();
739
740       // Extract the input elements by hand.
741       for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
742         SDValue Arg = Mask.getOperand(FirstMaskIdx + MaskOffset);
743
744         // The mask element.  This indexes into the input.
745         unsigned Idx = Arg.getOpcode() == ISD::UNDEF ?
746           -1U : cast<ConstantSDNode>(Arg)->getZExtValue();
747
748         // The input vector this mask element indexes into.
749         unsigned Input = Idx / NewElts;
750
751         if (Input >= array_lengthof(Inputs)) {
752           // The mask element is "undef" or indexes off the end of the input.
753           Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
754           continue;
755         }
756
757         // Turn the index into an offset from the start of the input vector.
758         Idx -= Input * NewElts;
759
760         // Extract the vector element by hand.
761         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT,
762                                   Inputs[Input], DAG.getIntPtrConstant(Idx)));
763       }
764
765       // Construct the Lo/Hi output using a BUILD_VECTOR.
766       Output = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &Ops[0], Ops.size());
767     } else if (InputUsed[0] == -1U) {
768       // No input vectors were used!  The result is undefined.
769       Output = DAG.getNode(ISD::UNDEF, NewVT);
770     } else {
771       // At least one input vector was used.  Create a new shuffle vector.
772       SDValue NewMask = DAG.getNode(ISD::BUILD_VECTOR,
773                                     MVT::getVectorVT(IdxVT, Ops.size()),
774                                     &Ops[0], Ops.size());
775       SDValue Op0 = Inputs[InputUsed[0]];
776       // If only one input was used, use an undefined vector for the other.
777       SDValue Op1 = InputUsed[1] == -1U ?
778         DAG.getNode(ISD::UNDEF, NewVT) : Inputs[InputUsed[1]];
779       Output = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, Op0, Op1, NewMask);
780     }
781
782     Ops.clear();
783   }
784 }
785
786 void DAGTypeLegalizer::SplitVecRes_VSETCC(SDNode *N, SDValue &Lo,
787                                           SDValue &Hi) {
788   MVT LoVT, HiVT;
789   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
790
791   SDValue LL, LH, RL, RH;
792   GetSplitVector(N->getOperand(0), LL, LH);
793   GetSplitVector(N->getOperand(1), RL, RH);
794
795   Lo = DAG.getNode(ISD::VSETCC, LoVT, LL, RL, N->getOperand(2));
796   Hi = DAG.getNode(ISD::VSETCC, HiVT, LH, RH, N->getOperand(2));
797 }
798
799
800 //===----------------------------------------------------------------------===//
801 //  Operand Vector Splitting
802 //===----------------------------------------------------------------------===//
803
804 /// SplitVectorOperand - This method is called when the specified operand of the
805 /// specified node is found to need vector splitting.  At this point, all of the
806 /// result types of the node are known to be legal, but other operands of the
807 /// node may need legalization as well as the specified one.
808 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
809   DEBUG(cerr << "Split node operand: "; N->dump(&DAG); cerr << "\n");
810   SDValue Res = SDValue();
811
812   if (Res.getNode() == 0) {
813     switch (N->getOpcode()) {
814     default:
815 #ifndef NDEBUG
816       cerr << "SplitVectorOperand Op #" << OpNo << ": ";
817       N->dump(&DAG); cerr << "\n";
818 #endif
819       assert(0 && "Do not know how to split this operator's operand!");
820       abort();
821
822     case ISD::BIT_CONVERT:       Res = SplitVecOp_BIT_CONVERT(N); break;
823     case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
824     case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
825     case ISD::STORE:             Res = SplitVecOp_STORE(cast<StoreSDNode>(N),
826                                                         OpNo); break;
827     case ISD::VECTOR_SHUFFLE:    Res = SplitVecOp_VECTOR_SHUFFLE(N, OpNo);break;
828
829     case ISD::CTTZ:
830     case ISD::CTLZ:
831     case ISD::CTPOP:
832     case ISD::FP_TO_SINT:
833     case ISD::FP_TO_UINT:
834     case ISD::SINT_TO_FP:
835     case ISD::TRUNCATE:
836     case ISD::UINT_TO_FP: Res = SplitVecOp_UnaryOp(N); break;
837     }
838   }
839
840   // If the result is null, the sub-method took care of registering results etc.
841   if (!Res.getNode()) return false;
842
843   // If the result is N, the sub-method updated N in place.  Check to see if any
844   // operands are new, and if so, mark them.
845   if (Res.getNode() == N) {
846     // Mark N as new and remark N and its operands.  This allows us to correctly
847     // revisit N if it needs another step of promotion and allows us to visit
848     // any new operands to N.
849     ReanalyzeNode(N);
850     return true;
851   }
852
853   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
854          "Invalid operand expansion");
855
856   ReplaceValueWith(SDValue(N, 0), Res);
857   return false;
858 }
859
860 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
861   // The result has a legal vector type, but the input needs splitting.
862   MVT ResVT = N->getValueType(0);
863   SDValue Lo, Hi;
864   GetSplitVector(N->getOperand(0), Lo, Hi);
865   assert(Lo.getValueType() == Hi.getValueType() &&
866          "Returns legal non-power-of-two vector type?");
867   MVT InVT = Lo.getValueType();
868
869   MVT OutVT = MVT::getVectorVT(ResVT.getVectorElementType(),
870                                InVT.getVectorNumElements());
871
872   Lo = DAG.getNode(N->getOpcode(), OutVT, Lo);
873   Hi = DAG.getNode(N->getOpcode(), OutVT, Hi);
874
875   return DAG.getNode(ISD::CONCAT_VECTORS, ResVT, Lo, Hi);
876 }
877
878 SDValue DAGTypeLegalizer::SplitVecOp_BIT_CONVERT(SDNode *N) {
879   // For example, i64 = BIT_CONVERT v4i16 on alpha.  Typically the vector will
880   // end up being split all the way down to individual components.  Convert the
881   // split pieces into integers and reassemble.
882   SDValue Lo, Hi;
883   GetSplitVector(N->getOperand(0), Lo, Hi);
884   Lo = BitConvertToInteger(Lo);
885   Hi = BitConvertToInteger(Hi);
886
887   if (TLI.isBigEndian())
888     std::swap(Lo, Hi);
889
890   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
891                      JoinIntegers(Lo, Hi));
892 }
893
894 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
895   // We know that the extracted result type is legal.  For now, assume the index
896   // is a constant.
897   MVT SubVT = N->getValueType(0);
898   SDValue Idx = N->getOperand(1);
899   SDValue Lo, Hi;
900   GetSplitVector(N->getOperand(0), Lo, Hi);
901
902   uint64_t LoElts = Lo.getValueType().getVectorNumElements();
903   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
904
905   if (IdxVal < LoElts) {
906     assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
907            "Extracted subvector crosses vector split!");
908     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Lo, Idx);
909   } else {
910     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Hi,
911                        DAG.getConstant(IdxVal - LoElts, Idx.getValueType()));
912   }
913 }
914
915 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
916   SDValue Vec = N->getOperand(0);
917   SDValue Idx = N->getOperand(1);
918   MVT VecVT = Vec.getValueType();
919
920   if (isa<ConstantSDNode>(Idx)) {
921     uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
922     assert(IdxVal < VecVT.getVectorNumElements() && "Invalid vector index!");
923
924     SDValue Lo, Hi;
925     GetSplitVector(Vec, Lo, Hi);
926
927     uint64_t LoElts = Lo.getValueType().getVectorNumElements();
928
929     if (IdxVal < LoElts)
930       return DAG.UpdateNodeOperands(SDValue(N, 0), Lo, Idx);
931     else
932       return DAG.UpdateNodeOperands(SDValue(N, 0), Hi,
933                                     DAG.getConstant(IdxVal - LoElts,
934                                                     Idx.getValueType()));
935   }
936
937   // Store the vector to the stack.
938   MVT EltVT = VecVT.getVectorElementType();
939   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
940   SDValue Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
941
942   // Load back the required element.
943   StackPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
944   return DAG.getLoad(EltVT, Store, StackPtr, NULL, 0);
945 }
946
947 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
948   assert(N->isUnindexed() && "Indexed store of vector?");
949   assert(OpNo == 1 && "Can only split the stored value");
950
951   bool isTruncating = N->isTruncatingStore();
952   SDValue Ch  = N->getChain();
953   SDValue Ptr = N->getBasePtr();
954   int SVOffset = N->getSrcValueOffset();
955   MVT MemoryVT = N->getMemoryVT();
956   unsigned Alignment = N->getAlignment();
957   bool isVol = N->isVolatile();
958   SDValue Lo, Hi;
959   GetSplitVector(N->getOperand(1), Lo, Hi);
960
961   MVT LoMemVT, HiMemVT;
962   GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
963
964   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
965
966   if (isTruncating)
967     Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
968                            LoMemVT, isVol, Alignment);
969   else
970     Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
971                       isVol, Alignment);
972
973   // Increment the pointer to the other half.
974   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
975                     DAG.getIntPtrConstant(IncrementSize));
976
977   if (isTruncating)
978     Hi = DAG.getTruncStore(Ch, Hi, Ptr,
979                            N->getSrcValue(), SVOffset+IncrementSize,
980                            HiMemVT,
981                            isVol, MinAlign(Alignment, IncrementSize));
982   else
983     Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
984                       isVol, MinAlign(Alignment, IncrementSize));
985
986   return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
987 }
988
989 SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_SHUFFLE(SDNode *N, unsigned OpNo) {
990   assert(OpNo == 2 && "Shuffle source type differs from result type?");
991   SDValue Mask = N->getOperand(2);
992   unsigned MaskLength = Mask.getValueType().getVectorNumElements();
993   unsigned LargestMaskEntryPlusOne = 2 * MaskLength;
994   unsigned MinimumBitWidth = Log2_32_Ceil(LargestMaskEntryPlusOne);
995
996   // Look for a legal vector type to place the mask values in.
997   // Note that there may not be *any* legal vector-of-integer
998   // type for which the element type is legal!
999   for (MVT::SimpleValueType EltVT = MVT::FIRST_INTEGER_VALUETYPE;
1000        EltVT <= MVT::LAST_INTEGER_VALUETYPE;
1001        // Integer values types are consecutively numbered.  Exploit this.
1002        EltVT = MVT::SimpleValueType(EltVT + 1)) {
1003
1004     // Is the element type big enough to hold the values?
1005     if (MVT(EltVT).getSizeInBits() < MinimumBitWidth)
1006       // Nope.
1007       continue;
1008
1009     // Is the vector type legal?
1010     MVT VecVT = MVT::getVectorVT(EltVT, MaskLength);
1011     if (!isTypeLegal(VecVT))
1012       // Nope.
1013       continue;
1014
1015     // If the element type is not legal, find a larger legal type to use for
1016     // the BUILD_VECTOR operands.  This is an ugly hack, but seems to work!
1017     // FIXME: The real solution is to change VECTOR_SHUFFLE into a variadic
1018     // node where the shuffle mask is a list of integer operands, #2 .. #2+n.
1019     for (MVT::SimpleValueType OpVT = EltVT; OpVT <= MVT::LAST_INTEGER_VALUETYPE;
1020          // Integer values types are consecutively numbered.  Exploit this.
1021          OpVT = MVT::SimpleValueType(OpVT + 1)) {
1022       if (!isTypeLegal(OpVT))
1023         continue;
1024
1025       // Success!  Rebuild the vector using the legal types.
1026       SmallVector<SDValue, 16> Ops(MaskLength);
1027       for (unsigned i = 0; i < MaskLength; ++i) {
1028         SDValue Arg = Mask.getOperand(i);
1029         if (Arg.getOpcode() == ISD::UNDEF) {
1030           Ops[i] = DAG.getNode(ISD::UNDEF, OpVT);
1031         } else {
1032           uint64_t Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
1033           Ops[i] = DAG.getConstant(Idx, OpVT);
1034         }
1035       }
1036       return DAG.UpdateNodeOperands(SDValue(N,0),
1037                                     N->getOperand(0), N->getOperand(1),
1038                                     DAG.getNode(ISD::BUILD_VECTOR,
1039                                                 VecVT, &Ops[0], Ops.size()));
1040     }
1041
1042     // Continuing is pointless - failure is certain.
1043     break;
1044   }
1045   assert(false && "Failed to find an appropriate mask type!");
1046   return SDValue(N, 0);
1047 }