Add a target legalize hook for SplitVectorOperand
[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 two vectors of half the size.  For example, implementing
19 // <128 x f32> operations in terms of two <64 x f32> operations.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #include "LegalizeTypes.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace llvm;
28
29 //===----------------------------------------------------------------------===//
30 //  Result Vector Scalarization: <1 x ty> -> ty.
31 //===----------------------------------------------------------------------===//
32
33 void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
34   DEBUG(dbgs() << "Scalarize node result " << ResNo << ": ";
35         N->dump(&DAG);
36         dbgs() << "\n");
37   SDValue R = SDValue();
38
39   switch (N->getOpcode()) {
40   default:
41 #ifndef NDEBUG
42     dbgs() << "ScalarizeVectorResult #" << ResNo << ": ";
43     N->dump(&DAG);
44     dbgs() << "\n";
45 #endif
46     report_fatal_error("Do not know how to scalarize the result of this "
47                        "operator!\n");
48
49   case ISD::MERGE_VALUES:      R = ScalarizeVecRes_MERGE_VALUES(N, ResNo);break;
50   case ISD::BITCAST:           R = ScalarizeVecRes_BITCAST(N); break;
51   case ISD::BUILD_VECTOR:      R = ScalarizeVecRes_BUILD_VECTOR(N); break;
52   case ISD::CONVERT_RNDSAT:    R = ScalarizeVecRes_CONVERT_RNDSAT(N); break;
53   case ISD::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
54   case ISD::FP_ROUND:          R = ScalarizeVecRes_FP_ROUND(N); break;
55   case ISD::FP_ROUND_INREG:    R = ScalarizeVecRes_InregOp(N); break;
56   case ISD::FPOWI:             R = ScalarizeVecRes_FPOWI(N); break;
57   case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
58   case ISD::LOAD:           R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
59   case ISD::SCALAR_TO_VECTOR:  R = ScalarizeVecRes_SCALAR_TO_VECTOR(N); break;
60   case ISD::SIGN_EXTEND_INREG: R = ScalarizeVecRes_InregOp(N); break;
61   case ISD::VSELECT:           R = ScalarizeVecRes_VSELECT(N); break;
62   case ISD::SELECT:            R = ScalarizeVecRes_SELECT(N); break;
63   case ISD::SELECT_CC:         R = ScalarizeVecRes_SELECT_CC(N); break;
64   case ISD::SETCC:             R = ScalarizeVecRes_SETCC(N); break;
65   case ISD::UNDEF:             R = ScalarizeVecRes_UNDEF(N); break;
66   case ISD::VECTOR_SHUFFLE:    R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
67   case ISD::ANY_EXTEND:
68   case ISD::CTLZ:
69   case ISD::CTPOP:
70   case ISD::CTTZ:
71   case ISD::FABS:
72   case ISD::FCEIL:
73   case ISD::FCOS:
74   case ISD::FEXP:
75   case ISD::FEXP2:
76   case ISD::FFLOOR:
77   case ISD::FLOG:
78   case ISD::FLOG10:
79   case ISD::FLOG2:
80   case ISD::FNEARBYINT:
81   case ISD::FNEG:
82   case ISD::FP_EXTEND:
83   case ISD::FP_TO_SINT:
84   case ISD::FP_TO_UINT:
85   case ISD::FRINT:
86   case ISD::FSIN:
87   case ISD::FSQRT:
88   case ISD::FTRUNC:
89   case ISD::SIGN_EXTEND:
90   case ISD::SINT_TO_FP:
91   case ISD::TRUNCATE:
92   case ISD::UINT_TO_FP:
93   case ISD::ZERO_EXTEND:
94     R = ScalarizeVecRes_UnaryOp(N);
95     break;
96
97   case ISD::ADD:
98   case ISD::AND:
99   case ISD::FADD:
100   case ISD::FDIV:
101   case ISD::FMUL:
102   case ISD::FPOW:
103   case ISD::FREM:
104   case ISD::FSUB:
105   case ISD::MUL:
106   case ISD::OR:
107   case ISD::SDIV:
108   case ISD::SREM:
109   case ISD::SUB:
110   case ISD::UDIV:
111   case ISD::UREM:
112   case ISD::XOR:
113   case ISD::SHL:
114   case ISD::SRA:
115   case ISD::SRL:
116     R = ScalarizeVecRes_BinOp(N);
117     break;
118   case ISD::FMA:
119     R = ScalarizeVecRes_TernaryOp(N);
120     break;
121   }
122
123   // If R is null, the sub-method took care of registering the result.
124   if (R.getNode())
125     SetScalarizedVector(SDValue(N, ResNo), R);
126 }
127
128 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
129   SDValue LHS = GetScalarizedVector(N->getOperand(0));
130   SDValue RHS = GetScalarizedVector(N->getOperand(1));
131   return DAG.getNode(N->getOpcode(), SDLoc(N),
132                      LHS.getValueType(), LHS, RHS);
133 }
134
135 SDValue DAGTypeLegalizer::ScalarizeVecRes_TernaryOp(SDNode *N) {
136   SDValue Op0 = GetScalarizedVector(N->getOperand(0));
137   SDValue Op1 = GetScalarizedVector(N->getOperand(1));
138   SDValue Op2 = GetScalarizedVector(N->getOperand(2));
139   return DAG.getNode(N->getOpcode(), SDLoc(N),
140                      Op0.getValueType(), Op0, Op1, Op2);
141 }
142
143 SDValue DAGTypeLegalizer::ScalarizeVecRes_MERGE_VALUES(SDNode *N,
144                                                        unsigned ResNo) {
145   SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
146   return GetScalarizedVector(Op);
147 }
148
149 SDValue DAGTypeLegalizer::ScalarizeVecRes_BITCAST(SDNode *N) {
150   EVT NewVT = N->getValueType(0).getVectorElementType();
151   return DAG.getNode(ISD::BITCAST, SDLoc(N),
152                      NewVT, N->getOperand(0));
153 }
154
155 SDValue DAGTypeLegalizer::ScalarizeVecRes_BUILD_VECTOR(SDNode *N) {
156   EVT EltVT = N->getValueType(0).getVectorElementType();
157   SDValue InOp = N->getOperand(0);
158   // The BUILD_VECTOR operands may be of wider element types and
159   // we may need to truncate them back to the requested return type.
160   if (EltVT.isInteger())
161     return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
162   return InOp;
163 }
164
165 SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_RNDSAT(SDNode *N) {
166   EVT NewVT = N->getValueType(0).getVectorElementType();
167   SDValue Op0 = GetScalarizedVector(N->getOperand(0));
168   return DAG.getConvertRndSat(NewVT, SDLoc(N),
169                               Op0, DAG.getValueType(NewVT),
170                               DAG.getValueType(Op0.getValueType()),
171                               N->getOperand(3),
172                               N->getOperand(4),
173                               cast<CvtRndSatSDNode>(N)->getCvtCode());
174 }
175
176 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
177   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
178                      N->getValueType(0).getVectorElementType(),
179                      N->getOperand(0), N->getOperand(1));
180 }
181
182 SDValue DAGTypeLegalizer::ScalarizeVecRes_FP_ROUND(SDNode *N) {
183   EVT NewVT = N->getValueType(0).getVectorElementType();
184   SDValue Op = GetScalarizedVector(N->getOperand(0));
185   return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
186                      NewVT, Op, N->getOperand(1));
187 }
188
189 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
190   SDValue Op = GetScalarizedVector(N->getOperand(0));
191   return DAG.getNode(ISD::FPOWI, SDLoc(N),
192                      Op.getValueType(), Op, N->getOperand(1));
193 }
194
195 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
196   // The value to insert may have a wider type than the vector element type,
197   // so be sure to truncate it to the element type if necessary.
198   SDValue Op = N->getOperand(1);
199   EVT EltVT = N->getValueType(0).getVectorElementType();
200   if (Op.getValueType() != EltVT)
201     // FIXME: Can this happen for floating point types?
202     Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, Op);
203   return Op;
204 }
205
206 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
207   assert(N->isUnindexed() && "Indexed vector load?");
208
209   SDValue Result = DAG.getLoad(ISD::UNINDEXED,
210                                N->getExtensionType(),
211                                N->getValueType(0).getVectorElementType(),
212                                SDLoc(N),
213                                N->getChain(), N->getBasePtr(),
214                                DAG.getUNDEF(N->getBasePtr().getValueType()),
215                                N->getPointerInfo(),
216                                N->getMemoryVT().getVectorElementType(),
217                                N->isVolatile(), N->isNonTemporal(),
218                                N->isInvariant(), N->getOriginalAlignment());
219
220   // Legalized the chain result - switch anything that used the old chain to
221   // use the new one.
222   ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
223   return Result;
224 }
225
226 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
227   // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
228   EVT DestVT = N->getValueType(0).getVectorElementType();
229   SDValue Op = GetScalarizedVector(N->getOperand(0));
230   return DAG.getNode(N->getOpcode(), SDLoc(N), DestVT, Op);
231 }
232
233 SDValue DAGTypeLegalizer::ScalarizeVecRes_InregOp(SDNode *N) {
234   EVT EltVT = N->getValueType(0).getVectorElementType();
235   EVT ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT().getVectorElementType();
236   SDValue LHS = GetScalarizedVector(N->getOperand(0));
237   return DAG.getNode(N->getOpcode(), SDLoc(N), EltVT,
238                      LHS, DAG.getValueType(ExtVT));
239 }
240
241 SDValue DAGTypeLegalizer::ScalarizeVecRes_SCALAR_TO_VECTOR(SDNode *N) {
242   // If the operand is wider than the vector element type then it is implicitly
243   // truncated.  Make that explicit here.
244   EVT EltVT = N->getValueType(0).getVectorElementType();
245   SDValue InOp = N->getOperand(0);
246   if (InOp.getValueType() != EltVT)
247     return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
248   return InOp;
249 }
250
251 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSELECT(SDNode *N) {
252   SDValue Cond = GetScalarizedVector(N->getOperand(0));
253   SDValue LHS = GetScalarizedVector(N->getOperand(1));
254   TargetLowering::BooleanContent ScalarBool = TLI.getBooleanContents(false);
255   TargetLowering::BooleanContent VecBool = TLI.getBooleanContents(true);
256   if (ScalarBool != VecBool) {
257     EVT CondVT = Cond.getValueType();
258     switch (ScalarBool) {
259       case TargetLowering::UndefinedBooleanContent:
260         break;
261       case TargetLowering::ZeroOrOneBooleanContent:
262         assert(VecBool == TargetLowering::UndefinedBooleanContent ||
263                VecBool == TargetLowering::ZeroOrNegativeOneBooleanContent);
264         // Vector read from all ones, scalar expects a single 1 so mask.
265         Cond = DAG.getNode(ISD::AND, SDLoc(N), CondVT,
266                            Cond, DAG.getConstant(1, CondVT));
267         break;
268       case TargetLowering::ZeroOrNegativeOneBooleanContent:
269         assert(VecBool == TargetLowering::UndefinedBooleanContent ||
270                VecBool == TargetLowering::ZeroOrOneBooleanContent);
271         // Vector reads from a one, scalar from all ones so sign extend.
272         Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), CondVT,
273                            Cond, DAG.getValueType(MVT::i1));
274         break;
275     }
276   }
277
278   return DAG.getSelect(SDLoc(N),
279                        LHS.getValueType(), Cond, LHS,
280                        GetScalarizedVector(N->getOperand(2)));
281 }
282
283 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
284   SDValue LHS = GetScalarizedVector(N->getOperand(1));
285   return DAG.getSelect(SDLoc(N),
286                        LHS.getValueType(), N->getOperand(0), LHS,
287                        GetScalarizedVector(N->getOperand(2)));
288 }
289
290 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
291   SDValue LHS = GetScalarizedVector(N->getOperand(2));
292   return DAG.getNode(ISD::SELECT_CC, SDLoc(N), LHS.getValueType(),
293                      N->getOperand(0), N->getOperand(1),
294                      LHS, GetScalarizedVector(N->getOperand(3)),
295                      N->getOperand(4));
296 }
297
298 SDValue DAGTypeLegalizer::ScalarizeVecRes_SETCC(SDNode *N) {
299   assert(N->getValueType(0).isVector() ==
300          N->getOperand(0).getValueType().isVector() &&
301          "Scalar/Vector type mismatch");
302
303   if (N->getValueType(0).isVector()) return ScalarizeVecRes_VSETCC(N);
304
305   SDValue LHS = GetScalarizedVector(N->getOperand(0));
306   SDValue RHS = GetScalarizedVector(N->getOperand(1));
307   SDLoc DL(N);
308
309   // Turn it into a scalar SETCC.
310   return DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, N->getOperand(2));
311 }
312
313 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
314   return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
315 }
316
317 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
318   // Figure out if the scalar is the LHS or RHS and return it.
319   SDValue Arg = N->getOperand(2).getOperand(0);
320   if (Arg.getOpcode() == ISD::UNDEF)
321     return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
322   unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
323   return GetScalarizedVector(N->getOperand(Op));
324 }
325
326 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSETCC(SDNode *N) {
327   assert(N->getValueType(0).isVector() &&
328          N->getOperand(0).getValueType().isVector() &&
329          "Operand types must be vectors");
330
331   SDValue LHS = GetScalarizedVector(N->getOperand(0));
332   SDValue RHS = GetScalarizedVector(N->getOperand(1));
333   EVT NVT = N->getValueType(0).getVectorElementType();
334   SDLoc DL(N);
335
336   // Turn it into a scalar SETCC.
337   SDValue Res = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS,
338                             N->getOperand(2));
339   // Vectors may have a different boolean contents to scalars.  Promote the
340   // value appropriately.
341   ISD::NodeType ExtendCode =
342     TargetLowering::getExtendForContent(TLI.getBooleanContents(true));
343   return DAG.getNode(ExtendCode, DL, NVT, Res);
344 }
345
346
347 //===----------------------------------------------------------------------===//
348 //  Operand Vector Scalarization <1 x ty> -> ty.
349 //===----------------------------------------------------------------------===//
350
351 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
352   DEBUG(dbgs() << "Scalarize node operand " << OpNo << ": ";
353         N->dump(&DAG);
354         dbgs() << "\n");
355   SDValue Res = SDValue();
356
357   if (Res.getNode() == 0) {
358     switch (N->getOpcode()) {
359     default:
360 #ifndef NDEBUG
361       dbgs() << "ScalarizeVectorOperand Op #" << OpNo << ": ";
362       N->dump(&DAG);
363       dbgs() << "\n";
364 #endif
365       llvm_unreachable("Do not know how to scalarize this operator's operand!");
366     case ISD::BITCAST:
367       Res = ScalarizeVecOp_BITCAST(N);
368       break;
369     case ISD::ANY_EXTEND:
370     case ISD::ZERO_EXTEND:
371     case ISD::SIGN_EXTEND:
372       Res = ScalarizeVecOp_EXTEND(N);
373       break;
374     case ISD::CONCAT_VECTORS:
375       Res = ScalarizeVecOp_CONCAT_VECTORS(N);
376       break;
377     case ISD::EXTRACT_VECTOR_ELT:
378       Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N);
379       break;
380     case ISD::STORE:
381       Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo);
382       break;
383     }
384   }
385
386   // If the result is null, the sub-method took care of registering results etc.
387   if (!Res.getNode()) return false;
388
389   // If the result is N, the sub-method updated N in place.  Tell the legalizer
390   // core about this.
391   if (Res.getNode() == N)
392     return true;
393
394   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
395          "Invalid operand expansion");
396
397   ReplaceValueWith(SDValue(N, 0), Res);
398   return false;
399 }
400
401 /// ScalarizeVecOp_BITCAST - If the value to convert is a vector that needs
402 /// to be scalarized, it must be <1 x ty>.  Convert the element instead.
403 SDValue DAGTypeLegalizer::ScalarizeVecOp_BITCAST(SDNode *N) {
404   SDValue Elt = GetScalarizedVector(N->getOperand(0));
405   return DAG.getNode(ISD::BITCAST, SDLoc(N),
406                      N->getValueType(0), Elt);
407 }
408
409 /// ScalarizeVecOp_EXTEND - If the value to extend is a vector that needs
410 /// to be scalarized, it must be <1 x ty>.  Extend the element instead.
411 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTEND(SDNode *N) {
412   assert(N->getValueType(0).getVectorNumElements() == 1 &&
413          "Unexected vector type!");
414   SDValue Elt = GetScalarizedVector(N->getOperand(0));
415   SmallVector<SDValue, 1> Ops(1);
416   Ops[0] = DAG.getNode(N->getOpcode(), SDLoc(N),
417                        N->getValueType(0).getScalarType(), Elt);
418   // Revectorize the result so the types line up with what the uses of this
419   // expression expect.
420   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), N->getValueType(0),
421                      &Ops[0], 1);
422 }
423
424 /// ScalarizeVecOp_CONCAT_VECTORS - The vectors to concatenate have length one -
425 /// use a BUILD_VECTOR instead.
426 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
427   SmallVector<SDValue, 8> Ops(N->getNumOperands());
428   for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
429     Ops[i] = GetScalarizedVector(N->getOperand(i));
430   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), N->getValueType(0),
431                      &Ops[0], Ops.size());
432 }
433
434 /// ScalarizeVecOp_EXTRACT_VECTOR_ELT - If the input is a vector that needs to
435 /// be scalarized, it must be <1 x ty>, so just return the element, ignoring the
436 /// index.
437 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
438   SDValue Res = GetScalarizedVector(N->getOperand(0));
439   if (Res.getValueType() != N->getValueType(0))
440     Res = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0),
441                       Res);
442   return Res;
443 }
444
445 /// ScalarizeVecOp_STORE - If the value to store is a vector that needs to be
446 /// scalarized, it must be <1 x ty>.  Just store the element.
447 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
448   assert(N->isUnindexed() && "Indexed store of one-element vector?");
449   assert(OpNo == 1 && "Do not know how to scalarize this operand!");
450   SDLoc dl(N);
451
452   if (N->isTruncatingStore())
453     return DAG.getTruncStore(N->getChain(), dl,
454                              GetScalarizedVector(N->getOperand(1)),
455                              N->getBasePtr(), N->getPointerInfo(),
456                              N->getMemoryVT().getVectorElementType(),
457                              N->isVolatile(), N->isNonTemporal(),
458                              N->getAlignment());
459
460   return DAG.getStore(N->getChain(), dl, GetScalarizedVector(N->getOperand(1)),
461                       N->getBasePtr(), N->getPointerInfo(),
462                       N->isVolatile(), N->isNonTemporal(),
463                       N->getOriginalAlignment());
464 }
465
466
467 //===----------------------------------------------------------------------===//
468 //  Result Vector Splitting
469 //===----------------------------------------------------------------------===//
470
471 /// SplitVectorResult - This method is called when the specified result of the
472 /// specified node is found to need vector splitting.  At this point, the node
473 /// may also have invalid operands or may have other results that need
474 /// legalization, we just know that (at least) one result needs vector
475 /// splitting.
476 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
477   DEBUG(dbgs() << "Split node result: ";
478         N->dump(&DAG);
479         dbgs() << "\n");
480   SDValue Lo, Hi;
481
482   // See if the target wants to custom expand this node.
483   if (CustomLowerNode(N, N->getValueType(ResNo), true))
484     return;
485
486   switch (N->getOpcode()) {
487   default:
488 #ifndef NDEBUG
489     dbgs() << "SplitVectorResult #" << ResNo << ": ";
490     N->dump(&DAG);
491     dbgs() << "\n";
492 #endif
493     report_fatal_error("Do not know how to split the result of this "
494                        "operator!\n");
495
496   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
497   case ISD::VSELECT:
498   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
499   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
500   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
501   case ISD::BITCAST:           SplitVecRes_BITCAST(N, Lo, Hi); break;
502   case ISD::BUILD_VECTOR:      SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
503   case ISD::CONCAT_VECTORS:    SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
504   case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
505   case ISD::FP_ROUND_INREG:    SplitVecRes_InregOp(N, Lo, Hi); break;
506   case ISD::FPOWI:             SplitVecRes_FPOWI(N, Lo, Hi); break;
507   case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
508   case ISD::SCALAR_TO_VECTOR:  SplitVecRes_SCALAR_TO_VECTOR(N, Lo, Hi); break;
509   case ISD::SIGN_EXTEND_INREG: SplitVecRes_InregOp(N, Lo, Hi); break;
510   case ISD::LOAD:
511     SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);
512     break;
513   case ISD::SETCC:
514     SplitVecRes_SETCC(N, Lo, Hi);
515     break;
516   case ISD::VECTOR_SHUFFLE:
517     SplitVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N), Lo, Hi);
518     break;
519
520   case ISD::ANY_EXTEND:
521   case ISD::CONVERT_RNDSAT:
522   case ISD::CTLZ:
523   case ISD::CTTZ:
524   case ISD::CTLZ_ZERO_UNDEF:
525   case ISD::CTTZ_ZERO_UNDEF:
526   case ISD::CTPOP:
527   case ISD::FABS:
528   case ISD::FCEIL:
529   case ISD::FCOS:
530   case ISD::FEXP:
531   case ISD::FEXP2:
532   case ISD::FFLOOR:
533   case ISD::FLOG:
534   case ISD::FLOG10:
535   case ISD::FLOG2:
536   case ISD::FNEARBYINT:
537   case ISD::FNEG:
538   case ISD::FP_EXTEND:
539   case ISD::FP_ROUND:
540   case ISD::FP_TO_SINT:
541   case ISD::FP_TO_UINT:
542   case ISD::FRINT:
543   case ISD::FSIN:
544   case ISD::FSQRT:
545   case ISD::FTRUNC:
546   case ISD::SIGN_EXTEND:
547   case ISD::SINT_TO_FP:
548   case ISD::TRUNCATE:
549   case ISD::UINT_TO_FP:
550   case ISD::ZERO_EXTEND:
551     SplitVecRes_UnaryOp(N, Lo, Hi);
552     break;
553
554   case ISD::ADD:
555   case ISD::SUB:
556   case ISD::MUL:
557   case ISD::FADD:
558   case ISD::FSUB:
559   case ISD::FMUL:
560   case ISD::SDIV:
561   case ISD::UDIV:
562   case ISD::FDIV:
563   case ISD::FPOW:
564   case ISD::AND:
565   case ISD::OR:
566   case ISD::XOR:
567   case ISD::SHL:
568   case ISD::SRA:
569   case ISD::SRL:
570   case ISD::UREM:
571   case ISD::SREM:
572   case ISD::FREM:
573     SplitVecRes_BinOp(N, Lo, Hi);
574     break;
575   case ISD::FMA:
576     SplitVecRes_TernaryOp(N, Lo, Hi);
577     break;
578   }
579
580   // If Lo/Hi is null, the sub-method took care of registering results etc.
581   if (Lo.getNode())
582     SetSplitVector(SDValue(N, ResNo), Lo, Hi);
583 }
584
585 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
586                                          SDValue &Hi) {
587   SDValue LHSLo, LHSHi;
588   GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
589   SDValue RHSLo, RHSHi;
590   GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
591   SDLoc dl(N);
592
593   Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo, RHSLo);
594   Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi, RHSHi);
595 }
596
597 void DAGTypeLegalizer::SplitVecRes_TernaryOp(SDNode *N, SDValue &Lo,
598                                              SDValue &Hi) {
599   SDValue Op0Lo, Op0Hi;
600   GetSplitVector(N->getOperand(0), Op0Lo, Op0Hi);
601   SDValue Op1Lo, Op1Hi;
602   GetSplitVector(N->getOperand(1), Op1Lo, Op1Hi);
603   SDValue Op2Lo, Op2Hi;
604   GetSplitVector(N->getOperand(2), Op2Lo, Op2Hi);
605   SDLoc dl(N);
606
607   Lo = DAG.getNode(N->getOpcode(), dl, Op0Lo.getValueType(),
608                    Op0Lo, Op1Lo, Op2Lo);
609   Hi = DAG.getNode(N->getOpcode(), dl, Op0Hi.getValueType(),
610                    Op0Hi, Op1Hi, Op2Hi);
611 }
612
613 void DAGTypeLegalizer::SplitVecRes_BITCAST(SDNode *N, SDValue &Lo,
614                                            SDValue &Hi) {
615   // We know the result is a vector.  The input may be either a vector or a
616   // scalar value.
617   EVT LoVT, HiVT;
618   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
619   SDLoc dl(N);
620
621   SDValue InOp = N->getOperand(0);
622   EVT InVT = InOp.getValueType();
623
624   // Handle some special cases efficiently.
625   switch (getTypeAction(InVT)) {
626   case TargetLowering::TypeLegal:
627   case TargetLowering::TypePromoteInteger:
628   case TargetLowering::TypeSoftenFloat:
629   case TargetLowering::TypeScalarizeVector:
630   case TargetLowering::TypeWidenVector:
631     break;
632   case TargetLowering::TypeExpandInteger:
633   case TargetLowering::TypeExpandFloat:
634     // A scalar to vector conversion, where the scalar needs expansion.
635     // If the vector is being split in two then we can just convert the
636     // expanded pieces.
637     if (LoVT == HiVT) {
638       GetExpandedOp(InOp, Lo, Hi);
639       if (TLI.isBigEndian())
640         std::swap(Lo, Hi);
641       Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
642       Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
643       return;
644     }
645     break;
646   case TargetLowering::TypeSplitVector:
647     // If the input is a vector that needs to be split, convert each split
648     // piece of the input now.
649     GetSplitVector(InOp, Lo, Hi);
650     Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
651     Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
652     return;
653   }
654
655   // In the general case, convert the input to an integer and split it by hand.
656   EVT LoIntVT = EVT::getIntegerVT(*DAG.getContext(), LoVT.getSizeInBits());
657   EVT HiIntVT = EVT::getIntegerVT(*DAG.getContext(), HiVT.getSizeInBits());
658   if (TLI.isBigEndian())
659     std::swap(LoIntVT, HiIntVT);
660
661   SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
662
663   if (TLI.isBigEndian())
664     std::swap(Lo, Hi);
665   Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
666   Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
667 }
668
669 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
670                                                 SDValue &Hi) {
671   EVT LoVT, HiVT;
672   SDLoc dl(N);
673   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
674   unsigned LoNumElts = LoVT.getVectorNumElements();
675   SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
676   Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, LoVT, &LoOps[0], LoOps.size());
677
678   SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
679   Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, HiVT, &HiOps[0], HiOps.size());
680 }
681
682 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
683                                                   SDValue &Hi) {
684   assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
685   SDLoc dl(N);
686   unsigned NumSubvectors = N->getNumOperands() / 2;
687   if (NumSubvectors == 1) {
688     Lo = N->getOperand(0);
689     Hi = N->getOperand(1);
690     return;
691   }
692
693   EVT LoVT, HiVT;
694   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
695
696   SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
697   Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, LoVT, &LoOps[0], LoOps.size());
698
699   SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
700   Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HiVT, &HiOps[0], HiOps.size());
701 }
702
703 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
704                                                      SDValue &Hi) {
705   SDValue Vec = N->getOperand(0);
706   SDValue Idx = N->getOperand(1);
707   SDLoc dl(N);
708
709   EVT LoVT, HiVT;
710   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
711
712   Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, LoVT, Vec, Idx);
713   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
714   Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, HiVT, Vec,
715                    DAG.getIntPtrConstant(IdxVal + LoVT.getVectorNumElements()));
716 }
717
718 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
719                                          SDValue &Hi) {
720   SDLoc dl(N);
721   GetSplitVector(N->getOperand(0), Lo, Hi);
722   Lo = DAG.getNode(ISD::FPOWI, dl, Lo.getValueType(), Lo, N->getOperand(1));
723   Hi = DAG.getNode(ISD::FPOWI, dl, Hi.getValueType(), Hi, N->getOperand(1));
724 }
725
726 void DAGTypeLegalizer::SplitVecRes_InregOp(SDNode *N, SDValue &Lo,
727                                            SDValue &Hi) {
728   SDValue LHSLo, LHSHi;
729   GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
730   SDLoc dl(N);
731
732   EVT LoVT, HiVT;
733   GetSplitDestVTs(cast<VTSDNode>(N->getOperand(1))->getVT(), LoVT, HiVT);
734
735   Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo,
736                    DAG.getValueType(LoVT));
737   Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi,
738                    DAG.getValueType(HiVT));
739 }
740
741 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
742                                                      SDValue &Hi) {
743   SDValue Vec = N->getOperand(0);
744   SDValue Elt = N->getOperand(1);
745   SDValue Idx = N->getOperand(2);
746   SDLoc dl(N);
747   GetSplitVector(Vec, Lo, Hi);
748
749   if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
750     unsigned IdxVal = CIdx->getZExtValue();
751     unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
752     if (IdxVal < LoNumElts)
753       Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
754                        Lo.getValueType(), Lo, Elt, Idx);
755     else
756       Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, Hi.getValueType(), Hi, Elt,
757                        DAG.getIntPtrConstant(IdxVal - LoNumElts));
758     return;
759   }
760
761   // Spill the vector to the stack.
762   EVT VecVT = Vec.getValueType();
763   EVT EltVT = VecVT.getVectorElementType();
764   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
765   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
766                                MachinePointerInfo(), false, false, 0);
767
768   // Store the new element.  This may be larger than the vector element type,
769   // so use a truncating store.
770   SDValue EltPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
771   Type *VecType = VecVT.getTypeForEVT(*DAG.getContext());
772   unsigned Alignment =
773     TLI.getDataLayout()->getPrefTypeAlignment(VecType);
774   Store = DAG.getTruncStore(Store, dl, Elt, EltPtr, MachinePointerInfo(), EltVT,
775                             false, false, 0);
776
777   // Load the Lo part from the stack slot.
778   Lo = DAG.getLoad(Lo.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
779                    false, false, false, 0);
780
781   // Increment the pointer to the other part.
782   unsigned IncrementSize = Lo.getValueType().getSizeInBits() / 8;
783   StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
784                          DAG.getIntPtrConstant(IncrementSize));
785
786   // Load the Hi part from the stack slot.
787   Hi = DAG.getLoad(Hi.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
788                    false, false, false, MinAlign(Alignment, IncrementSize));
789 }
790
791 void DAGTypeLegalizer::SplitVecRes_SCALAR_TO_VECTOR(SDNode *N, SDValue &Lo,
792                                                     SDValue &Hi) {
793   EVT LoVT, HiVT;
794   SDLoc dl(N);
795   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
796   Lo = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoVT, N->getOperand(0));
797   Hi = DAG.getUNDEF(HiVT);
798 }
799
800 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
801                                         SDValue &Hi) {
802   assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
803   EVT LoVT, HiVT;
804   SDLoc dl(LD);
805   GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
806
807   ISD::LoadExtType ExtType = LD->getExtensionType();
808   SDValue Ch = LD->getChain();
809   SDValue Ptr = LD->getBasePtr();
810   SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
811   EVT MemoryVT = LD->getMemoryVT();
812   unsigned Alignment = LD->getOriginalAlignment();
813   bool isVolatile = LD->isVolatile();
814   bool isNonTemporal = LD->isNonTemporal();
815   bool isInvariant = LD->isInvariant();
816
817   EVT LoMemVT, HiMemVT;
818   GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
819
820   Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, dl, Ch, Ptr, Offset,
821                    LD->getPointerInfo(), LoMemVT, isVolatile, isNonTemporal,
822                    isInvariant, Alignment);
823
824   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
825   Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
826                     DAG.getIntPtrConstant(IncrementSize));
827   Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, dl, Ch, Ptr, Offset,
828                    LD->getPointerInfo().getWithOffset(IncrementSize),
829                    HiMemVT, isVolatile, isNonTemporal, isInvariant, Alignment);
830
831   // Build a factor node to remember that this load is independent of the
832   // other one.
833   Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
834                    Hi.getValue(1));
835
836   // Legalized the chain result - switch anything that used the old chain to
837   // use the new one.
838   ReplaceValueWith(SDValue(LD, 1), Ch);
839 }
840
841 void DAGTypeLegalizer::SplitVecRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
842   assert(N->getValueType(0).isVector() &&
843          N->getOperand(0).getValueType().isVector() &&
844          "Operand types must be vectors");
845
846   EVT LoVT, HiVT;
847   SDLoc DL(N);
848   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
849
850   // Split the input.
851   EVT InVT = N->getOperand(0).getValueType();
852   SDValue LL, LH, RL, RH;
853   EVT InNVT = EVT::getVectorVT(*DAG.getContext(), InVT.getVectorElementType(),
854                                LoVT.getVectorNumElements());
855   LL = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(0),
856                    DAG.getIntPtrConstant(0));
857   LH = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(0),
858                    DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
859
860   RL = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(1),
861                    DAG.getIntPtrConstant(0));
862   RH = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(1),
863                    DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
864
865   Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
866   Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
867 }
868
869 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
870                                            SDValue &Hi) {
871   // Get the dest types - they may not match the input types, e.g. int_to_fp.
872   EVT LoVT, HiVT;
873   SDLoc dl(N);
874   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
875
876   // If the input also splits, handle it directly for a compile time speedup.
877   // Otherwise split it by hand.
878   EVT InVT = N->getOperand(0).getValueType();
879   if (getTypeAction(InVT) == TargetLowering::TypeSplitVector) {
880     GetSplitVector(N->getOperand(0), Lo, Hi);
881   } else {
882     EVT InNVT = EVT::getVectorVT(*DAG.getContext(), InVT.getVectorElementType(),
883                                  LoVT.getVectorNumElements());
884     Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InNVT, N->getOperand(0),
885                      DAG.getIntPtrConstant(0));
886     Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InNVT, N->getOperand(0),
887                      DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
888   }
889
890   if (N->getOpcode() == ISD::FP_ROUND) {
891     Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo, N->getOperand(1));
892     Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi, N->getOperand(1));
893   } else if (N->getOpcode() == ISD::CONVERT_RNDSAT) {
894     SDValue DTyOpLo = DAG.getValueType(LoVT);
895     SDValue DTyOpHi = DAG.getValueType(HiVT);
896     SDValue STyOpLo = DAG.getValueType(Lo.getValueType());
897     SDValue STyOpHi = DAG.getValueType(Hi.getValueType());
898     SDValue RndOp = N->getOperand(3);
899     SDValue SatOp = N->getOperand(4);
900     ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
901     Lo = DAG.getConvertRndSat(LoVT, dl, Lo, DTyOpLo, STyOpLo, RndOp, SatOp,
902                               CvtCode);
903     Hi = DAG.getConvertRndSat(HiVT, dl, Hi, DTyOpHi, STyOpHi, RndOp, SatOp,
904                               CvtCode);
905   } else {
906     Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo);
907     Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi);
908   }
909 }
910
911 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
912                                                   SDValue &Lo, SDValue &Hi) {
913   // The low and high parts of the original input give four input vectors.
914   SDValue Inputs[4];
915   SDLoc dl(N);
916   GetSplitVector(N->getOperand(0), Inputs[0], Inputs[1]);
917   GetSplitVector(N->getOperand(1), Inputs[2], Inputs[3]);
918   EVT NewVT = Inputs[0].getValueType();
919   unsigned NewElts = NewVT.getVectorNumElements();
920
921   // If Lo or Hi uses elements from at most two of the four input vectors, then
922   // express it as a vector shuffle of those two inputs.  Otherwise extract the
923   // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR.
924   SmallVector<int, 16> Ops;
925   for (unsigned High = 0; High < 2; ++High) {
926     SDValue &Output = High ? Hi : Lo;
927
928     // Build a shuffle mask for the output, discovering on the fly which
929     // input vectors to use as shuffle operands (recorded in InputUsed).
930     // If building a suitable shuffle vector proves too hard, then bail
931     // out with useBuildVector set.
932     unsigned InputUsed[2] = { -1U, -1U }; // Not yet discovered.
933     unsigned FirstMaskIdx = High * NewElts;
934     bool useBuildVector = false;
935     for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
936       // The mask element.  This indexes into the input.
937       int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
938
939       // The input vector this mask element indexes into.
940       unsigned Input = (unsigned)Idx / NewElts;
941
942       if (Input >= array_lengthof(Inputs)) {
943         // The mask element does not index into any input vector.
944         Ops.push_back(-1);
945         continue;
946       }
947
948       // Turn the index into an offset from the start of the input vector.
949       Idx -= Input * NewElts;
950
951       // Find or create a shuffle vector operand to hold this input.
952       unsigned OpNo;
953       for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
954         if (InputUsed[OpNo] == Input) {
955           // This input vector is already an operand.
956           break;
957         } else if (InputUsed[OpNo] == -1U) {
958           // Create a new operand for this input vector.
959           InputUsed[OpNo] = Input;
960           break;
961         }
962       }
963
964       if (OpNo >= array_lengthof(InputUsed)) {
965         // More than two input vectors used!  Give up on trying to create a
966         // shuffle vector.  Insert all elements into a BUILD_VECTOR instead.
967         useBuildVector = true;
968         break;
969       }
970
971       // Add the mask index for the new shuffle vector.
972       Ops.push_back(Idx + OpNo * NewElts);
973     }
974
975     if (useBuildVector) {
976       EVT EltVT = NewVT.getVectorElementType();
977       SmallVector<SDValue, 16> SVOps;
978
979       // Extract the input elements by hand.
980       for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
981         // The mask element.  This indexes into the input.
982         int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
983
984         // The input vector this mask element indexes into.
985         unsigned Input = (unsigned)Idx / NewElts;
986
987         if (Input >= array_lengthof(Inputs)) {
988           // The mask element is "undef" or indexes off the end of the input.
989           SVOps.push_back(DAG.getUNDEF(EltVT));
990           continue;
991         }
992
993         // Turn the index into an offset from the start of the input vector.
994         Idx -= Input * NewElts;
995
996         // Extract the vector element by hand.
997         SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
998                                     Inputs[Input], DAG.getIntPtrConstant(Idx)));
999       }
1000
1001       // Construct the Lo/Hi output using a BUILD_VECTOR.
1002       Output = DAG.getNode(ISD::BUILD_VECTOR,dl,NewVT, &SVOps[0], SVOps.size());
1003     } else if (InputUsed[0] == -1U) {
1004       // No input vectors were used!  The result is undefined.
1005       Output = DAG.getUNDEF(NewVT);
1006     } else {
1007       SDValue Op0 = Inputs[InputUsed[0]];
1008       // If only one input was used, use an undefined vector for the other.
1009       SDValue Op1 = InputUsed[1] == -1U ?
1010         DAG.getUNDEF(NewVT) : Inputs[InputUsed[1]];
1011       // At least one input vector was used.  Create a new shuffle vector.
1012       Output =  DAG.getVectorShuffle(NewVT, dl, Op0, Op1, &Ops[0]);
1013     }
1014
1015     Ops.clear();
1016   }
1017 }
1018
1019
1020 //===----------------------------------------------------------------------===//
1021 //  Operand Vector Splitting
1022 //===----------------------------------------------------------------------===//
1023
1024 /// SplitVectorOperand - This method is called when the specified operand of the
1025 /// specified node is found to need vector splitting.  At this point, all of the
1026 /// result types of the node are known to be legal, but other operands of the
1027 /// node may need legalization as well as the specified one.
1028 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
1029   DEBUG(dbgs() << "Split node operand: ";
1030         N->dump(&DAG);
1031         dbgs() << "\n");
1032   SDValue Res = SDValue();
1033
1034   // See if the target wants to custom split this node.
1035   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1036     return false;
1037
1038   if (Res.getNode() == 0) {
1039     switch (N->getOpcode()) {
1040     default:
1041 #ifndef NDEBUG
1042       dbgs() << "SplitVectorOperand Op #" << OpNo << ": ";
1043       N->dump(&DAG);
1044       dbgs() << "\n";
1045 #endif
1046       report_fatal_error("Do not know how to split this operator's "
1047                          "operand!\n");
1048
1049     case ISD::SETCC:             Res = SplitVecOp_VSETCC(N); break;
1050     case ISD::BITCAST:           Res = SplitVecOp_BITCAST(N); break;
1051     case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
1052     case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
1053     case ISD::CONCAT_VECTORS:    Res = SplitVecOp_CONCAT_VECTORS(N); break;
1054     case ISD::TRUNCATE:          Res = SplitVecOp_TRUNCATE(N); break;
1055     case ISD::FP_ROUND:          Res = SplitVecOp_FP_ROUND(N); break;
1056     case ISD::STORE:
1057       Res = SplitVecOp_STORE(cast<StoreSDNode>(N), OpNo);
1058       break;
1059     case ISD::VSELECT:
1060       Res = SplitVecOp_VSELECT(N, OpNo);
1061       break;
1062     case ISD::CTTZ:
1063     case ISD::CTLZ:
1064     case ISD::CTPOP:
1065     case ISD::FP_EXTEND:
1066     case ISD::FP_TO_SINT:
1067     case ISD::FP_TO_UINT:
1068     case ISD::SINT_TO_FP:
1069     case ISD::UINT_TO_FP:
1070     case ISD::FTRUNC:
1071     case ISD::SIGN_EXTEND:
1072     case ISD::ZERO_EXTEND:
1073     case ISD::ANY_EXTEND:
1074       Res = SplitVecOp_UnaryOp(N);
1075       break;
1076     }
1077   }
1078
1079   // If the result is null, the sub-method took care of registering results etc.
1080   if (!Res.getNode()) return false;
1081
1082   // If the result is N, the sub-method updated N in place.  Tell the legalizer
1083   // core about this.
1084   if (Res.getNode() == N)
1085     return true;
1086
1087   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1088          "Invalid operand expansion");
1089
1090   ReplaceValueWith(SDValue(N, 0), Res);
1091   return false;
1092 }
1093
1094 SDValue DAGTypeLegalizer::SplitVecOp_VSELECT(SDNode *N, unsigned OpNo) {
1095   // The only possibility for an illegal operand is the mask, since result type
1096   // legalization would have handled this node already otherwise.
1097   assert(OpNo == 0 && "Illegal operand must be mask");
1098
1099   SDValue Mask = N->getOperand(0);
1100   SDValue Src0 = N->getOperand(1);
1101   SDValue Src1 = N->getOperand(2);
1102   SDLoc DL(N);
1103   EVT MaskVT = Mask.getValueType();
1104   assert(MaskVT.isVector() && "VSELECT without a vector mask?");
1105
1106   SDValue Lo, Hi;
1107   GetSplitVector(N->getOperand(0), Lo, Hi);
1108   assert(Lo.getValueType() == Hi.getValueType() &&
1109          "Lo and Hi have differing types");
1110
1111   unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
1112   unsigned HiNumElts = Hi.getValueType().getVectorNumElements();
1113   assert(LoNumElts == HiNumElts && "Asymmetric vector split?");
1114
1115   LLVMContext &Ctx = *DAG.getContext();
1116   SDValue Zero = DAG.getIntPtrConstant(0);
1117   SDValue LoElts = DAG.getIntPtrConstant(LoNumElts);
1118   EVT Src0VT = Src0.getValueType();
1119   EVT Src0EltTy = Src0VT.getVectorElementType();
1120   EVT MaskEltTy = MaskVT.getVectorElementType();
1121
1122   EVT LoOpVT = EVT::getVectorVT(Ctx, Src0EltTy, LoNumElts);
1123   EVT LoMaskVT = EVT::getVectorVT(Ctx, MaskEltTy, LoNumElts);
1124   EVT HiOpVT = EVT::getVectorVT(Ctx, Src0EltTy, HiNumElts);
1125   EVT HiMaskVT = EVT::getVectorVT(Ctx, MaskEltTy, HiNumElts);
1126
1127   SDValue LoOp0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, LoOpVT, Src0, Zero);
1128   SDValue LoOp1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, LoOpVT, Src1, Zero);
1129
1130   SDValue HiOp0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HiOpVT, Src0, LoElts);
1131   SDValue HiOp1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HiOpVT, Src1, LoElts);
1132
1133   SDValue LoMask =
1134     DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, LoMaskVT, Mask, Zero);
1135   SDValue HiMask =
1136     DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HiMaskVT, Mask, LoElts);
1137
1138   SDValue LoSelect =
1139     DAG.getNode(ISD::VSELECT, DL, LoOpVT, LoMask, LoOp0, LoOp1);
1140   SDValue HiSelect =
1141     DAG.getNode(ISD::VSELECT, DL, HiOpVT, HiMask, HiOp0, HiOp1);
1142
1143   return DAG.getNode(ISD::CONCAT_VECTORS, DL, Src0VT, LoSelect, HiSelect);
1144 }
1145
1146 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
1147   // The result has a legal vector type, but the input needs splitting.
1148   EVT ResVT = N->getValueType(0);
1149   SDValue Lo, Hi;
1150   SDLoc dl(N);
1151   GetSplitVector(N->getOperand(0), Lo, Hi);
1152   EVT InVT = Lo.getValueType();
1153
1154   EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
1155                                InVT.getVectorNumElements());
1156
1157   Lo = DAG.getNode(N->getOpcode(), dl, OutVT, Lo);
1158   Hi = DAG.getNode(N->getOpcode(), dl, OutVT, Hi);
1159
1160   return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
1161 }
1162
1163 SDValue DAGTypeLegalizer::SplitVecOp_BITCAST(SDNode *N) {
1164   // For example, i64 = BITCAST v4i16 on alpha.  Typically the vector will
1165   // end up being split all the way down to individual components.  Convert the
1166   // split pieces into integers and reassemble.
1167   SDValue Lo, Hi;
1168   GetSplitVector(N->getOperand(0), Lo, Hi);
1169   Lo = BitConvertToInteger(Lo);
1170   Hi = BitConvertToInteger(Hi);
1171
1172   if (TLI.isBigEndian())
1173     std::swap(Lo, Hi);
1174
1175   return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
1176                      JoinIntegers(Lo, Hi));
1177 }
1178
1179 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
1180   // We know that the extracted result type is legal.
1181   EVT SubVT = N->getValueType(0);
1182   SDValue Idx = N->getOperand(1);
1183   SDLoc dl(N);
1184   SDValue Lo, Hi;
1185   GetSplitVector(N->getOperand(0), Lo, Hi);
1186
1187   uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1188   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1189
1190   if (IdxVal < LoElts) {
1191     assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
1192            "Extracted subvector crosses vector split!");
1193     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Lo, Idx);
1194   } else {
1195     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Hi,
1196                        DAG.getConstant(IdxVal - LoElts, Idx.getValueType()));
1197   }
1198 }
1199
1200 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
1201   SDValue Vec = N->getOperand(0);
1202   SDValue Idx = N->getOperand(1);
1203   EVT VecVT = Vec.getValueType();
1204
1205   if (isa<ConstantSDNode>(Idx)) {
1206     uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1207     assert(IdxVal < VecVT.getVectorNumElements() && "Invalid vector index!");
1208
1209     SDValue Lo, Hi;
1210     GetSplitVector(Vec, Lo, Hi);
1211
1212     uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1213
1214     if (IdxVal < LoElts)
1215       return SDValue(DAG.UpdateNodeOperands(N, Lo, Idx), 0);
1216     return SDValue(DAG.UpdateNodeOperands(N, Hi,
1217                                   DAG.getConstant(IdxVal - LoElts,
1218                                                   Idx.getValueType())), 0);
1219   }
1220
1221   // Store the vector to the stack.
1222   EVT EltVT = VecVT.getVectorElementType();
1223   SDLoc dl(N);
1224   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
1225   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1226                                MachinePointerInfo(), false, false, 0);
1227
1228   // Load back the required element.
1229   StackPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
1230   return DAG.getExtLoad(ISD::EXTLOAD, dl, N->getValueType(0), Store, StackPtr,
1231                         MachinePointerInfo(), EltVT, false, false, 0);
1232 }
1233
1234 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
1235   assert(N->isUnindexed() && "Indexed store of vector?");
1236   assert(OpNo == 1 && "Can only split the stored value");
1237   SDLoc DL(N);
1238
1239   bool isTruncating = N->isTruncatingStore();
1240   SDValue Ch  = N->getChain();
1241   SDValue Ptr = N->getBasePtr();
1242   EVT MemoryVT = N->getMemoryVT();
1243   unsigned Alignment = N->getOriginalAlignment();
1244   bool isVol = N->isVolatile();
1245   bool isNT = N->isNonTemporal();
1246   SDValue Lo, Hi;
1247   GetSplitVector(N->getOperand(1), Lo, Hi);
1248
1249   EVT LoMemVT, HiMemVT;
1250   GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
1251
1252   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
1253
1254   if (isTruncating)
1255     Lo = DAG.getTruncStore(Ch, DL, Lo, Ptr, N->getPointerInfo(),
1256                            LoMemVT, isVol, isNT, Alignment);
1257   else
1258     Lo = DAG.getStore(Ch, DL, Lo, Ptr, N->getPointerInfo(),
1259                       isVol, isNT, Alignment);
1260
1261   // Increment the pointer to the other half.
1262   Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1263                     DAG.getIntPtrConstant(IncrementSize));
1264
1265   if (isTruncating)
1266     Hi = DAG.getTruncStore(Ch, DL, Hi, Ptr,
1267                            N->getPointerInfo().getWithOffset(IncrementSize),
1268                            HiMemVT, isVol, isNT, Alignment);
1269   else
1270     Hi = DAG.getStore(Ch, DL, Hi, Ptr,
1271                       N->getPointerInfo().getWithOffset(IncrementSize),
1272                       isVol, isNT, Alignment);
1273
1274   return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
1275 }
1276
1277 SDValue DAGTypeLegalizer::SplitVecOp_CONCAT_VECTORS(SDNode *N) {
1278   SDLoc DL(N);
1279
1280   // The input operands all must have the same type, and we know the result
1281   // type is valid.  Convert this to a buildvector which extracts all the
1282   // input elements.
1283   // TODO: If the input elements are power-two vectors, we could convert this to
1284   // a new CONCAT_VECTORS node with elements that are half-wide.
1285   SmallVector<SDValue, 32> Elts;
1286   EVT EltVT = N->getValueType(0).getVectorElementType();
1287   for (unsigned op = 0, e = N->getNumOperands(); op != e; ++op) {
1288     SDValue Op = N->getOperand(op);
1289     for (unsigned i = 0, e = Op.getValueType().getVectorNumElements();
1290          i != e; ++i) {
1291       Elts.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT,
1292                                  Op, DAG.getIntPtrConstant(i)));
1293
1294     }
1295   }
1296
1297   return DAG.getNode(ISD::BUILD_VECTOR, DL, N->getValueType(0),
1298                      &Elts[0], Elts.size());
1299 }
1300
1301 SDValue DAGTypeLegalizer::SplitVecOp_TRUNCATE(SDNode *N) {
1302   // The result type is legal, but the input type is illegal.  If splitting
1303   // ends up with the result type of each half still being legal, just
1304   // do that.  If, however, that would result in an illegal result type,
1305   // we can try to get more clever with power-two vectors. Specifically,
1306   // split the input type, but also widen the result element size, then
1307   // concatenate the halves and truncate again.  For example, consider a target
1308   // where v8i8 is legal and v8i32 is not (ARM, which doesn't have 256-bit
1309   // vectors). To perform a "%res = v8i8 trunc v8i32 %in" we do:
1310   //   %inlo = v4i32 extract_subvector %in, 0
1311   //   %inhi = v4i32 extract_subvector %in, 4
1312   //   %lo16 = v4i16 trunc v4i32 %inlo
1313   //   %hi16 = v4i16 trunc v4i32 %inhi
1314   //   %in16 = v8i16 concat_vectors v4i16 %lo16, v4i16 %hi16
1315   //   %res = v8i8 trunc v8i16 %in16
1316   //
1317   // Without this transform, the original truncate would end up being
1318   // scalarized, which is pretty much always a last resort.
1319   SDValue InVec = N->getOperand(0);
1320   EVT InVT = InVec->getValueType(0);
1321   EVT OutVT = N->getValueType(0);
1322   unsigned NumElements = OutVT.getVectorNumElements();
1323   // Widening should have already made sure this is a power-two vector
1324   // if we're trying to split it at all. assert() that's true, just in case.
1325   assert(!(NumElements & 1) && "Splitting vector, but not in half!");
1326
1327   unsigned InElementSize = InVT.getVectorElementType().getSizeInBits();
1328   unsigned OutElementSize = OutVT.getVectorElementType().getSizeInBits();
1329
1330   // If the input elements are only 1/2 the width of the result elements,
1331   // just use the normal splitting. Our trick only work if there's room
1332   // to split more than once.
1333   if (InElementSize <= OutElementSize * 2)
1334     return SplitVecOp_UnaryOp(N);
1335   SDLoc DL(N);
1336
1337   // Extract the halves of the input via extract_subvector.
1338   EVT SplitVT = EVT::getVectorVT(*DAG.getContext(),
1339                                  InVT.getVectorElementType(), NumElements/2);
1340   SDValue InLoVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SplitVT, InVec,
1341                                 DAG.getIntPtrConstant(0));
1342   SDValue InHiVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SplitVT, InVec,
1343                                 DAG.getIntPtrConstant(NumElements/2));
1344   // Truncate them to 1/2 the element size.
1345   EVT HalfElementVT = EVT::getIntegerVT(*DAG.getContext(), InElementSize/2);
1346   EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT,
1347                                 NumElements/2);
1348   SDValue HalfLo = DAG.getNode(ISD::TRUNCATE, DL, HalfVT, InLoVec);
1349   SDValue HalfHi = DAG.getNode(ISD::TRUNCATE, DL, HalfVT, InHiVec);
1350   // Concatenate them to get the full intermediate truncation result.
1351   EVT InterVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT, NumElements);
1352   SDValue InterVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InterVT, HalfLo,
1353                                  HalfHi);
1354   // Now finish up by truncating all the way down to the original result
1355   // type. This should normally be something that ends up being legal directly,
1356   // but in theory if a target has very wide vectors and an annoyingly
1357   // restricted set of legal types, this split can chain to build things up.
1358   return DAG.getNode(ISD::TRUNCATE, DL, OutVT, InterVec);
1359 }
1360
1361 SDValue DAGTypeLegalizer::SplitVecOp_VSETCC(SDNode *N) {
1362   assert(N->getValueType(0).isVector() &&
1363          N->getOperand(0).getValueType().isVector() &&
1364          "Operand types must be vectors");
1365   // The result has a legal vector type, but the input needs splitting.
1366   SDValue Lo0, Hi0, Lo1, Hi1, LoRes, HiRes;
1367   SDLoc DL(N);
1368   GetSplitVector(N->getOperand(0), Lo0, Hi0);
1369   GetSplitVector(N->getOperand(1), Lo1, Hi1);
1370   unsigned PartElements = Lo0.getValueType().getVectorNumElements();
1371   EVT PartResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, PartElements);
1372   EVT WideResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, 2*PartElements);
1373
1374   LoRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Lo0, Lo1, N->getOperand(2));
1375   HiRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Hi0, Hi1, N->getOperand(2));
1376   SDValue Con = DAG.getNode(ISD::CONCAT_VECTORS, DL, WideResVT, LoRes, HiRes);
1377   return PromoteTargetBoolean(Con, N->getValueType(0));
1378 }
1379
1380
1381 SDValue DAGTypeLegalizer::SplitVecOp_FP_ROUND(SDNode *N) {
1382   // The result has a legal vector type, but the input needs splitting.
1383   EVT ResVT = N->getValueType(0);
1384   SDValue Lo, Hi;
1385   SDLoc DL(N);
1386   GetSplitVector(N->getOperand(0), Lo, Hi);
1387   EVT InVT = Lo.getValueType();
1388
1389   EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
1390                                InVT.getVectorNumElements());
1391
1392   Lo = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Lo, N->getOperand(1));
1393   Hi = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Hi, N->getOperand(1));
1394
1395   return DAG.getNode(ISD::CONCAT_VECTORS, DL, ResVT, Lo, Hi);
1396 }
1397
1398
1399
1400 //===----------------------------------------------------------------------===//
1401 //  Result Vector Widening
1402 //===----------------------------------------------------------------------===//
1403
1404 void DAGTypeLegalizer::WidenVectorResult(SDNode *N, unsigned ResNo) {
1405   DEBUG(dbgs() << "Widen node result " << ResNo << ": ";
1406         N->dump(&DAG);
1407         dbgs() << "\n");
1408
1409   // See if the target wants to custom widen this node.
1410   if (CustomWidenLowerNode(N, N->getValueType(ResNo)))
1411     return;
1412
1413   SDValue Res = SDValue();
1414   switch (N->getOpcode()) {
1415   default:
1416 #ifndef NDEBUG
1417     dbgs() << "WidenVectorResult #" << ResNo << ": ";
1418     N->dump(&DAG);
1419     dbgs() << "\n";
1420 #endif
1421     llvm_unreachable("Do not know how to widen the result of this operator!");
1422
1423   case ISD::MERGE_VALUES:      Res = WidenVecRes_MERGE_VALUES(N, ResNo); break;
1424   case ISD::BITCAST:           Res = WidenVecRes_BITCAST(N); break;
1425   case ISD::BUILD_VECTOR:      Res = WidenVecRes_BUILD_VECTOR(N); break;
1426   case ISD::CONCAT_VECTORS:    Res = WidenVecRes_CONCAT_VECTORS(N); break;
1427   case ISD::CONVERT_RNDSAT:    Res = WidenVecRes_CONVERT_RNDSAT(N); break;
1428   case ISD::EXTRACT_SUBVECTOR: Res = WidenVecRes_EXTRACT_SUBVECTOR(N); break;
1429   case ISD::FP_ROUND_INREG:    Res = WidenVecRes_InregOp(N); break;
1430   case ISD::INSERT_VECTOR_ELT: Res = WidenVecRes_INSERT_VECTOR_ELT(N); break;
1431   case ISD::LOAD:              Res = WidenVecRes_LOAD(N); break;
1432   case ISD::SCALAR_TO_VECTOR:  Res = WidenVecRes_SCALAR_TO_VECTOR(N); break;
1433   case ISD::SIGN_EXTEND_INREG: Res = WidenVecRes_InregOp(N); break;
1434   case ISD::VSELECT:
1435   case ISD::SELECT:            Res = WidenVecRes_SELECT(N); break;
1436   case ISD::SELECT_CC:         Res = WidenVecRes_SELECT_CC(N); break;
1437   case ISD::SETCC:             Res = WidenVecRes_SETCC(N); break;
1438   case ISD::UNDEF:             Res = WidenVecRes_UNDEF(N); break;
1439   case ISD::VECTOR_SHUFFLE:
1440     Res = WidenVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N));
1441     break;
1442   case ISD::ADD:
1443   case ISD::AND:
1444   case ISD::BSWAP:
1445   case ISD::FADD:
1446   case ISD::FCOPYSIGN:
1447   case ISD::FDIV:
1448   case ISD::FMUL:
1449   case ISD::FPOW:
1450   case ISD::FREM:
1451   case ISD::FSUB:
1452   case ISD::MUL:
1453   case ISD::MULHS:
1454   case ISD::MULHU:
1455   case ISD::OR:
1456   case ISD::SDIV:
1457   case ISD::SREM:
1458   case ISD::UDIV:
1459   case ISD::UREM:
1460   case ISD::SUB:
1461   case ISD::XOR:
1462     Res = WidenVecRes_Binary(N);
1463     break;
1464
1465   case ISD::FPOWI:
1466     Res = WidenVecRes_POWI(N);
1467     break;
1468
1469   case ISD::SHL:
1470   case ISD::SRA:
1471   case ISD::SRL:
1472     Res = WidenVecRes_Shift(N);
1473     break;
1474
1475   case ISD::ANY_EXTEND:
1476   case ISD::FP_EXTEND:
1477   case ISD::FP_ROUND:
1478   case ISD::FP_TO_SINT:
1479   case ISD::FP_TO_UINT:
1480   case ISD::SIGN_EXTEND:
1481   case ISD::SINT_TO_FP:
1482   case ISD::TRUNCATE:
1483   case ISD::UINT_TO_FP:
1484   case ISD::ZERO_EXTEND:
1485     Res = WidenVecRes_Convert(N);
1486     break;
1487
1488   case ISD::CTLZ:
1489   case ISD::CTPOP:
1490   case ISD::CTTZ:
1491   case ISD::FABS:
1492   case ISD::FCEIL:
1493   case ISD::FCOS:
1494   case ISD::FEXP:
1495   case ISD::FEXP2:
1496   case ISD::FFLOOR:
1497   case ISD::FLOG:
1498   case ISD::FLOG10:
1499   case ISD::FLOG2:
1500   case ISD::FNEARBYINT:
1501   case ISD::FNEG:
1502   case ISD::FRINT:
1503   case ISD::FSIN:
1504   case ISD::FSQRT:
1505   case ISD::FTRUNC:
1506     Res = WidenVecRes_Unary(N);
1507     break;
1508   case ISD::FMA:
1509     Res = WidenVecRes_Ternary(N);
1510     break;
1511   }
1512
1513   // If Res is null, the sub-method took care of registering the result.
1514   if (Res.getNode())
1515     SetWidenedVector(SDValue(N, ResNo), Res);
1516 }
1517
1518 SDValue DAGTypeLegalizer::WidenVecRes_Ternary(SDNode *N) {
1519   // Ternary op widening.
1520   SDLoc dl(N);
1521   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1522   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1523   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1524   SDValue InOp3 = GetWidenedVector(N->getOperand(2));
1525   return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, InOp3);
1526 }
1527
1528 SDValue DAGTypeLegalizer::WidenVecRes_Binary(SDNode *N) {
1529   // Binary op widening.
1530   unsigned Opcode = N->getOpcode();
1531   SDLoc dl(N);
1532   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1533   EVT WidenEltVT = WidenVT.getVectorElementType();
1534   EVT VT = WidenVT;
1535   unsigned NumElts =  VT.getVectorNumElements();
1536   while (!TLI.isTypeLegal(VT) && NumElts != 1) {
1537     NumElts = NumElts / 2;
1538     VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
1539   }
1540
1541   if (NumElts != 1 && !TLI.canOpTrap(N->getOpcode(), VT)) {
1542     // Operation doesn't trap so just widen as normal.
1543     SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1544     SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1545     return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2);
1546   }
1547
1548   // No legal vector version so unroll the vector operation and then widen.
1549   if (NumElts == 1)
1550     return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
1551
1552   // Since the operation can trap, apply operation on the original vector.
1553   EVT MaxVT = VT;
1554   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1555   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1556   unsigned CurNumElts = N->getValueType(0).getVectorNumElements();
1557
1558   SmallVector<SDValue, 16> ConcatOps(CurNumElts);
1559   unsigned ConcatEnd = 0;  // Current ConcatOps index.
1560   int Idx = 0;        // Current Idx into input vectors.
1561
1562   // NumElts := greatest legal vector size (at most WidenVT)
1563   // while (orig. vector has unhandled elements) {
1564   //   take munches of size NumElts from the beginning and add to ConcatOps
1565   //   NumElts := next smaller supported vector size or 1
1566   // }
1567   while (CurNumElts != 0) {
1568     while (CurNumElts >= NumElts) {
1569       SDValue EOp1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, InOp1,
1570                                  DAG.getIntPtrConstant(Idx));
1571       SDValue EOp2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, InOp2,
1572                                  DAG.getIntPtrConstant(Idx));
1573       ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, VT, EOp1, EOp2);
1574       Idx += NumElts;
1575       CurNumElts -= NumElts;
1576     }
1577     do {
1578       NumElts = NumElts / 2;
1579       VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
1580     } while (!TLI.isTypeLegal(VT) && NumElts != 1);
1581
1582     if (NumElts == 1) {
1583       for (unsigned i = 0; i != CurNumElts; ++i, ++Idx) {
1584         SDValue EOp1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT,
1585                                    InOp1, DAG.getIntPtrConstant(Idx));
1586         SDValue EOp2 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT,
1587                                    InOp2, DAG.getIntPtrConstant(Idx));
1588         ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, WidenEltVT,
1589                                              EOp1, EOp2);
1590       }
1591       CurNumElts = 0;
1592     }
1593   }
1594
1595   // Check to see if we have a single operation with the widen type.
1596   if (ConcatEnd == 1) {
1597     VT = ConcatOps[0].getValueType();
1598     if (VT == WidenVT)
1599       return ConcatOps[0];
1600   }
1601
1602   // while (Some element of ConcatOps is not of type MaxVT) {
1603   //   From the end of ConcatOps, collect elements of the same type and put
1604   //   them into an op of the next larger supported type
1605   // }
1606   while (ConcatOps[ConcatEnd-1].getValueType() != MaxVT) {
1607     Idx = ConcatEnd - 1;
1608     VT = ConcatOps[Idx--].getValueType();
1609     while (Idx >= 0 && ConcatOps[Idx].getValueType() == VT)
1610       Idx--;
1611
1612     int NextSize = VT.isVector() ? VT.getVectorNumElements() : 1;
1613     EVT NextVT;
1614     do {
1615       NextSize *= 2;
1616       NextVT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NextSize);
1617     } while (!TLI.isTypeLegal(NextVT));
1618
1619     if (!VT.isVector()) {
1620       // Scalar type, create an INSERT_VECTOR_ELEMENT of type NextVT
1621       SDValue VecOp = DAG.getUNDEF(NextVT);
1622       unsigned NumToInsert = ConcatEnd - Idx - 1;
1623       for (unsigned i = 0, OpIdx = Idx+1; i < NumToInsert; i++, OpIdx++) {
1624         VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NextVT, VecOp,
1625                             ConcatOps[OpIdx], DAG.getIntPtrConstant(i));
1626       }
1627       ConcatOps[Idx+1] = VecOp;
1628       ConcatEnd = Idx + 2;
1629     } else {
1630       // Vector type, create a CONCAT_VECTORS of type NextVT
1631       SDValue undefVec = DAG.getUNDEF(VT);
1632       unsigned OpsToConcat = NextSize/VT.getVectorNumElements();
1633       SmallVector<SDValue, 16> SubConcatOps(OpsToConcat);
1634       unsigned RealVals = ConcatEnd - Idx - 1;
1635       unsigned SubConcatEnd = 0;
1636       unsigned SubConcatIdx = Idx + 1;
1637       while (SubConcatEnd < RealVals)
1638         SubConcatOps[SubConcatEnd++] = ConcatOps[++Idx];
1639       while (SubConcatEnd < OpsToConcat)
1640         SubConcatOps[SubConcatEnd++] = undefVec;
1641       ConcatOps[SubConcatIdx] = DAG.getNode(ISD::CONCAT_VECTORS, dl,
1642                                             NextVT, &SubConcatOps[0],
1643                                             OpsToConcat);
1644       ConcatEnd = SubConcatIdx + 1;
1645     }
1646   }
1647
1648   // Check to see if we have a single operation with the widen type.
1649   if (ConcatEnd == 1) {
1650     VT = ConcatOps[0].getValueType();
1651     if (VT == WidenVT)
1652       return ConcatOps[0];
1653   }
1654
1655   // add undefs of size MaxVT until ConcatOps grows to length of WidenVT
1656   unsigned NumOps = WidenVT.getVectorNumElements()/MaxVT.getVectorNumElements();
1657   if (NumOps != ConcatEnd ) {
1658     SDValue UndefVal = DAG.getUNDEF(MaxVT);
1659     for (unsigned j = ConcatEnd; j < NumOps; ++j)
1660       ConcatOps[j] = UndefVal;
1661   }
1662   return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &ConcatOps[0], NumOps);
1663 }
1664
1665 SDValue DAGTypeLegalizer::WidenVecRes_Convert(SDNode *N) {
1666   SDValue InOp = N->getOperand(0);
1667   SDLoc DL(N);
1668
1669   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1670   unsigned WidenNumElts = WidenVT.getVectorNumElements();
1671
1672   EVT InVT = InOp.getValueType();
1673   EVT InEltVT = InVT.getVectorElementType();
1674   EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
1675
1676   unsigned Opcode = N->getOpcode();
1677   unsigned InVTNumElts = InVT.getVectorNumElements();
1678
1679   if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
1680     InOp = GetWidenedVector(N->getOperand(0));
1681     InVT = InOp.getValueType();
1682     InVTNumElts = InVT.getVectorNumElements();
1683     if (InVTNumElts == WidenNumElts) {
1684       if (N->getNumOperands() == 1)
1685         return DAG.getNode(Opcode, DL, WidenVT, InOp);
1686       return DAG.getNode(Opcode, DL, WidenVT, InOp, N->getOperand(1));
1687     }
1688   }
1689
1690   if (TLI.isTypeLegal(InWidenVT)) {
1691     // Because the result and the input are different vector types, widening
1692     // the result could create a legal type but widening the input might make
1693     // it an illegal type that might lead to repeatedly splitting the input
1694     // and then widening it. To avoid this, we widen the input only if
1695     // it results in a legal type.
1696     if (WidenNumElts % InVTNumElts == 0) {
1697       // Widen the input and call convert on the widened input vector.
1698       unsigned NumConcat = WidenNumElts/InVTNumElts;
1699       SmallVector<SDValue, 16> Ops(NumConcat);
1700       Ops[0] = InOp;
1701       SDValue UndefVal = DAG.getUNDEF(InVT);
1702       for (unsigned i = 1; i != NumConcat; ++i)
1703         Ops[i] = UndefVal;
1704       SDValue InVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InWidenVT,
1705                                   &Ops[0], NumConcat);
1706       if (N->getNumOperands() == 1)
1707         return DAG.getNode(Opcode, DL, WidenVT, InVec);
1708       return DAG.getNode(Opcode, DL, WidenVT, InVec, N->getOperand(1));
1709     }
1710
1711     if (InVTNumElts % WidenNumElts == 0) {
1712       SDValue InVal = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InWidenVT,
1713                                   InOp, DAG.getIntPtrConstant(0));
1714       // Extract the input and convert the shorten input vector.
1715       if (N->getNumOperands() == 1)
1716         return DAG.getNode(Opcode, DL, WidenVT, InVal);
1717       return DAG.getNode(Opcode, DL, WidenVT, InVal, N->getOperand(1));
1718     }
1719   }
1720
1721   // Otherwise unroll into some nasty scalar code and rebuild the vector.
1722   SmallVector<SDValue, 16> Ops(WidenNumElts);
1723   EVT EltVT = WidenVT.getVectorElementType();
1724   unsigned MinElts = std::min(InVTNumElts, WidenNumElts);
1725   unsigned i;
1726   for (i=0; i < MinElts; ++i) {
1727     SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, InEltVT, InOp,
1728                               DAG.getIntPtrConstant(i));
1729     if (N->getNumOperands() == 1)
1730       Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val);
1731     else
1732       Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val, N->getOperand(1));
1733   }
1734
1735   SDValue UndefVal = DAG.getUNDEF(EltVT);
1736   for (; i < WidenNumElts; ++i)
1737     Ops[i] = UndefVal;
1738
1739   return DAG.getNode(ISD::BUILD_VECTOR, DL, WidenVT, &Ops[0], WidenNumElts);
1740 }
1741
1742 SDValue DAGTypeLegalizer::WidenVecRes_POWI(SDNode *N) {
1743   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1744   SDValue InOp = GetWidenedVector(N->getOperand(0));
1745   SDValue ShOp = N->getOperand(1);
1746   return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ShOp);
1747 }
1748
1749 SDValue DAGTypeLegalizer::WidenVecRes_Shift(SDNode *N) {
1750   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1751   SDValue InOp = GetWidenedVector(N->getOperand(0));
1752   SDValue ShOp = N->getOperand(1);
1753
1754   EVT ShVT = ShOp.getValueType();
1755   if (getTypeAction(ShVT) == TargetLowering::TypeWidenVector) {
1756     ShOp = GetWidenedVector(ShOp);
1757     ShVT = ShOp.getValueType();
1758   }
1759   EVT ShWidenVT = EVT::getVectorVT(*DAG.getContext(),
1760                                    ShVT.getVectorElementType(),
1761                                    WidenVT.getVectorNumElements());
1762   if (ShVT != ShWidenVT)
1763     ShOp = ModifyToType(ShOp, ShWidenVT);
1764
1765   return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ShOp);
1766 }
1767
1768 SDValue DAGTypeLegalizer::WidenVecRes_Unary(SDNode *N) {
1769   // Unary op widening.
1770   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1771   SDValue InOp = GetWidenedVector(N->getOperand(0));
1772   return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp);
1773 }
1774
1775 SDValue DAGTypeLegalizer::WidenVecRes_InregOp(SDNode *N) {
1776   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1777   EVT ExtVT = EVT::getVectorVT(*DAG.getContext(),
1778                                cast<VTSDNode>(N->getOperand(1))->getVT()
1779                                  .getVectorElementType(),
1780                                WidenVT.getVectorNumElements());
1781   SDValue WidenLHS = GetWidenedVector(N->getOperand(0));
1782   return DAG.getNode(N->getOpcode(), SDLoc(N),
1783                      WidenVT, WidenLHS, DAG.getValueType(ExtVT));
1784 }
1785
1786 SDValue DAGTypeLegalizer::WidenVecRes_MERGE_VALUES(SDNode *N, unsigned ResNo) {
1787   SDValue WidenVec = DisintegrateMERGE_VALUES(N, ResNo);
1788   return GetWidenedVector(WidenVec);
1789 }
1790
1791 SDValue DAGTypeLegalizer::WidenVecRes_BITCAST(SDNode *N) {
1792   SDValue InOp = N->getOperand(0);
1793   EVT InVT = InOp.getValueType();
1794   EVT VT = N->getValueType(0);
1795   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1796   SDLoc dl(N);
1797
1798   switch (getTypeAction(InVT)) {
1799   case TargetLowering::TypeLegal:
1800     break;
1801   case TargetLowering::TypePromoteInteger:
1802     // If the incoming type is a vector that is being promoted, then
1803     // we know that the elements are arranged differently and that we
1804     // must perform the conversion using a stack slot.
1805     if (InVT.isVector())
1806       break;
1807
1808     // If the InOp is promoted to the same size, convert it.  Otherwise,
1809     // fall out of the switch and widen the promoted input.
1810     InOp = GetPromotedInteger(InOp);
1811     InVT = InOp.getValueType();
1812     if (WidenVT.bitsEq(InVT))
1813       return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
1814     break;
1815   case TargetLowering::TypeSoftenFloat:
1816   case TargetLowering::TypeExpandInteger:
1817   case TargetLowering::TypeExpandFloat:
1818   case TargetLowering::TypeScalarizeVector:
1819   case TargetLowering::TypeSplitVector:
1820     break;
1821   case TargetLowering::TypeWidenVector:
1822     // If the InOp is widened to the same size, convert it.  Otherwise, fall
1823     // out of the switch and widen the widened input.
1824     InOp = GetWidenedVector(InOp);
1825     InVT = InOp.getValueType();
1826     if (WidenVT.bitsEq(InVT))
1827       // The input widens to the same size. Convert to the widen value.
1828       return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
1829     break;
1830   }
1831
1832   unsigned WidenSize = WidenVT.getSizeInBits();
1833   unsigned InSize = InVT.getSizeInBits();
1834   // x86mmx is not an acceptable vector element type, so don't try.
1835   if (WidenSize % InSize == 0 && InVT != MVT::x86mmx) {
1836     // Determine new input vector type.  The new input vector type will use
1837     // the same element type (if its a vector) or use the input type as a
1838     // vector.  It is the same size as the type to widen to.
1839     EVT NewInVT;
1840     unsigned NewNumElts = WidenSize / InSize;
1841     if (InVT.isVector()) {
1842       EVT InEltVT = InVT.getVectorElementType();
1843       NewInVT = EVT::getVectorVT(*DAG.getContext(), InEltVT,
1844                                  WidenSize / InEltVT.getSizeInBits());
1845     } else {
1846       NewInVT = EVT::getVectorVT(*DAG.getContext(), InVT, NewNumElts);
1847     }
1848
1849     if (TLI.isTypeLegal(NewInVT)) {
1850       // Because the result and the input are different vector types, widening
1851       // the result could create a legal type but widening the input might make
1852       // it an illegal type that might lead to repeatedly splitting the input
1853       // and then widening it. To avoid this, we widen the input only if
1854       // it results in a legal type.
1855       SmallVector<SDValue, 16> Ops(NewNumElts);
1856       SDValue UndefVal = DAG.getUNDEF(InVT);
1857       Ops[0] = InOp;
1858       for (unsigned i = 1; i < NewNumElts; ++i)
1859         Ops[i] = UndefVal;
1860
1861       SDValue NewVec;
1862       if (InVT.isVector())
1863         NewVec = DAG.getNode(ISD::CONCAT_VECTORS, dl,
1864                              NewInVT, &Ops[0], NewNumElts);
1865       else
1866         NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl,
1867                              NewInVT, &Ops[0], NewNumElts);
1868       return DAG.getNode(ISD::BITCAST, dl, WidenVT, NewVec);
1869     }
1870   }
1871
1872   return CreateStackStoreLoad(InOp, WidenVT);
1873 }
1874
1875 SDValue DAGTypeLegalizer::WidenVecRes_BUILD_VECTOR(SDNode *N) {
1876   SDLoc dl(N);
1877   // Build a vector with undefined for the new nodes.
1878   EVT VT = N->getValueType(0);
1879
1880   // Integer BUILD_VECTOR operands may be larger than the node's vector element
1881   // type. The UNDEFs need to have the same type as the existing operands.
1882   EVT EltVT = N->getOperand(0).getValueType();
1883   unsigned NumElts = VT.getVectorNumElements();
1884
1885   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1886   unsigned WidenNumElts = WidenVT.getVectorNumElements();
1887
1888   SmallVector<SDValue, 16> NewOps(N->op_begin(), N->op_end());
1889   assert(WidenNumElts >= NumElts && "Shrinking vector instead of widening!");
1890   NewOps.append(WidenNumElts - NumElts, DAG.getUNDEF(EltVT));
1891
1892   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &NewOps[0], NewOps.size());
1893 }
1894
1895 SDValue DAGTypeLegalizer::WidenVecRes_CONCAT_VECTORS(SDNode *N) {
1896   EVT InVT = N->getOperand(0).getValueType();
1897   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1898   SDLoc dl(N);
1899   unsigned WidenNumElts = WidenVT.getVectorNumElements();
1900   unsigned NumInElts = InVT.getVectorNumElements();
1901   unsigned NumOperands = N->getNumOperands();
1902
1903   bool InputWidened = false; // Indicates we need to widen the input.
1904   if (getTypeAction(InVT) != TargetLowering::TypeWidenVector) {
1905     if (WidenVT.getVectorNumElements() % InVT.getVectorNumElements() == 0) {
1906       // Add undef vectors to widen to correct length.
1907       unsigned NumConcat = WidenVT.getVectorNumElements() /
1908                            InVT.getVectorNumElements();
1909       SDValue UndefVal = DAG.getUNDEF(InVT);
1910       SmallVector<SDValue, 16> Ops(NumConcat);
1911       for (unsigned i=0; i < NumOperands; ++i)
1912         Ops[i] = N->getOperand(i);
1913       for (unsigned i = NumOperands; i != NumConcat; ++i)
1914         Ops[i] = UndefVal;
1915       return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &Ops[0], NumConcat);
1916     }
1917   } else {
1918     InputWidened = true;
1919     if (WidenVT == TLI.getTypeToTransformTo(*DAG.getContext(), InVT)) {
1920       // The inputs and the result are widen to the same value.
1921       unsigned i;
1922       for (i=1; i < NumOperands; ++i)
1923         if (N->getOperand(i).getOpcode() != ISD::UNDEF)
1924           break;
1925
1926       if (i == NumOperands)
1927         // Everything but the first operand is an UNDEF so just return the
1928         // widened first operand.
1929         return GetWidenedVector(N->getOperand(0));
1930
1931       if (NumOperands == 2) {
1932         // Replace concat of two operands with a shuffle.
1933         SmallVector<int, 16> MaskOps(WidenNumElts, -1);
1934         for (unsigned i = 0; i < NumInElts; ++i) {
1935           MaskOps[i] = i;
1936           MaskOps[i + NumInElts] = i + WidenNumElts;
1937         }
1938         return DAG.getVectorShuffle(WidenVT, dl,
1939                                     GetWidenedVector(N->getOperand(0)),
1940                                     GetWidenedVector(N->getOperand(1)),
1941                                     &MaskOps[0]);
1942       }
1943     }
1944   }
1945
1946   // Fall back to use extracts and build vector.
1947   EVT EltVT = WidenVT.getVectorElementType();
1948   SmallVector<SDValue, 16> Ops(WidenNumElts);
1949   unsigned Idx = 0;
1950   for (unsigned i=0; i < NumOperands; ++i) {
1951     SDValue InOp = N->getOperand(i);
1952     if (InputWidened)
1953       InOp = GetWidenedVector(InOp);
1954     for (unsigned j=0; j < NumInElts; ++j)
1955       Ops[Idx++] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
1956                                DAG.getIntPtrConstant(j));
1957   }
1958   SDValue UndefVal = DAG.getUNDEF(EltVT);
1959   for (; Idx < WidenNumElts; ++Idx)
1960     Ops[Idx] = UndefVal;
1961   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
1962 }
1963
1964 SDValue DAGTypeLegalizer::WidenVecRes_CONVERT_RNDSAT(SDNode *N) {
1965   SDLoc dl(N);
1966   SDValue InOp  = N->getOperand(0);
1967   SDValue RndOp = N->getOperand(3);
1968   SDValue SatOp = N->getOperand(4);
1969
1970   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1971   unsigned WidenNumElts = WidenVT.getVectorNumElements();
1972
1973   EVT InVT = InOp.getValueType();
1974   EVT InEltVT = InVT.getVectorElementType();
1975   EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
1976
1977   SDValue DTyOp = DAG.getValueType(WidenVT);
1978   SDValue STyOp = DAG.getValueType(InWidenVT);
1979   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
1980
1981   unsigned InVTNumElts = InVT.getVectorNumElements();
1982   if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
1983     InOp = GetWidenedVector(InOp);
1984     InVT = InOp.getValueType();
1985     InVTNumElts = InVT.getVectorNumElements();
1986     if (InVTNumElts == WidenNumElts)
1987       return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
1988                                   SatOp, CvtCode);
1989   }
1990
1991   if (TLI.isTypeLegal(InWidenVT)) {
1992     // Because the result and the input are different vector types, widening
1993     // the result could create a legal type but widening the input might make
1994     // it an illegal type that might lead to repeatedly splitting the input
1995     // and then widening it. To avoid this, we widen the input only if
1996     // it results in a legal type.
1997     if (WidenNumElts % InVTNumElts == 0) {
1998       // Widen the input and call convert on the widened input vector.
1999       unsigned NumConcat = WidenNumElts/InVTNumElts;
2000       SmallVector<SDValue, 16> Ops(NumConcat);
2001       Ops[0] = InOp;
2002       SDValue UndefVal = DAG.getUNDEF(InVT);
2003       for (unsigned i = 1; i != NumConcat; ++i)
2004         Ops[i] = UndefVal;
2005
2006       InOp = DAG.getNode(ISD::CONCAT_VECTORS, dl, InWidenVT, &Ops[0],NumConcat);
2007       return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
2008                                   SatOp, CvtCode);
2009     }
2010
2011     if (InVTNumElts % WidenNumElts == 0) {
2012       // Extract the input and convert the shorten input vector.
2013       InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InWidenVT, InOp,
2014                          DAG.getIntPtrConstant(0));
2015       return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
2016                                   SatOp, CvtCode);
2017     }
2018   }
2019
2020   // Otherwise unroll into some nasty scalar code and rebuild the vector.
2021   SmallVector<SDValue, 16> Ops(WidenNumElts);
2022   EVT EltVT = WidenVT.getVectorElementType();
2023   DTyOp = DAG.getValueType(EltVT);
2024   STyOp = DAG.getValueType(InEltVT);
2025
2026   unsigned MinElts = std::min(InVTNumElts, WidenNumElts);
2027   unsigned i;
2028   for (i=0; i < MinElts; ++i) {
2029     SDValue ExtVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
2030                                  DAG.getIntPtrConstant(i));
2031     Ops[i] = DAG.getConvertRndSat(WidenVT, dl, ExtVal, DTyOp, STyOp, RndOp,
2032                                   SatOp, CvtCode);
2033   }
2034
2035   SDValue UndefVal = DAG.getUNDEF(EltVT);
2036   for (; i < WidenNumElts; ++i)
2037     Ops[i] = UndefVal;
2038
2039   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
2040 }
2041
2042 SDValue DAGTypeLegalizer::WidenVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
2043   EVT      VT = N->getValueType(0);
2044   EVT      WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2045   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2046   SDValue  InOp = N->getOperand(0);
2047   SDValue  Idx  = N->getOperand(1);
2048   SDLoc dl(N);
2049
2050   if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2051     InOp = GetWidenedVector(InOp);
2052
2053   EVT InVT = InOp.getValueType();
2054
2055   // Check if we can just return the input vector after widening.
2056   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
2057   if (IdxVal == 0 && InVT == WidenVT)
2058     return InOp;
2059
2060   // Check if we can extract from the vector.
2061   unsigned InNumElts = InVT.getVectorNumElements();
2062   if (IdxVal % WidenNumElts == 0 && IdxVal + WidenNumElts < InNumElts)
2063     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, InOp, Idx);
2064
2065   // We could try widening the input to the right length but for now, extract
2066   // the original elements, fill the rest with undefs and build a vector.
2067   SmallVector<SDValue, 16> Ops(WidenNumElts);
2068   EVT EltVT = VT.getVectorElementType();
2069   unsigned NumElts = VT.getVectorNumElements();
2070   unsigned i;
2071   for (i=0; i < NumElts; ++i)
2072     Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2073                          DAG.getIntPtrConstant(IdxVal+i));
2074
2075   SDValue UndefVal = DAG.getUNDEF(EltVT);
2076   for (; i < WidenNumElts; ++i)
2077     Ops[i] = UndefVal;
2078   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
2079 }
2080
2081 SDValue DAGTypeLegalizer::WidenVecRes_INSERT_VECTOR_ELT(SDNode *N) {
2082   SDValue InOp = GetWidenedVector(N->getOperand(0));
2083   return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N),
2084                      InOp.getValueType(), InOp,
2085                      N->getOperand(1), N->getOperand(2));
2086 }
2087
2088 SDValue DAGTypeLegalizer::WidenVecRes_LOAD(SDNode *N) {
2089   LoadSDNode *LD = cast<LoadSDNode>(N);
2090   ISD::LoadExtType ExtType = LD->getExtensionType();
2091
2092   SDValue Result;
2093   SmallVector<SDValue, 16> LdChain;  // Chain for the series of load
2094   if (ExtType != ISD::NON_EXTLOAD)
2095     Result = GenWidenVectorExtLoads(LdChain, LD, ExtType);
2096   else
2097     Result = GenWidenVectorLoads(LdChain, LD);
2098
2099   // If we generate a single load, we can use that for the chain.  Otherwise,
2100   // build a factor node to remember the multiple loads are independent and
2101   // chain to that.
2102   SDValue NewChain;
2103   if (LdChain.size() == 1)
2104     NewChain = LdChain[0];
2105   else
2106     NewChain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
2107                            &LdChain[0], LdChain.size());
2108
2109   // Modified the chain - switch anything that used the old chain to use
2110   // the new one.
2111   ReplaceValueWith(SDValue(N, 1), NewChain);
2112
2113   return Result;
2114 }
2115
2116 SDValue DAGTypeLegalizer::WidenVecRes_SCALAR_TO_VECTOR(SDNode *N) {
2117   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2118   return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N),
2119                      WidenVT, N->getOperand(0));
2120 }
2121
2122 SDValue DAGTypeLegalizer::WidenVecRes_SELECT(SDNode *N) {
2123   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2124   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2125
2126   SDValue Cond1 = N->getOperand(0);
2127   EVT CondVT = Cond1.getValueType();
2128   if (CondVT.isVector()) {
2129     EVT CondEltVT = CondVT.getVectorElementType();
2130     EVT CondWidenVT =  EVT::getVectorVT(*DAG.getContext(),
2131                                         CondEltVT, WidenNumElts);
2132     if (getTypeAction(CondVT) == TargetLowering::TypeWidenVector)
2133       Cond1 = GetWidenedVector(Cond1);
2134
2135     if (Cond1.getValueType() != CondWidenVT)
2136       Cond1 = ModifyToType(Cond1, CondWidenVT);
2137   }
2138
2139   SDValue InOp1 = GetWidenedVector(N->getOperand(1));
2140   SDValue InOp2 = GetWidenedVector(N->getOperand(2));
2141   assert(InOp1.getValueType() == WidenVT && InOp2.getValueType() == WidenVT);
2142   return DAG.getNode(N->getOpcode(), SDLoc(N),
2143                      WidenVT, Cond1, InOp1, InOp2);
2144 }
2145
2146 SDValue DAGTypeLegalizer::WidenVecRes_SELECT_CC(SDNode *N) {
2147   SDValue InOp1 = GetWidenedVector(N->getOperand(2));
2148   SDValue InOp2 = GetWidenedVector(N->getOperand(3));
2149   return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
2150                      InOp1.getValueType(), N->getOperand(0),
2151                      N->getOperand(1), InOp1, InOp2, N->getOperand(4));
2152 }
2153
2154 SDValue DAGTypeLegalizer::WidenVecRes_SETCC(SDNode *N) {
2155   assert(N->getValueType(0).isVector() ==
2156          N->getOperand(0).getValueType().isVector() &&
2157          "Scalar/Vector type mismatch");
2158   if (N->getValueType(0).isVector()) return WidenVecRes_VSETCC(N);
2159
2160   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2161   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2162   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2163   return DAG.getNode(ISD::SETCC, SDLoc(N), WidenVT,
2164                      InOp1, InOp2, N->getOperand(2));
2165 }
2166
2167 SDValue DAGTypeLegalizer::WidenVecRes_UNDEF(SDNode *N) {
2168  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2169  return DAG.getUNDEF(WidenVT);
2170 }
2171
2172 SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
2173   EVT VT = N->getValueType(0);
2174   SDLoc dl(N);
2175
2176   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2177   unsigned NumElts = VT.getVectorNumElements();
2178   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2179
2180   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2181   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2182
2183   // Adjust mask based on new input vector length.
2184   SmallVector<int, 16> NewMask;
2185   for (unsigned i = 0; i != NumElts; ++i) {
2186     int Idx = N->getMaskElt(i);
2187     if (Idx < (int)NumElts)
2188       NewMask.push_back(Idx);
2189     else
2190       NewMask.push_back(Idx - NumElts + WidenNumElts);
2191   }
2192   for (unsigned i = NumElts; i != WidenNumElts; ++i)
2193     NewMask.push_back(-1);
2194   return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, &NewMask[0]);
2195 }
2196
2197 SDValue DAGTypeLegalizer::WidenVecRes_VSETCC(SDNode *N) {
2198   assert(N->getValueType(0).isVector() &&
2199          N->getOperand(0).getValueType().isVector() &&
2200          "Operands must be vectors");
2201   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2202   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2203
2204   SDValue InOp1 = N->getOperand(0);
2205   EVT InVT = InOp1.getValueType();
2206   assert(InVT.isVector() && "can not widen non vector type");
2207   EVT WidenInVT = EVT::getVectorVT(*DAG.getContext(),
2208                                    InVT.getVectorElementType(), WidenNumElts);
2209   InOp1 = GetWidenedVector(InOp1);
2210   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2211
2212   // Assume that the input and output will be widen appropriately.  If not,
2213   // we will have to unroll it at some point.
2214   assert(InOp1.getValueType() == WidenInVT &&
2215          InOp2.getValueType() == WidenInVT &&
2216          "Input not widened to expected type!");
2217   (void)WidenInVT;
2218   return DAG.getNode(ISD::SETCC, SDLoc(N),
2219                      WidenVT, InOp1, InOp2, N->getOperand(2));
2220 }
2221
2222
2223 //===----------------------------------------------------------------------===//
2224 // Widen Vector Operand
2225 //===----------------------------------------------------------------------===//
2226 bool DAGTypeLegalizer::WidenVectorOperand(SDNode *N, unsigned OpNo) {
2227   DEBUG(dbgs() << "Widen node operand " << OpNo << ": ";
2228         N->dump(&DAG);
2229         dbgs() << "\n");
2230   SDValue Res = SDValue();
2231
2232   // See if the target wants to custom widen this node.
2233   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
2234     return false;
2235
2236   switch (N->getOpcode()) {
2237   default:
2238 #ifndef NDEBUG
2239     dbgs() << "WidenVectorOperand op #" << OpNo << ": ";
2240     N->dump(&DAG);
2241     dbgs() << "\n";
2242 #endif
2243     llvm_unreachable("Do not know how to widen this operator's operand!");
2244
2245   case ISD::BITCAST:            Res = WidenVecOp_BITCAST(N); break;
2246   case ISD::CONCAT_VECTORS:     Res = WidenVecOp_CONCAT_VECTORS(N); break;
2247   case ISD::EXTRACT_SUBVECTOR:  Res = WidenVecOp_EXTRACT_SUBVECTOR(N); break;
2248   case ISD::EXTRACT_VECTOR_ELT: Res = WidenVecOp_EXTRACT_VECTOR_ELT(N); break;
2249   case ISD::STORE:              Res = WidenVecOp_STORE(N); break;
2250   case ISD::SETCC:              Res = WidenVecOp_SETCC(N); break;
2251
2252   case ISD::FP_EXTEND:
2253   case ISD::FP_TO_SINT:
2254   case ISD::FP_TO_UINT:
2255   case ISD::SINT_TO_FP:
2256   case ISD::UINT_TO_FP:
2257   case ISD::TRUNCATE:
2258   case ISD::SIGN_EXTEND:
2259   case ISD::ZERO_EXTEND:
2260   case ISD::ANY_EXTEND:
2261     Res = WidenVecOp_Convert(N);
2262     break;
2263   }
2264
2265   // If Res is null, the sub-method took care of registering the result.
2266   if (!Res.getNode()) return false;
2267
2268   // If the result is N, the sub-method updated N in place.  Tell the legalizer
2269   // core about this.
2270   if (Res.getNode() == N)
2271     return true;
2272
2273
2274   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
2275          "Invalid operand expansion");
2276
2277   ReplaceValueWith(SDValue(N, 0), Res);
2278   return false;
2279 }
2280
2281 SDValue DAGTypeLegalizer::WidenVecOp_Convert(SDNode *N) {
2282   // Since the result is legal and the input is illegal, it is unlikely
2283   // that we can fix the input to a legal type so unroll the convert
2284   // into some scalar code and create a nasty build vector.
2285   EVT VT = N->getValueType(0);
2286   EVT EltVT = VT.getVectorElementType();
2287   SDLoc dl(N);
2288   unsigned NumElts = VT.getVectorNumElements();
2289   SDValue InOp = N->getOperand(0);
2290   if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2291     InOp = GetWidenedVector(InOp);
2292   EVT InVT = InOp.getValueType();
2293   EVT InEltVT = InVT.getVectorElementType();
2294
2295   unsigned Opcode = N->getOpcode();
2296   SmallVector<SDValue, 16> Ops(NumElts);
2297   for (unsigned i=0; i < NumElts; ++i)
2298     Ops[i] = DAG.getNode(Opcode, dl, EltVT,
2299                          DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
2300                                      DAG.getIntPtrConstant(i)));
2301
2302   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts);
2303 }
2304
2305 SDValue DAGTypeLegalizer::WidenVecOp_BITCAST(SDNode *N) {
2306   EVT VT = N->getValueType(0);
2307   SDValue InOp = GetWidenedVector(N->getOperand(0));
2308   EVT InWidenVT = InOp.getValueType();
2309   SDLoc dl(N);
2310
2311   // Check if we can convert between two legal vector types and extract.
2312   unsigned InWidenSize = InWidenVT.getSizeInBits();
2313   unsigned Size = VT.getSizeInBits();
2314   // x86mmx is not an acceptable vector element type, so don't try.
2315   if (InWidenSize % Size == 0 && !VT.isVector() && VT != MVT::x86mmx) {
2316     unsigned NewNumElts = InWidenSize / Size;
2317     EVT NewVT = EVT::getVectorVT(*DAG.getContext(), VT, NewNumElts);
2318     if (TLI.isTypeLegal(NewVT)) {
2319       SDValue BitOp = DAG.getNode(ISD::BITCAST, dl, NewVT, InOp);
2320       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, BitOp,
2321                          DAG.getIntPtrConstant(0));
2322     }
2323   }
2324
2325   return CreateStackStoreLoad(InOp, VT);
2326 }
2327
2328 SDValue DAGTypeLegalizer::WidenVecOp_CONCAT_VECTORS(SDNode *N) {
2329   // If the input vector is not legal, it is likely that we will not find a
2330   // legal vector of the same size. Replace the concatenate vector with a
2331   // nasty build vector.
2332   EVT VT = N->getValueType(0);
2333   EVT EltVT = VT.getVectorElementType();
2334   SDLoc dl(N);
2335   unsigned NumElts = VT.getVectorNumElements();
2336   SmallVector<SDValue, 16> Ops(NumElts);
2337
2338   EVT InVT = N->getOperand(0).getValueType();
2339   unsigned NumInElts = InVT.getVectorNumElements();
2340
2341   unsigned Idx = 0;
2342   unsigned NumOperands = N->getNumOperands();
2343   for (unsigned i=0; i < NumOperands; ++i) {
2344     SDValue InOp = N->getOperand(i);
2345     if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2346       InOp = GetWidenedVector(InOp);
2347     for (unsigned j=0; j < NumInElts; ++j)
2348       Ops[Idx++] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2349                                DAG.getIntPtrConstant(j));
2350   }
2351   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts);
2352 }
2353
2354 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
2355   SDValue InOp = GetWidenedVector(N->getOperand(0));
2356   return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N),
2357                      N->getValueType(0), InOp, N->getOperand(1));
2358 }
2359
2360 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
2361   SDValue InOp = GetWidenedVector(N->getOperand(0));
2362   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
2363                      N->getValueType(0), InOp, N->getOperand(1));
2364 }
2365
2366 SDValue DAGTypeLegalizer::WidenVecOp_STORE(SDNode *N) {
2367   // We have to widen the value but we want only to store the original
2368   // vector type.
2369   StoreSDNode *ST = cast<StoreSDNode>(N);
2370
2371   SmallVector<SDValue, 16> StChain;
2372   if (ST->isTruncatingStore())
2373     GenWidenVectorTruncStores(StChain, ST);
2374   else
2375     GenWidenVectorStores(StChain, ST);
2376
2377   if (StChain.size() == 1)
2378     return StChain[0];
2379   else
2380     return DAG.getNode(ISD::TokenFactor, SDLoc(ST),
2381                        MVT::Other,&StChain[0],StChain.size());
2382 }
2383
2384 SDValue DAGTypeLegalizer::WidenVecOp_SETCC(SDNode *N) {
2385   SDValue InOp0 = GetWidenedVector(N->getOperand(0));
2386   SDValue InOp1 = GetWidenedVector(N->getOperand(1));
2387   SDLoc dl(N);
2388
2389   // WARNING: In this code we widen the compare instruction with garbage.
2390   // This garbage may contain denormal floats which may be slow. Is this a real
2391   // concern ? Should we zero the unused lanes if this is a float compare ?
2392
2393   // Get a new SETCC node to compare the newly widened operands.
2394   // Only some of the compared elements are legal.
2395   EVT SVT = TLI.getSetCCResultType(*DAG.getContext(), InOp0.getValueType());
2396   SDValue WideSETCC = DAG.getNode(ISD::SETCC, SDLoc(N),
2397                      SVT, InOp0, InOp1, N->getOperand(2));
2398
2399   // Extract the needed results from the result vector.
2400   EVT ResVT = EVT::getVectorVT(*DAG.getContext(),
2401                                SVT.getVectorElementType(),
2402                                N->getValueType(0).getVectorNumElements());
2403   SDValue CC = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
2404                            ResVT, WideSETCC, DAG.getIntPtrConstant(0));
2405
2406   return PromoteTargetBoolean(CC, N->getValueType(0));
2407 }
2408
2409
2410 //===----------------------------------------------------------------------===//
2411 // Vector Widening Utilities
2412 //===----------------------------------------------------------------------===//
2413
2414 // Utility function to find the type to chop up a widen vector for load/store
2415 //  TLI:       Target lowering used to determine legal types.
2416 //  Width:     Width left need to load/store.
2417 //  WidenVT:   The widen vector type to load to/store from
2418 //  Align:     If 0, don't allow use of a wider type
2419 //  WidenEx:   If Align is not 0, the amount additional we can load/store from.
2420
2421 static EVT FindMemType(SelectionDAG& DAG, const TargetLowering &TLI,
2422                        unsigned Width, EVT WidenVT,
2423                        unsigned Align = 0, unsigned WidenEx = 0) {
2424   EVT WidenEltVT = WidenVT.getVectorElementType();
2425   unsigned WidenWidth = WidenVT.getSizeInBits();
2426   unsigned WidenEltWidth = WidenEltVT.getSizeInBits();
2427   unsigned AlignInBits = Align*8;
2428
2429   // If we have one element to load/store, return it.
2430   EVT RetVT = WidenEltVT;
2431   if (Width == WidenEltWidth)
2432     return RetVT;
2433
2434   // See if there is larger legal integer than the element type to load/store
2435   unsigned VT;
2436   for (VT = (unsigned)MVT::LAST_INTEGER_VALUETYPE;
2437        VT >= (unsigned)MVT::FIRST_INTEGER_VALUETYPE; --VT) {
2438     EVT MemVT((MVT::SimpleValueType) VT);
2439     unsigned MemVTWidth = MemVT.getSizeInBits();
2440     if (MemVT.getSizeInBits() <= WidenEltWidth)
2441       break;
2442     if (TLI.isTypeLegal(MemVT) && (WidenWidth % MemVTWidth) == 0 &&
2443         isPowerOf2_32(WidenWidth / MemVTWidth) &&
2444         (MemVTWidth <= Width ||
2445          (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
2446       RetVT = MemVT;
2447       break;
2448     }
2449   }
2450
2451   // See if there is a larger vector type to load/store that has the same vector
2452   // element type and is evenly divisible with the WidenVT.
2453   for (VT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
2454        VT >= (unsigned)MVT::FIRST_VECTOR_VALUETYPE; --VT) {
2455     EVT MemVT = (MVT::SimpleValueType) VT;
2456     unsigned MemVTWidth = MemVT.getSizeInBits();
2457     if (TLI.isTypeLegal(MemVT) && WidenEltVT == MemVT.getVectorElementType() &&
2458         (WidenWidth % MemVTWidth) == 0 &&
2459         isPowerOf2_32(WidenWidth / MemVTWidth) &&
2460         (MemVTWidth <= Width ||
2461          (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
2462       if (RetVT.getSizeInBits() < MemVTWidth || MemVT == WidenVT)
2463         return MemVT;
2464     }
2465   }
2466
2467   return RetVT;
2468 }
2469
2470 // Builds a vector type from scalar loads
2471 //  VecTy: Resulting Vector type
2472 //  LDOps: Load operators to build a vector type
2473 //  [Start,End) the list of loads to use.
2474 static SDValue BuildVectorFromScalar(SelectionDAG& DAG, EVT VecTy,
2475                                      SmallVectorImpl<SDValue> &LdOps,
2476                                      unsigned Start, unsigned End) {
2477   SDLoc dl(LdOps[Start]);
2478   EVT LdTy = LdOps[Start].getValueType();
2479   unsigned Width = VecTy.getSizeInBits();
2480   unsigned NumElts = Width / LdTy.getSizeInBits();
2481   EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), LdTy, NumElts);
2482
2483   unsigned Idx = 1;
2484   SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT,LdOps[Start]);
2485
2486   for (unsigned i = Start + 1; i != End; ++i) {
2487     EVT NewLdTy = LdOps[i].getValueType();
2488     if (NewLdTy != LdTy) {
2489       NumElts = Width / NewLdTy.getSizeInBits();
2490       NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewLdTy, NumElts);
2491       VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, VecOp);
2492       // Readjust position and vector position based on new load type
2493       Idx = Idx * LdTy.getSizeInBits() / NewLdTy.getSizeInBits();
2494       LdTy = NewLdTy;
2495     }
2496     VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, VecOp, LdOps[i],
2497                         DAG.getIntPtrConstant(Idx++));
2498   }
2499   return DAG.getNode(ISD::BITCAST, dl, VecTy, VecOp);
2500 }
2501
2502 SDValue DAGTypeLegalizer::GenWidenVectorLoads(SmallVectorImpl<SDValue> &LdChain,
2503                                               LoadSDNode *LD) {
2504   // The strategy assumes that we can efficiently load powers of two widths.
2505   // The routines chops the vector into the largest vector loads with the same
2506   // element type or scalar loads and then recombines it to the widen vector
2507   // type.
2508   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
2509   unsigned WidenWidth = WidenVT.getSizeInBits();
2510   EVT LdVT    = LD->getMemoryVT();
2511   SDLoc dl(LD);
2512   assert(LdVT.isVector() && WidenVT.isVector());
2513   assert(LdVT.getVectorElementType() == WidenVT.getVectorElementType());
2514
2515   // Load information
2516   SDValue   Chain = LD->getChain();
2517   SDValue   BasePtr = LD->getBasePtr();
2518   unsigned  Align    = LD->getAlignment();
2519   bool      isVolatile = LD->isVolatile();
2520   bool      isNonTemporal = LD->isNonTemporal();
2521   bool      isInvariant = LD->isInvariant();
2522
2523   int LdWidth = LdVT.getSizeInBits();
2524   int WidthDiff = WidenWidth - LdWidth;          // Difference
2525   unsigned LdAlign = (isVolatile) ? 0 : Align; // Allow wider loads
2526
2527   // Find the vector type that can load from.
2528   EVT NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
2529   int NewVTWidth = NewVT.getSizeInBits();
2530   SDValue LdOp = DAG.getLoad(NewVT, dl, Chain, BasePtr, LD->getPointerInfo(),
2531                              isVolatile, isNonTemporal, isInvariant, Align);
2532   LdChain.push_back(LdOp.getValue(1));
2533
2534   // Check if we can load the element with one instruction
2535   if (LdWidth <= NewVTWidth) {
2536     if (!NewVT.isVector()) {
2537       unsigned NumElts = WidenWidth / NewVTWidth;
2538       EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
2539       SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT, LdOp);
2540       return DAG.getNode(ISD::BITCAST, dl, WidenVT, VecOp);
2541     }
2542     if (NewVT == WidenVT)
2543       return LdOp;
2544
2545     assert(WidenWidth % NewVTWidth == 0);
2546     unsigned NumConcat = WidenWidth / NewVTWidth;
2547     SmallVector<SDValue, 16> ConcatOps(NumConcat);
2548     SDValue UndefVal = DAG.getUNDEF(NewVT);
2549     ConcatOps[0] = LdOp;
2550     for (unsigned i = 1; i != NumConcat; ++i)
2551       ConcatOps[i] = UndefVal;
2552     return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &ConcatOps[0],
2553                        NumConcat);
2554   }
2555
2556   // Load vector by using multiple loads from largest vector to scalar
2557   SmallVector<SDValue, 16> LdOps;
2558   LdOps.push_back(LdOp);
2559
2560   LdWidth -= NewVTWidth;
2561   unsigned Offset = 0;
2562
2563   while (LdWidth > 0) {
2564     unsigned Increment = NewVTWidth / 8;
2565     Offset += Increment;
2566     BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2567                           DAG.getIntPtrConstant(Increment));
2568
2569     SDValue L;
2570     if (LdWidth < NewVTWidth) {
2571       // Our current type we are using is too large, find a better size
2572       NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
2573       NewVTWidth = NewVT.getSizeInBits();
2574       L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
2575                       LD->getPointerInfo().getWithOffset(Offset), isVolatile,
2576                       isNonTemporal, isInvariant, MinAlign(Align, Increment));
2577       LdChain.push_back(L.getValue(1));
2578       if (L->getValueType(0).isVector()) {
2579         SmallVector<SDValue, 16> Loads;
2580         Loads.push_back(L);
2581         unsigned size = L->getValueSizeInBits(0);
2582         while (size < LdOp->getValueSizeInBits(0)) {
2583           Loads.push_back(DAG.getUNDEF(L->getValueType(0)));
2584           size += L->getValueSizeInBits(0);
2585         }
2586         L = DAG.getNode(ISD::CONCAT_VECTORS, dl, LdOp->getValueType(0),
2587                         &Loads[0], Loads.size());
2588       }
2589     } else {
2590       L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
2591                       LD->getPointerInfo().getWithOffset(Offset), isVolatile,
2592                       isNonTemporal, isInvariant, MinAlign(Align, Increment));
2593       LdChain.push_back(L.getValue(1));
2594     }
2595
2596     LdOps.push_back(L);
2597
2598
2599     LdWidth -= NewVTWidth;
2600   }
2601
2602   // Build the vector from the loads operations
2603   unsigned End = LdOps.size();
2604   if (!LdOps[0].getValueType().isVector())
2605     // All the loads are scalar loads.
2606     return BuildVectorFromScalar(DAG, WidenVT, LdOps, 0, End);
2607
2608   // If the load contains vectors, build the vector using concat vector.
2609   // All of the vectors used to loads are power of 2 and the scalars load
2610   // can be combined to make a power of 2 vector.
2611   SmallVector<SDValue, 16> ConcatOps(End);
2612   int i = End - 1;
2613   int Idx = End;
2614   EVT LdTy = LdOps[i].getValueType();
2615   // First combine the scalar loads to a vector
2616   if (!LdTy.isVector())  {
2617     for (--i; i >= 0; --i) {
2618       LdTy = LdOps[i].getValueType();
2619       if (LdTy.isVector())
2620         break;
2621     }
2622     ConcatOps[--Idx] = BuildVectorFromScalar(DAG, LdTy, LdOps, i+1, End);
2623   }
2624   ConcatOps[--Idx] = LdOps[i];
2625   for (--i; i >= 0; --i) {
2626     EVT NewLdTy = LdOps[i].getValueType();
2627     if (NewLdTy != LdTy) {
2628       // Create a larger vector
2629       ConcatOps[End-1] = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewLdTy,
2630                                      &ConcatOps[Idx], End - Idx);
2631       Idx = End - 1;
2632       LdTy = NewLdTy;
2633     }
2634     ConcatOps[--Idx] = LdOps[i];
2635   }
2636
2637   if (WidenWidth == LdTy.getSizeInBits()*(End - Idx))
2638     return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
2639                        &ConcatOps[Idx], End - Idx);
2640
2641   // We need to fill the rest with undefs to build the vector
2642   unsigned NumOps = WidenWidth / LdTy.getSizeInBits();
2643   SmallVector<SDValue, 16> WidenOps(NumOps);
2644   SDValue UndefVal = DAG.getUNDEF(LdTy);
2645   {
2646     unsigned i = 0;
2647     for (; i != End-Idx; ++i)
2648       WidenOps[i] = ConcatOps[Idx+i];
2649     for (; i != NumOps; ++i)
2650       WidenOps[i] = UndefVal;
2651   }
2652   return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &WidenOps[0],NumOps);
2653 }
2654
2655 SDValue
2656 DAGTypeLegalizer::GenWidenVectorExtLoads(SmallVectorImpl<SDValue> &LdChain,
2657                                          LoadSDNode *LD,
2658                                          ISD::LoadExtType ExtType) {
2659   // For extension loads, it may not be more efficient to chop up the vector
2660   // and then extended it.  Instead, we unroll the load and build a new vector.
2661   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
2662   EVT LdVT    = LD->getMemoryVT();
2663   SDLoc dl(LD);
2664   assert(LdVT.isVector() && WidenVT.isVector());
2665
2666   // Load information
2667   SDValue   Chain = LD->getChain();
2668   SDValue   BasePtr = LD->getBasePtr();
2669   unsigned  Align    = LD->getAlignment();
2670   bool      isVolatile = LD->isVolatile();
2671   bool      isNonTemporal = LD->isNonTemporal();
2672
2673   EVT EltVT = WidenVT.getVectorElementType();
2674   EVT LdEltVT = LdVT.getVectorElementType();
2675   unsigned NumElts = LdVT.getVectorNumElements();
2676
2677   // Load each element and widen
2678   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2679   SmallVector<SDValue, 16> Ops(WidenNumElts);
2680   unsigned Increment = LdEltVT.getSizeInBits() / 8;
2681   Ops[0] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, BasePtr,
2682                           LD->getPointerInfo(),
2683                           LdEltVT, isVolatile, isNonTemporal, Align);
2684   LdChain.push_back(Ops[0].getValue(1));
2685   unsigned i = 0, Offset = Increment;
2686   for (i=1; i < NumElts; ++i, Offset += Increment) {
2687     SDValue NewBasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
2688                                      BasePtr, DAG.getIntPtrConstant(Offset));
2689     Ops[i] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, NewBasePtr,
2690                             LD->getPointerInfo().getWithOffset(Offset), LdEltVT,
2691                             isVolatile, isNonTemporal, Align);
2692     LdChain.push_back(Ops[i].getValue(1));
2693   }
2694
2695   // Fill the rest with undefs
2696   SDValue UndefVal = DAG.getUNDEF(EltVT);
2697   for (; i != WidenNumElts; ++i)
2698     Ops[i] = UndefVal;
2699
2700   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], Ops.size());
2701 }
2702
2703
2704 void DAGTypeLegalizer::GenWidenVectorStores(SmallVectorImpl<SDValue> &StChain,
2705                                             StoreSDNode *ST) {
2706   // The strategy assumes that we can efficiently store powers of two widths.
2707   // The routines chops the vector into the largest vector stores with the same
2708   // element type or scalar stores.
2709   SDValue  Chain = ST->getChain();
2710   SDValue  BasePtr = ST->getBasePtr();
2711   unsigned Align = ST->getAlignment();
2712   bool     isVolatile = ST->isVolatile();
2713   bool     isNonTemporal = ST->isNonTemporal();
2714   SDValue  ValOp = GetWidenedVector(ST->getValue());
2715   SDLoc dl(ST);
2716
2717   EVT StVT = ST->getMemoryVT();
2718   unsigned StWidth = StVT.getSizeInBits();
2719   EVT ValVT = ValOp.getValueType();
2720   unsigned ValWidth = ValVT.getSizeInBits();
2721   EVT ValEltVT = ValVT.getVectorElementType();
2722   unsigned ValEltWidth = ValEltVT.getSizeInBits();
2723   assert(StVT.getVectorElementType() == ValEltVT);
2724
2725   int Idx = 0;          // current index to store
2726   unsigned Offset = 0;  // offset from base to store
2727   while (StWidth != 0) {
2728     // Find the largest vector type we can store with
2729     EVT NewVT = FindMemType(DAG, TLI, StWidth, ValVT);
2730     unsigned NewVTWidth = NewVT.getSizeInBits();
2731     unsigned Increment = NewVTWidth / 8;
2732     if (NewVT.isVector()) {
2733       unsigned NumVTElts = NewVT.getVectorNumElements();
2734       do {
2735         SDValue EOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT, ValOp,
2736                                    DAG.getIntPtrConstant(Idx));
2737         StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr,
2738                                     ST->getPointerInfo().getWithOffset(Offset),
2739                                        isVolatile, isNonTemporal,
2740                                        MinAlign(Align, Offset)));
2741         StWidth -= NewVTWidth;
2742         Offset += Increment;
2743         Idx += NumVTElts;
2744         BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2745                               DAG.getIntPtrConstant(Increment));
2746       } while (StWidth != 0 && StWidth >= NewVTWidth);
2747     } else {
2748       // Cast the vector to the scalar type we can store
2749       unsigned NumElts = ValWidth / NewVTWidth;
2750       EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
2751       SDValue VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, ValOp);
2752       // Readjust index position based on new vector type
2753       Idx = Idx * ValEltWidth / NewVTWidth;
2754       do {
2755         SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, VecOp,
2756                       DAG.getIntPtrConstant(Idx++));
2757         StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr,
2758                                     ST->getPointerInfo().getWithOffset(Offset),
2759                                        isVolatile, isNonTemporal,
2760                                        MinAlign(Align, Offset)));
2761         StWidth -= NewVTWidth;
2762         Offset += Increment;
2763         BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2764                               DAG.getIntPtrConstant(Increment));
2765       } while (StWidth != 0 && StWidth >= NewVTWidth);
2766       // Restore index back to be relative to the original widen element type
2767       Idx = Idx * NewVTWidth / ValEltWidth;
2768     }
2769   }
2770 }
2771
2772 void
2773 DAGTypeLegalizer::GenWidenVectorTruncStores(SmallVectorImpl<SDValue> &StChain,
2774                                             StoreSDNode *ST) {
2775   // For extension loads, it may not be more efficient to truncate the vector
2776   // and then store it.  Instead, we extract each element and then store it.
2777   SDValue  Chain = ST->getChain();
2778   SDValue  BasePtr = ST->getBasePtr();
2779   unsigned Align = ST->getAlignment();
2780   bool     isVolatile = ST->isVolatile();
2781   bool     isNonTemporal = ST->isNonTemporal();
2782   SDValue  ValOp = GetWidenedVector(ST->getValue());
2783   SDLoc dl(ST);
2784
2785   EVT StVT = ST->getMemoryVT();
2786   EVT ValVT = ValOp.getValueType();
2787
2788   // It must be true that we the widen vector type is bigger than where
2789   // we need to store.
2790   assert(StVT.isVector() && ValOp.getValueType().isVector());
2791   assert(StVT.bitsLT(ValOp.getValueType()));
2792
2793   // For truncating stores, we can not play the tricks of chopping legal
2794   // vector types and bit cast it to the right type.  Instead, we unroll
2795   // the store.
2796   EVT StEltVT  = StVT.getVectorElementType();
2797   EVT ValEltVT = ValVT.getVectorElementType();
2798   unsigned Increment = ValEltVT.getSizeInBits() / 8;
2799   unsigned NumElts = StVT.getVectorNumElements();
2800   SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ValEltVT, ValOp,
2801                             DAG.getIntPtrConstant(0));
2802   StChain.push_back(DAG.getTruncStore(Chain, dl, EOp, BasePtr,
2803                                       ST->getPointerInfo(), StEltVT,
2804                                       isVolatile, isNonTemporal, Align));
2805   unsigned Offset = Increment;
2806   for (unsigned i=1; i < NumElts; ++i, Offset += Increment) {
2807     SDValue NewBasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
2808                                      BasePtr, DAG.getIntPtrConstant(Offset));
2809     SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ValEltVT, ValOp,
2810                             DAG.getIntPtrConstant(0));
2811     StChain.push_back(DAG.getTruncStore(Chain, dl, EOp, NewBasePtr,
2812                                       ST->getPointerInfo().getWithOffset(Offset),
2813                                         StEltVT, isVolatile, isNonTemporal,
2814                                         MinAlign(Align, Offset)));
2815   }
2816 }
2817
2818 /// Modifies a vector input (widen or narrows) to a vector of NVT.  The
2819 /// input vector must have the same element type as NVT.
2820 SDValue DAGTypeLegalizer::ModifyToType(SDValue InOp, EVT NVT) {
2821   // Note that InOp might have been widened so it might already have
2822   // the right width or it might need be narrowed.
2823   EVT InVT = InOp.getValueType();
2824   assert(InVT.getVectorElementType() == NVT.getVectorElementType() &&
2825          "input and widen element type must match");
2826   SDLoc dl(InOp);
2827
2828   // Check if InOp already has the right width.
2829   if (InVT == NVT)
2830     return InOp;
2831
2832   unsigned InNumElts = InVT.getVectorNumElements();
2833   unsigned WidenNumElts = NVT.getVectorNumElements();
2834   if (WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0) {
2835     unsigned NumConcat = WidenNumElts / InNumElts;
2836     SmallVector<SDValue, 16> Ops(NumConcat);
2837     SDValue UndefVal = DAG.getUNDEF(InVT);
2838     Ops[0] = InOp;
2839     for (unsigned i = 1; i != NumConcat; ++i)
2840       Ops[i] = UndefVal;
2841
2842     return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, &Ops[0], NumConcat);
2843   }
2844
2845   if (WidenNumElts < InNumElts && InNumElts % WidenNumElts)
2846     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, InOp,
2847                        DAG.getIntPtrConstant(0));
2848
2849   // Fall back to extract and build.
2850   SmallVector<SDValue, 16> Ops(WidenNumElts);
2851   EVT EltVT = NVT.getVectorElementType();
2852   unsigned MinNumElts = std::min(WidenNumElts, InNumElts);
2853   unsigned Idx;
2854   for (Idx = 0; Idx < MinNumElts; ++Idx)
2855     Ops[Idx] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2856                            DAG.getIntPtrConstant(Idx));
2857
2858   SDValue UndefVal = DAG.getUNDEF(EltVT);
2859   for ( ; Idx < WidenNumElts; ++Idx)
2860     Ops[Idx] = UndefVal;
2861   return DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &Ops[0], WidenNumElts);
2862 }