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