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