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