OSDN Git Service

fix typos in comments; NFC
[android-x86/external-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeVectorTypes.cpp
1 //===------- LegalizeVectorTypes.cpp - Legalization of vector types -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file performs vector type splitting and scalarization for LegalizeTypes.
11 // Scalarization is the act of changing a computation in an illegal one-element
12 // vector type to be a computation in its scalar element type.  For example,
13 // implementing <1 x f32> arithmetic in a scalar f32 register.  This is needed
14 // as a base case when scalarizing vector arithmetic like <4 x f32>, which
15 // eventually decomposes to scalars if the target doesn't support v4f32 or v2f32
16 // types.
17 // Splitting is the act of changing a computation in an invalid vector type to
18 // be a computation in two vectors of half the size.  For example, implementing
19 // <128 x f32> operations in terms of two <64 x f32> operations.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #include "LegalizeTypes.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace llvm;
28
29 #define DEBUG_TYPE "legalize-types"
30
31 //===----------------------------------------------------------------------===//
32 //  Result Vector Scalarization: <1 x ty> -> ty.
33 //===----------------------------------------------------------------------===//
34
35 void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
36   DEBUG(dbgs() << "Scalarize node result " << ResNo << ": ";
37         N->dump(&DAG);
38         dbgs() << "\n");
39   SDValue R = SDValue();
40
41   switch (N->getOpcode()) {
42   default:
43 #ifndef NDEBUG
44     dbgs() << "ScalarizeVectorResult #" << ResNo << ": ";
45     N->dump(&DAG);
46     dbgs() << "\n";
47 #endif
48     report_fatal_error("Do not know how to scalarize the result of this "
49                        "operator!\n");
50
51   case ISD::MERGE_VALUES:      R = ScalarizeVecRes_MERGE_VALUES(N, ResNo);break;
52   case ISD::BITCAST:           R = ScalarizeVecRes_BITCAST(N); break;
53   case ISD::BUILD_VECTOR:      R = ScalarizeVecRes_BUILD_VECTOR(N); break;
54   case ISD::CONVERT_RNDSAT:    R = ScalarizeVecRes_CONVERT_RNDSAT(N); break;
55   case ISD::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
56   case ISD::FP_ROUND:          R = ScalarizeVecRes_FP_ROUND(N); break;
57   case ISD::FP_ROUND_INREG:    R = ScalarizeVecRes_InregOp(N); break;
58   case ISD::FPOWI:             R = ScalarizeVecRes_FPOWI(N); break;
59   case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
60   case ISD::LOAD:           R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
61   case ISD::SCALAR_TO_VECTOR:  R = ScalarizeVecRes_SCALAR_TO_VECTOR(N); break;
62   case ISD::SIGN_EXTEND_INREG: R = ScalarizeVecRes_InregOp(N); break;
63   case ISD::VSELECT:           R = ScalarizeVecRes_VSELECT(N); break;
64   case ISD::SELECT:            R = ScalarizeVecRes_SELECT(N); break;
65   case ISD::SELECT_CC:         R = ScalarizeVecRes_SELECT_CC(N); break;
66   case ISD::SETCC:             R = ScalarizeVecRes_SETCC(N); break;
67   case ISD::UNDEF:             R = ScalarizeVecRes_UNDEF(N); break;
68   case ISD::VECTOR_SHUFFLE:    R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
69   case ISD::ANY_EXTEND:
70   case ISD::BITREVERSE:
71   case ISD::BSWAP:
72   case ISD::CTLZ:
73   case ISD::CTLZ_ZERO_UNDEF:
74   case ISD::CTPOP:
75   case ISD::CTTZ:
76   case ISD::CTTZ_ZERO_UNDEF:
77   case ISD::FABS:
78   case ISD::FCEIL:
79   case ISD::FCOS:
80   case ISD::FEXP:
81   case ISD::FEXP2:
82   case ISD::FFLOOR:
83   case ISD::FLOG:
84   case ISD::FLOG10:
85   case ISD::FLOG2:
86   case ISD::FNEARBYINT:
87   case ISD::FNEG:
88   case ISD::FP_EXTEND:
89   case ISD::FP_TO_SINT:
90   case ISD::FP_TO_UINT:
91   case ISD::FRINT:
92   case ISD::FROUND:
93   case ISD::FSIN:
94   case ISD::FSQRT:
95   case ISD::FTRUNC:
96   case ISD::SIGN_EXTEND:
97   case ISD::SINT_TO_FP:
98   case ISD::TRUNCATE:
99   case ISD::UINT_TO_FP:
100   case ISD::ZERO_EXTEND:
101     R = ScalarizeVecRes_UnaryOp(N);
102     break;
103
104   case ISD::ADD:
105   case ISD::AND:
106   case ISD::FADD:
107   case ISD::FCOPYSIGN:
108   case ISD::FDIV:
109   case ISD::FMUL:
110   case ISD::FMINNUM:
111   case ISD::FMAXNUM:
112   case ISD::FMINNAN:
113   case ISD::FMAXNAN:
114   case ISD::SMIN:
115   case ISD::SMAX:
116   case ISD::UMIN:
117   case ISD::UMAX:
118
119   case ISD::FPOW:
120   case ISD::FREM:
121   case ISD::FSUB:
122   case ISD::MUL:
123   case ISD::OR:
124   case ISD::SDIV:
125   case ISD::SREM:
126   case ISD::SUB:
127   case ISD::UDIV:
128   case ISD::UREM:
129   case ISD::XOR:
130   case ISD::SHL:
131   case ISD::SRA:
132   case ISD::SRL:
133     R = ScalarizeVecRes_BinOp(N);
134     break;
135   case ISD::FMA:
136     R = ScalarizeVecRes_TernaryOp(N);
137     break;
138   }
139
140   // If R is null, the sub-method took care of registering the result.
141   if (R.getNode())
142     SetScalarizedVector(SDValue(N, ResNo), R);
143 }
144
145 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
146   SDValue LHS = GetScalarizedVector(N->getOperand(0));
147   SDValue RHS = GetScalarizedVector(N->getOperand(1));
148   return DAG.getNode(N->getOpcode(), SDLoc(N),
149                      LHS.getValueType(), LHS, RHS, N->getFlags());
150 }
151
152 SDValue DAGTypeLegalizer::ScalarizeVecRes_TernaryOp(SDNode *N) {
153   SDValue Op0 = GetScalarizedVector(N->getOperand(0));
154   SDValue Op1 = GetScalarizedVector(N->getOperand(1));
155   SDValue Op2 = GetScalarizedVector(N->getOperand(2));
156   return DAG.getNode(N->getOpcode(), SDLoc(N),
157                      Op0.getValueType(), Op0, Op1, Op2);
158 }
159
160 SDValue DAGTypeLegalizer::ScalarizeVecRes_MERGE_VALUES(SDNode *N,
161                                                        unsigned ResNo) {
162   SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
163   return GetScalarizedVector(Op);
164 }
165
166 SDValue DAGTypeLegalizer::ScalarizeVecRes_BITCAST(SDNode *N) {
167   EVT NewVT = N->getValueType(0).getVectorElementType();
168   return DAG.getNode(ISD::BITCAST, SDLoc(N),
169                      NewVT, N->getOperand(0));
170 }
171
172 SDValue DAGTypeLegalizer::ScalarizeVecRes_BUILD_VECTOR(SDNode *N) {
173   EVT EltVT = N->getValueType(0).getVectorElementType();
174   SDValue InOp = N->getOperand(0);
175   // The BUILD_VECTOR operands may be of wider element types and
176   // we may need to truncate them back to the requested return type.
177   if (EltVT.isInteger())
178     return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
179   return InOp;
180 }
181
182 SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_RNDSAT(SDNode *N) {
183   EVT NewVT = N->getValueType(0).getVectorElementType();
184   SDValue Op0 = GetScalarizedVector(N->getOperand(0));
185   return DAG.getConvertRndSat(NewVT, SDLoc(N),
186                               Op0, DAG.getValueType(NewVT),
187                               DAG.getValueType(Op0.getValueType()),
188                               N->getOperand(3),
189                               N->getOperand(4),
190                               cast<CvtRndSatSDNode>(N)->getCvtCode());
191 }
192
193 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
194   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
195                      N->getValueType(0).getVectorElementType(),
196                      N->getOperand(0), N->getOperand(1));
197 }
198
199 SDValue DAGTypeLegalizer::ScalarizeVecRes_FP_ROUND(SDNode *N) {
200   EVT NewVT = N->getValueType(0).getVectorElementType();
201   SDValue Op = GetScalarizedVector(N->getOperand(0));
202   return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
203                      NewVT, Op, N->getOperand(1));
204 }
205
206 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
207   SDValue Op = GetScalarizedVector(N->getOperand(0));
208   return DAG.getNode(ISD::FPOWI, SDLoc(N),
209                      Op.getValueType(), Op, N->getOperand(1));
210 }
211
212 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
213   // The value to insert may have a wider type than the vector element type,
214   // so be sure to truncate it to the element type if necessary.
215   SDValue Op = N->getOperand(1);
216   EVT EltVT = N->getValueType(0).getVectorElementType();
217   if (Op.getValueType() != EltVT)
218     // FIXME: Can this happen for floating point types?
219     Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, Op);
220   return Op;
221 }
222
223 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
224   assert(N->isUnindexed() && "Indexed vector load?");
225
226   SDValue Result = DAG.getLoad(ISD::UNINDEXED,
227                                N->getExtensionType(),
228                                N->getValueType(0).getVectorElementType(),
229                                SDLoc(N),
230                                N->getChain(), N->getBasePtr(),
231                                DAG.getUNDEF(N->getBasePtr().getValueType()),
232                                N->getPointerInfo(),
233                                N->getMemoryVT().getVectorElementType(),
234                                N->isVolatile(), N->isNonTemporal(),
235                                N->isInvariant(), N->getOriginalAlignment(),
236                                N->getAAInfo());
237
238   // Legalize the chain result - switch anything that used the old chain to
239   // use the new one.
240   ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
241   return Result;
242 }
243
244 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
245   // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
246   EVT DestVT = N->getValueType(0).getVectorElementType();
247   SDValue Op = N->getOperand(0);
248   EVT OpVT = Op.getValueType();
249   SDLoc DL(N);
250   // The result needs scalarizing, but it's not a given that the source does.
251   // This is a workaround for targets where it's impossible to scalarize the
252   // result of a conversion, because the source type is legal.
253   // For instance, this happens on AArch64: v1i1 is illegal but v1i{8,16,32}
254   // are widened to v8i8, v4i16, and v2i32, which is legal, because v1i64 is
255   // legal and was not scalarized.
256   // See the similar logic in ScalarizeVecRes_VSETCC
257   if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
258     Op = GetScalarizedVector(Op);
259   } else {
260     EVT VT = OpVT.getVectorElementType();
261     Op = DAG.getNode(
262         ISD::EXTRACT_VECTOR_ELT, DL, VT, Op,
263         DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
264   }
265   return DAG.getNode(N->getOpcode(), SDLoc(N), DestVT, Op);
266 }
267
268 SDValue DAGTypeLegalizer::ScalarizeVecRes_InregOp(SDNode *N) {
269   EVT EltVT = N->getValueType(0).getVectorElementType();
270   EVT ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT().getVectorElementType();
271   SDValue LHS = GetScalarizedVector(N->getOperand(0));
272   return DAG.getNode(N->getOpcode(), SDLoc(N), EltVT,
273                      LHS, DAG.getValueType(ExtVT));
274 }
275
276 SDValue DAGTypeLegalizer::ScalarizeVecRes_SCALAR_TO_VECTOR(SDNode *N) {
277   // If the operand is wider than the vector element type then it is implicitly
278   // truncated.  Make that explicit here.
279   EVT EltVT = N->getValueType(0).getVectorElementType();
280   SDValue InOp = N->getOperand(0);
281   if (InOp.getValueType() != EltVT)
282     return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
283   return InOp;
284 }
285
286 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSELECT(SDNode *N) {
287   SDValue Cond = GetScalarizedVector(N->getOperand(0));
288   SDValue LHS = GetScalarizedVector(N->getOperand(1));
289   TargetLowering::BooleanContent ScalarBool =
290       TLI.getBooleanContents(false, false);
291   TargetLowering::BooleanContent VecBool = TLI.getBooleanContents(true, false);
292
293   // If integer and float booleans have different contents then we can't
294   // reliably optimize in all cases. There is a full explanation for this in
295   // DAGCombiner::visitSELECT() where the same issue affects folding
296   // (select C, 0, 1) to (xor C, 1).
297   if (TLI.getBooleanContents(false, false) !=
298       TLI.getBooleanContents(false, true)) {
299     // At least try the common case where the boolean is generated by a
300     // comparison.
301     if (Cond->getOpcode() == ISD::SETCC) {
302       EVT OpVT = Cond->getOperand(0)->getValueType(0);
303       ScalarBool = TLI.getBooleanContents(OpVT.getScalarType());
304       VecBool = TLI.getBooleanContents(OpVT);
305     } else
306       ScalarBool = TargetLowering::UndefinedBooleanContent;
307   }
308
309   if (ScalarBool != VecBool) {
310     EVT CondVT = Cond.getValueType();
311     switch (ScalarBool) {
312       case TargetLowering::UndefinedBooleanContent:
313         break;
314       case TargetLowering::ZeroOrOneBooleanContent:
315         assert(VecBool == TargetLowering::UndefinedBooleanContent ||
316                VecBool == TargetLowering::ZeroOrNegativeOneBooleanContent);
317         // Vector read from all ones, scalar expects a single 1 so mask.
318         Cond = DAG.getNode(ISD::AND, SDLoc(N), CondVT,
319                            Cond, DAG.getConstant(1, SDLoc(N), CondVT));
320         break;
321       case TargetLowering::ZeroOrNegativeOneBooleanContent:
322         assert(VecBool == TargetLowering::UndefinedBooleanContent ||
323                VecBool == TargetLowering::ZeroOrOneBooleanContent);
324         // Vector reads from a one, scalar from all ones so sign extend.
325         Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), CondVT,
326                            Cond, DAG.getValueType(MVT::i1));
327         break;
328     }
329   }
330
331   return DAG.getSelect(SDLoc(N),
332                        LHS.getValueType(), Cond, LHS,
333                        GetScalarizedVector(N->getOperand(2)));
334 }
335
336 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
337   SDValue LHS = GetScalarizedVector(N->getOperand(1));
338   return DAG.getSelect(SDLoc(N),
339                        LHS.getValueType(), N->getOperand(0), LHS,
340                        GetScalarizedVector(N->getOperand(2)));
341 }
342
343 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
344   SDValue LHS = GetScalarizedVector(N->getOperand(2));
345   return DAG.getNode(ISD::SELECT_CC, SDLoc(N), LHS.getValueType(),
346                      N->getOperand(0), N->getOperand(1),
347                      LHS, GetScalarizedVector(N->getOperand(3)),
348                      N->getOperand(4));
349 }
350
351 SDValue DAGTypeLegalizer::ScalarizeVecRes_SETCC(SDNode *N) {
352   assert(N->getValueType(0).isVector() ==
353          N->getOperand(0).getValueType().isVector() &&
354          "Scalar/Vector type mismatch");
355
356   if (N->getValueType(0).isVector()) return ScalarizeVecRes_VSETCC(N);
357
358   SDValue LHS = GetScalarizedVector(N->getOperand(0));
359   SDValue RHS = GetScalarizedVector(N->getOperand(1));
360   SDLoc DL(N);
361
362   // Turn it into a scalar SETCC.
363   return DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, N->getOperand(2));
364 }
365
366 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
367   return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
368 }
369
370 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
371   // Figure out if the scalar is the LHS or RHS and return it.
372   SDValue Arg = N->getOperand(2).getOperand(0);
373   if (Arg.isUndef())
374     return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
375   unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
376   return GetScalarizedVector(N->getOperand(Op));
377 }
378
379 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSETCC(SDNode *N) {
380   assert(N->getValueType(0).isVector() &&
381          N->getOperand(0).getValueType().isVector() &&
382          "Operand types must be vectors");
383   SDValue LHS = N->getOperand(0);
384   SDValue RHS = N->getOperand(1);
385   EVT OpVT = LHS.getValueType();
386   EVT NVT = N->getValueType(0).getVectorElementType();
387   SDLoc DL(N);
388
389   // The result needs scalarizing, but it's not a given that the source does.
390   if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
391     LHS = GetScalarizedVector(LHS);
392     RHS = GetScalarizedVector(RHS);
393   } else {
394     EVT VT = OpVT.getVectorElementType();
395     LHS = DAG.getNode(
396         ISD::EXTRACT_VECTOR_ELT, DL, VT, LHS,
397         DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
398     RHS = DAG.getNode(
399         ISD::EXTRACT_VECTOR_ELT, DL, VT, RHS,
400         DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
401   }
402
403   // Turn it into a scalar SETCC.
404   SDValue Res = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS,
405                             N->getOperand(2));
406   // Vectors may have a different boolean contents to scalars.  Promote the
407   // value appropriately.
408   ISD::NodeType ExtendCode =
409       TargetLowering::getExtendForContent(TLI.getBooleanContents(OpVT));
410   return DAG.getNode(ExtendCode, DL, NVT, Res);
411 }
412
413
414 //===----------------------------------------------------------------------===//
415 //  Operand Vector Scalarization <1 x ty> -> ty.
416 //===----------------------------------------------------------------------===//
417
418 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
419   DEBUG(dbgs() << "Scalarize node operand " << OpNo << ": ";
420         N->dump(&DAG);
421         dbgs() << "\n");
422   SDValue Res = SDValue();
423
424   if (!Res.getNode()) {
425     switch (N->getOpcode()) {
426     default:
427 #ifndef NDEBUG
428       dbgs() << "ScalarizeVectorOperand Op #" << OpNo << ": ";
429       N->dump(&DAG);
430       dbgs() << "\n";
431 #endif
432       llvm_unreachable("Do not know how to scalarize this operator's operand!");
433     case ISD::BITCAST:
434       Res = ScalarizeVecOp_BITCAST(N);
435       break;
436     case ISD::ANY_EXTEND:
437     case ISD::ZERO_EXTEND:
438     case ISD::SIGN_EXTEND:
439     case ISD::TRUNCATE:
440     case ISD::FP_TO_SINT:
441     case ISD::FP_TO_UINT:
442     case ISD::SINT_TO_FP:
443     case ISD::UINT_TO_FP:
444       Res = ScalarizeVecOp_UnaryOp(N);
445       break;
446     case ISD::CONCAT_VECTORS:
447       Res = ScalarizeVecOp_CONCAT_VECTORS(N);
448       break;
449     case ISD::EXTRACT_VECTOR_ELT:
450       Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N);
451       break;
452     case ISD::VSELECT:
453       Res = ScalarizeVecOp_VSELECT(N);
454       break;
455     case ISD::STORE:
456       Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo);
457       break;
458     case ISD::FP_ROUND:
459       Res = ScalarizeVecOp_FP_ROUND(N, OpNo);
460       break;
461     }
462   }
463
464   // If the result is null, the sub-method took care of registering results etc.
465   if (!Res.getNode()) return false;
466
467   // If the result is N, the sub-method updated N in place.  Tell the legalizer
468   // core about this.
469   if (Res.getNode() == N)
470     return true;
471
472   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
473          "Invalid operand expansion");
474
475   ReplaceValueWith(SDValue(N, 0), Res);
476   return false;
477 }
478
479 /// ScalarizeVecOp_BITCAST - If the value to convert is a vector that needs
480 /// to be scalarized, it must be <1 x ty>.  Convert the element instead.
481 SDValue DAGTypeLegalizer::ScalarizeVecOp_BITCAST(SDNode *N) {
482   SDValue Elt = GetScalarizedVector(N->getOperand(0));
483   return DAG.getNode(ISD::BITCAST, SDLoc(N),
484                      N->getValueType(0), Elt);
485 }
486
487 /// ScalarizeVecOp_UnaryOp - If the input is a vector that needs to be
488 /// scalarized, it must be <1 x ty>.  Do the operation on the element instead.
489 SDValue DAGTypeLegalizer::ScalarizeVecOp_UnaryOp(SDNode *N) {
490   assert(N->getValueType(0).getVectorNumElements() == 1 &&
491          "Unexpected vector type!");
492   SDValue Elt = GetScalarizedVector(N->getOperand(0));
493   SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N),
494                            N->getValueType(0).getScalarType(), Elt);
495   // Revectorize the result so the types line up with what the uses of this
496   // expression expect.
497   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), N->getValueType(0), Op);
498 }
499
500 /// ScalarizeVecOp_CONCAT_VECTORS - The vectors to concatenate have length one -
501 /// use a BUILD_VECTOR instead.
502 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
503   SmallVector<SDValue, 8> Ops(N->getNumOperands());
504   for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
505     Ops[i] = GetScalarizedVector(N->getOperand(i));
506   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), N->getValueType(0), Ops);
507 }
508
509 /// ScalarizeVecOp_EXTRACT_VECTOR_ELT - If the input is a vector that needs to
510 /// be scalarized, it must be <1 x ty>, so just return the element, ignoring the
511 /// index.
512 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
513   SDValue Res = GetScalarizedVector(N->getOperand(0));
514   if (Res.getValueType() != N->getValueType(0))
515     Res = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0),
516                       Res);
517   return Res;
518 }
519
520
521 /// ScalarizeVecOp_VSELECT - If the input condition is a vector that needs to be
522 /// scalarized, it must be <1 x i1>, so just convert to a normal ISD::SELECT
523 /// (still with vector output type since that was acceptable if we got here).
524 SDValue DAGTypeLegalizer::ScalarizeVecOp_VSELECT(SDNode *N) {
525   SDValue ScalarCond = GetScalarizedVector(N->getOperand(0));
526   EVT VT = N->getValueType(0);
527
528   return DAG.getNode(ISD::SELECT, SDLoc(N), VT, ScalarCond, N->getOperand(1),
529                      N->getOperand(2));
530 }
531
532 /// ScalarizeVecOp_STORE - If the value to store is a vector that needs to be
533 /// scalarized, it must be <1 x ty>.  Just store the element.
534 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
535   assert(N->isUnindexed() && "Indexed store of one-element vector?");
536   assert(OpNo == 1 && "Do not know how to scalarize this operand!");
537   SDLoc dl(N);
538
539   if (N->isTruncatingStore())
540     return DAG.getTruncStore(N->getChain(), dl,
541                              GetScalarizedVector(N->getOperand(1)),
542                              N->getBasePtr(), N->getPointerInfo(),
543                              N->getMemoryVT().getVectorElementType(),
544                              N->isVolatile(), N->isNonTemporal(),
545                              N->getAlignment(), N->getAAInfo());
546
547   return DAG.getStore(N->getChain(), dl, GetScalarizedVector(N->getOperand(1)),
548                       N->getBasePtr(), N->getPointerInfo(),
549                       N->isVolatile(), N->isNonTemporal(),
550                       N->getOriginalAlignment(), N->getAAInfo());
551 }
552
553 /// ScalarizeVecOp_FP_ROUND - If the value to round is a vector that needs
554 /// to be scalarized, it must be <1 x ty>.  Convert the element instead.
555 SDValue DAGTypeLegalizer::ScalarizeVecOp_FP_ROUND(SDNode *N, unsigned OpNo) {
556   SDValue Elt = GetScalarizedVector(N->getOperand(0));
557   SDValue Res = DAG.getNode(ISD::FP_ROUND, SDLoc(N),
558                             N->getValueType(0).getVectorElementType(), Elt,
559                             N->getOperand(1));
560   return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
561 }
562
563 //===----------------------------------------------------------------------===//
564 //  Result Vector Splitting
565 //===----------------------------------------------------------------------===//
566
567 /// SplitVectorResult - This method is called when the specified result of the
568 /// specified node is found to need vector splitting.  At this point, the node
569 /// may also have invalid operands or may have other results that need
570 /// legalization, we just know that (at least) one result needs vector
571 /// splitting.
572 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
573   DEBUG(dbgs() << "Split node result: ";
574         N->dump(&DAG);
575         dbgs() << "\n");
576   SDValue Lo, Hi;
577
578   // See if the target wants to custom expand this node.
579   if (CustomLowerNode(N, N->getValueType(ResNo), true))
580     return;
581
582   switch (N->getOpcode()) {
583   default:
584 #ifndef NDEBUG
585     dbgs() << "SplitVectorResult #" << ResNo << ": ";
586     N->dump(&DAG);
587     dbgs() << "\n";
588 #endif
589     report_fatal_error("Do not know how to split the result of this "
590                        "operator!\n");
591
592   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
593   case ISD::VSELECT:
594   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
595   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
596   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
597   case ISD::BITCAST:           SplitVecRes_BITCAST(N, Lo, Hi); break;
598   case ISD::BUILD_VECTOR:      SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
599   case ISD::CONCAT_VECTORS:    SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
600   case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
601   case ISD::INSERT_SUBVECTOR:  SplitVecRes_INSERT_SUBVECTOR(N, Lo, Hi); break;
602   case ISD::FP_ROUND_INREG:    SplitVecRes_InregOp(N, Lo, Hi); break;
603   case ISD::FPOWI:             SplitVecRes_FPOWI(N, Lo, Hi); break;
604   case ISD::FCOPYSIGN:         SplitVecRes_FCOPYSIGN(N, Lo, Hi); break;
605   case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
606   case ISD::SCALAR_TO_VECTOR:  SplitVecRes_SCALAR_TO_VECTOR(N, Lo, Hi); break;
607   case ISD::SIGN_EXTEND_INREG: SplitVecRes_InregOp(N, Lo, Hi); break;
608   case ISD::LOAD:
609     SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);
610     break;
611   case ISD::MLOAD:
612     SplitVecRes_MLOAD(cast<MaskedLoadSDNode>(N), Lo, Hi);
613     break;
614   case ISD::MGATHER:
615     SplitVecRes_MGATHER(cast<MaskedGatherSDNode>(N), Lo, Hi);
616     break;
617   case ISD::SETCC:
618     SplitVecRes_SETCC(N, Lo, Hi);
619     break;
620   case ISD::VECTOR_SHUFFLE:
621     SplitVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N), Lo, Hi);
622     break;
623
624   case ISD::ANY_EXTEND_VECTOR_INREG:
625   case ISD::SIGN_EXTEND_VECTOR_INREG:
626   case ISD::ZERO_EXTEND_VECTOR_INREG:
627     SplitVecRes_ExtVecInRegOp(N, Lo, Hi);
628     break;
629
630   case ISD::BITREVERSE:
631   case ISD::BSWAP:
632   case ISD::CONVERT_RNDSAT:
633   case ISD::CTLZ:
634   case ISD::CTTZ:
635   case ISD::CTLZ_ZERO_UNDEF:
636   case ISD::CTTZ_ZERO_UNDEF:
637   case ISD::CTPOP:
638   case ISD::FABS:
639   case ISD::FCEIL:
640   case ISD::FCOS:
641   case ISD::FEXP:
642   case ISD::FEXP2:
643   case ISD::FFLOOR:
644   case ISD::FLOG:
645   case ISD::FLOG10:
646   case ISD::FLOG2:
647   case ISD::FNEARBYINT:
648   case ISD::FNEG:
649   case ISD::FP_EXTEND:
650   case ISD::FP_ROUND:
651   case ISD::FP_TO_SINT:
652   case ISD::FP_TO_UINT:
653   case ISD::FRINT:
654   case ISD::FROUND:
655   case ISD::FSIN:
656   case ISD::FSQRT:
657   case ISD::FTRUNC:
658   case ISD::SINT_TO_FP:
659   case ISD::TRUNCATE:
660   case ISD::UINT_TO_FP:
661     SplitVecRes_UnaryOp(N, Lo, Hi);
662     break;
663
664   case ISD::ANY_EXTEND:
665   case ISD::SIGN_EXTEND:
666   case ISD::ZERO_EXTEND:
667     SplitVecRes_ExtendOp(N, Lo, Hi);
668     break;
669
670   case ISD::ADD:
671   case ISD::SUB:
672   case ISD::MUL:
673   case ISD::FADD:
674   case ISD::FSUB:
675   case ISD::FMUL:
676   case ISD::FMINNUM:
677   case ISD::FMAXNUM:
678   case ISD::FMINNAN:
679   case ISD::FMAXNAN:
680   case ISD::SDIV:
681   case ISD::UDIV:
682   case ISD::FDIV:
683   case ISD::FPOW:
684   case ISD::AND:
685   case ISD::OR:
686   case ISD::XOR:
687   case ISD::SHL:
688   case ISD::SRA:
689   case ISD::SRL:
690   case ISD::UREM:
691   case ISD::SREM:
692   case ISD::FREM:
693   case ISD::SMIN:
694   case ISD::SMAX:
695   case ISD::UMIN:
696   case ISD::UMAX:
697     SplitVecRes_BinOp(N, Lo, Hi);
698     break;
699   case ISD::FMA:
700     SplitVecRes_TernaryOp(N, Lo, Hi);
701     break;
702   }
703
704   // If Lo/Hi is null, the sub-method took care of registering results etc.
705   if (Lo.getNode())
706     SetSplitVector(SDValue(N, ResNo), Lo, Hi);
707 }
708
709 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
710                                          SDValue &Hi) {
711   SDValue LHSLo, LHSHi;
712   GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
713   SDValue RHSLo, RHSHi;
714   GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
715   SDLoc dl(N);
716
717   const SDNodeFlags *Flags = N->getFlags();
718   unsigned Opcode = N->getOpcode();
719   Lo = DAG.getNode(Opcode, dl, LHSLo.getValueType(), LHSLo, RHSLo, Flags);
720   Hi = DAG.getNode(Opcode, dl, LHSHi.getValueType(), LHSHi, RHSHi, Flags);
721 }
722
723 void DAGTypeLegalizer::SplitVecRes_TernaryOp(SDNode *N, SDValue &Lo,
724                                              SDValue &Hi) {
725   SDValue Op0Lo, Op0Hi;
726   GetSplitVector(N->getOperand(0), Op0Lo, Op0Hi);
727   SDValue Op1Lo, Op1Hi;
728   GetSplitVector(N->getOperand(1), Op1Lo, Op1Hi);
729   SDValue Op2Lo, Op2Hi;
730   GetSplitVector(N->getOperand(2), Op2Lo, Op2Hi);
731   SDLoc dl(N);
732
733   Lo = DAG.getNode(N->getOpcode(), dl, Op0Lo.getValueType(),
734                    Op0Lo, Op1Lo, Op2Lo);
735   Hi = DAG.getNode(N->getOpcode(), dl, Op0Hi.getValueType(),
736                    Op0Hi, Op1Hi, Op2Hi);
737 }
738
739 void DAGTypeLegalizer::SplitVecRes_BITCAST(SDNode *N, SDValue &Lo,
740                                            SDValue &Hi) {
741   // We know the result is a vector.  The input may be either a vector or a
742   // scalar value.
743   EVT LoVT, HiVT;
744   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
745   SDLoc dl(N);
746
747   SDValue InOp = N->getOperand(0);
748   EVT InVT = InOp.getValueType();
749
750   // Handle some special cases efficiently.
751   switch (getTypeAction(InVT)) {
752   case TargetLowering::TypeLegal:
753   case TargetLowering::TypePromoteInteger:
754   case TargetLowering::TypePromoteFloat:
755   case TargetLowering::TypeSoftenFloat:
756   case TargetLowering::TypeScalarizeVector:
757   case TargetLowering::TypeWidenVector:
758     break;
759   case TargetLowering::TypeExpandInteger:
760   case TargetLowering::TypeExpandFloat:
761     // A scalar to vector conversion, where the scalar needs expansion.
762     // If the vector is being split in two then we can just convert the
763     // expanded pieces.
764     if (LoVT == HiVT) {
765       GetExpandedOp(InOp, Lo, Hi);
766       if (DAG.getDataLayout().isBigEndian())
767         std::swap(Lo, Hi);
768       Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
769       Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
770       return;
771     }
772     break;
773   case TargetLowering::TypeSplitVector:
774     // If the input is a vector that needs to be split, convert each split
775     // piece of the input now.
776     GetSplitVector(InOp, Lo, Hi);
777     Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
778     Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
779     return;
780   }
781
782   // In the general case, convert the input to an integer and split it by hand.
783   EVT LoIntVT = EVT::getIntegerVT(*DAG.getContext(), LoVT.getSizeInBits());
784   EVT HiIntVT = EVT::getIntegerVT(*DAG.getContext(), HiVT.getSizeInBits());
785   if (DAG.getDataLayout().isBigEndian())
786     std::swap(LoIntVT, HiIntVT);
787
788   SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
789
790   if (DAG.getDataLayout().isBigEndian())
791     std::swap(Lo, Hi);
792   Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
793   Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
794 }
795
796 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
797                                                 SDValue &Hi) {
798   EVT LoVT, HiVT;
799   SDLoc dl(N);
800   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
801   unsigned LoNumElts = LoVT.getVectorNumElements();
802   SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
803   Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, LoVT, LoOps);
804
805   SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
806   Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, HiVT, HiOps);
807 }
808
809 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
810                                                   SDValue &Hi) {
811   assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
812   SDLoc dl(N);
813   unsigned NumSubvectors = N->getNumOperands() / 2;
814   if (NumSubvectors == 1) {
815     Lo = N->getOperand(0);
816     Hi = N->getOperand(1);
817     return;
818   }
819
820   EVT LoVT, HiVT;
821   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
822
823   SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
824   Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, LoVT, LoOps);
825
826   SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
827   Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HiVT, HiOps);
828 }
829
830 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
831                                                      SDValue &Hi) {
832   SDValue Vec = N->getOperand(0);
833   SDValue Idx = N->getOperand(1);
834   SDLoc dl(N);
835
836   EVT LoVT, HiVT;
837   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
838
839   Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, LoVT, Vec, Idx);
840   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
841   Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, HiVT, Vec,
842                    DAG.getConstant(IdxVal + LoVT.getVectorNumElements(), dl,
843                                    TLI.getVectorIdxTy(DAG.getDataLayout())));
844 }
845
846 void DAGTypeLegalizer::SplitVecRes_INSERT_SUBVECTOR(SDNode *N, SDValue &Lo,
847                                                     SDValue &Hi) {
848   SDValue Vec = N->getOperand(0);
849   SDValue SubVec = N->getOperand(1);
850   SDValue Idx = N->getOperand(2);
851   SDLoc dl(N);
852   GetSplitVector(Vec, Lo, Hi);
853
854   // Spill the vector to the stack.
855   EVT VecVT = Vec.getValueType();
856   EVT SubVecVT = VecVT.getVectorElementType();
857   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
858   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
859                                MachinePointerInfo(), false, false, 0);
860
861   // Store the new subvector into the specified index.
862   SDValue SubVecPtr = GetVectorElementPointer(StackPtr, SubVecVT, Idx);
863   Type *VecType = VecVT.getTypeForEVT(*DAG.getContext());
864   unsigned Alignment = DAG.getDataLayout().getPrefTypeAlignment(VecType);
865   Store = DAG.getStore(Store, dl, SubVec, SubVecPtr, MachinePointerInfo(),
866                        false, false, 0);
867
868   // Load the Lo part from the stack slot.
869   Lo = DAG.getLoad(Lo.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
870                    false, false, false, 0);
871
872   // Increment the pointer to the other part.
873   unsigned IncrementSize = Lo.getValueType().getSizeInBits() / 8;
874   StackPtr =
875       DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
876                   DAG.getConstant(IncrementSize, dl, StackPtr.getValueType()));
877
878   // Load the Hi part from the stack slot.
879   Hi = DAG.getLoad(Hi.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
880                    false, false, false, MinAlign(Alignment, IncrementSize));
881 }
882
883 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
884                                          SDValue &Hi) {
885   SDLoc dl(N);
886   GetSplitVector(N->getOperand(0), Lo, Hi);
887   Lo = DAG.getNode(ISD::FPOWI, dl, Lo.getValueType(), Lo, N->getOperand(1));
888   Hi = DAG.getNode(ISD::FPOWI, dl, Hi.getValueType(), Hi, N->getOperand(1));
889 }
890
891 void DAGTypeLegalizer::SplitVecRes_FCOPYSIGN(SDNode *N, SDValue &Lo,
892                                              SDValue &Hi) {
893   SDValue LHSLo, LHSHi;
894   GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
895   SDLoc DL(N);
896
897   SDValue RHSLo, RHSHi;
898   SDValue RHS = N->getOperand(1);
899   EVT RHSVT = RHS.getValueType();
900   if (getTypeAction(RHSVT) == TargetLowering::TypeSplitVector)
901     GetSplitVector(RHS, RHSLo, RHSHi);
902   else
903     std::tie(RHSLo, RHSHi) = DAG.SplitVector(RHS, SDLoc(RHS));
904
905
906   Lo = DAG.getNode(ISD::FCOPYSIGN, DL, LHSLo.getValueType(), LHSLo, RHSLo);
907   Hi = DAG.getNode(ISD::FCOPYSIGN, DL, LHSHi.getValueType(), LHSHi, RHSHi);
908 }
909
910 void DAGTypeLegalizer::SplitVecRes_InregOp(SDNode *N, SDValue &Lo,
911                                            SDValue &Hi) {
912   SDValue LHSLo, LHSHi;
913   GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
914   SDLoc dl(N);
915
916   EVT LoVT, HiVT;
917   std::tie(LoVT, HiVT) =
918     DAG.GetSplitDestVTs(cast<VTSDNode>(N->getOperand(1))->getVT());
919
920   Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo,
921                    DAG.getValueType(LoVT));
922   Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi,
923                    DAG.getValueType(HiVT));
924 }
925
926 void DAGTypeLegalizer::SplitVecRes_ExtVecInRegOp(SDNode *N, SDValue &Lo,
927                                                  SDValue &Hi) {
928   unsigned Opcode = N->getOpcode();
929   SDValue N0 = N->getOperand(0);
930
931   SDLoc dl(N);
932   SDValue InLo, InHi;
933   GetSplitVector(N0, InLo, InHi);
934   EVT InLoVT = InLo.getValueType();
935   unsigned InNumElements = InLoVT.getVectorNumElements();
936
937   EVT OutLoVT, OutHiVT;
938   std::tie(OutLoVT, OutHiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
939   unsigned OutNumElements = OutLoVT.getVectorNumElements();
940   assert((2 * OutNumElements) <= InNumElements &&
941          "Illegal extend vector in reg split");
942
943   // *_EXTEND_VECTOR_INREG instructions extend the lowest elements of the
944   // input vector (i.e. we only use InLo):
945   // OutLo will extend the first OutNumElements from InLo.
946   // OutHi will extend the next OutNumElements from InLo.
947
948   // Shuffle the elements from InLo for OutHi into the bottom elements to
949   // create a 'fake' InHi.
950   SmallVector<int, 8> SplitHi(InNumElements, -1);
951   for (unsigned i = 0; i != OutNumElements; ++i)
952     SplitHi[i] = i + OutNumElements;
953   InHi = DAG.getVectorShuffle(InLoVT, dl, InLo, DAG.getUNDEF(InLoVT), SplitHi);
954
955   Lo = DAG.getNode(Opcode, dl, OutLoVT, InLo);
956   Hi = DAG.getNode(Opcode, dl, OutHiVT, InHi);
957 }
958
959 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
960                                                      SDValue &Hi) {
961   SDValue Vec = N->getOperand(0);
962   SDValue Elt = N->getOperand(1);
963   SDValue Idx = N->getOperand(2);
964   SDLoc dl(N);
965   GetSplitVector(Vec, Lo, Hi);
966
967   if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
968     unsigned IdxVal = CIdx->getZExtValue();
969     unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
970     if (IdxVal < LoNumElts)
971       Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
972                        Lo.getValueType(), Lo, Elt, Idx);
973     else
974       Hi =
975           DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, Hi.getValueType(), Hi, Elt,
976                       DAG.getConstant(IdxVal - LoNumElts, dl,
977                                       TLI.getVectorIdxTy(DAG.getDataLayout())));
978     return;
979   }
980
981   // See if the target wants to custom expand this node.
982   if (CustomLowerNode(N, N->getValueType(0), true))
983     return;
984
985   // Spill the vector to the stack.
986   EVT VecVT = Vec.getValueType();
987   EVT EltVT = VecVT.getVectorElementType();
988   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
989   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
990                                MachinePointerInfo(), false, false, 0);
991
992   // Store the new element.  This may be larger than the vector element type,
993   // so use a truncating store.
994   SDValue EltPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
995   Type *VecType = VecVT.getTypeForEVT(*DAG.getContext());
996   unsigned Alignment = DAG.getDataLayout().getPrefTypeAlignment(VecType);
997   Store = DAG.getTruncStore(Store, dl, Elt, EltPtr, MachinePointerInfo(), EltVT,
998                             false, false, 0);
999
1000   // Load the Lo part from the stack slot.
1001   Lo = DAG.getLoad(Lo.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
1002                    false, false, false, 0);
1003
1004   // Increment the pointer to the other part.
1005   unsigned IncrementSize = Lo.getValueType().getSizeInBits() / 8;
1006   StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
1007                          DAG.getConstant(IncrementSize, dl,
1008                                          StackPtr.getValueType()));
1009
1010   // Load the Hi part from the stack slot.
1011   Hi = DAG.getLoad(Hi.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
1012                    false, false, false, MinAlign(Alignment, IncrementSize));
1013 }
1014
1015 void DAGTypeLegalizer::SplitVecRes_SCALAR_TO_VECTOR(SDNode *N, SDValue &Lo,
1016                                                     SDValue &Hi) {
1017   EVT LoVT, HiVT;
1018   SDLoc dl(N);
1019   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1020   Lo = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoVT, N->getOperand(0));
1021   Hi = DAG.getUNDEF(HiVT);
1022 }
1023
1024 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
1025                                         SDValue &Hi) {
1026   assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
1027   EVT LoVT, HiVT;
1028   SDLoc dl(LD);
1029   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(LD->getValueType(0));
1030
1031   ISD::LoadExtType ExtType = LD->getExtensionType();
1032   SDValue Ch = LD->getChain();
1033   SDValue Ptr = LD->getBasePtr();
1034   SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
1035   EVT MemoryVT = LD->getMemoryVT();
1036   unsigned Alignment = LD->getOriginalAlignment();
1037   bool isVolatile = LD->isVolatile();
1038   bool isNonTemporal = LD->isNonTemporal();
1039   bool isInvariant = LD->isInvariant();
1040   AAMDNodes AAInfo = LD->getAAInfo();
1041
1042   EVT LoMemVT, HiMemVT;
1043   std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1044
1045   Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, dl, Ch, Ptr, Offset,
1046                    LD->getPointerInfo(), LoMemVT, isVolatile, isNonTemporal,
1047                    isInvariant, Alignment, AAInfo);
1048
1049   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
1050   Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1051                     DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
1052   Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, dl, Ch, Ptr, Offset,
1053                    LD->getPointerInfo().getWithOffset(IncrementSize),
1054                    HiMemVT, isVolatile, isNonTemporal, isInvariant, Alignment,
1055                    AAInfo);
1056
1057   // Build a factor node to remember that this load is independent of the
1058   // other one.
1059   Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1060                    Hi.getValue(1));
1061
1062   // Legalize the chain result - switch anything that used the old chain to
1063   // use the new one.
1064   ReplaceValueWith(SDValue(LD, 1), Ch);
1065 }
1066
1067 void DAGTypeLegalizer::SplitVecRes_MLOAD(MaskedLoadSDNode *MLD,
1068                                          SDValue &Lo, SDValue &Hi) {
1069   EVT LoVT, HiVT;
1070   SDLoc dl(MLD);
1071   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MLD->getValueType(0));
1072
1073   SDValue Ch = MLD->getChain();
1074   SDValue Ptr = MLD->getBasePtr();
1075   SDValue Mask = MLD->getMask();
1076   SDValue Src0 = MLD->getSrc0();
1077   unsigned Alignment = MLD->getOriginalAlignment();
1078   ISD::LoadExtType ExtType = MLD->getExtensionType();
1079
1080   // if Alignment is equal to the vector size,
1081   // take the half of it for the second part
1082   unsigned SecondHalfAlignment =
1083     (Alignment == MLD->getValueType(0).getSizeInBits()/8) ?
1084      Alignment/2 : Alignment;
1085
1086   // Split Mask operand
1087   SDValue MaskLo, MaskHi;
1088   if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
1089     GetSplitVector(Mask, MaskLo, MaskHi);
1090   else
1091     std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
1092
1093   EVT MemoryVT = MLD->getMemoryVT();
1094   EVT LoMemVT, HiMemVT;
1095   std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1096
1097   SDValue Src0Lo, Src0Hi;
1098   if (getTypeAction(Src0.getValueType()) == TargetLowering::TypeSplitVector)
1099     GetSplitVector(Src0, Src0Lo, Src0Hi);
1100   else
1101     std::tie(Src0Lo, Src0Hi) = DAG.SplitVector(Src0, dl);
1102
1103   MachineMemOperand *MMO = DAG.getMachineFunction().
1104     getMachineMemOperand(MLD->getPointerInfo(),
1105                          MachineMemOperand::MOLoad,  LoMemVT.getStoreSize(),
1106                          Alignment, MLD->getAAInfo(), MLD->getRanges());
1107
1108   Lo = DAG.getMaskedLoad(LoVT, dl, Ch, Ptr, MaskLo, Src0Lo, LoMemVT, MMO,
1109                          ExtType);
1110
1111   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
1112   Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1113                     DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
1114
1115   MMO = DAG.getMachineFunction().
1116     getMachineMemOperand(MLD->getPointerInfo(),
1117                          MachineMemOperand::MOLoad,  HiMemVT.getStoreSize(),
1118                          SecondHalfAlignment, MLD->getAAInfo(), MLD->getRanges());
1119
1120   Hi = DAG.getMaskedLoad(HiVT, dl, Ch, Ptr, MaskHi, Src0Hi, HiMemVT, MMO,
1121                          ExtType);
1122
1123
1124   // Build a factor node to remember that this load is independent of the
1125   // other one.
1126   Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1127                    Hi.getValue(1));
1128
1129   // Legalize the chain result - switch anything that used the old chain to
1130   // use the new one.
1131   ReplaceValueWith(SDValue(MLD, 1), Ch);
1132
1133 }
1134
1135 void DAGTypeLegalizer::SplitVecRes_MGATHER(MaskedGatherSDNode *MGT,
1136                                          SDValue &Lo, SDValue &Hi) {
1137   EVT LoVT, HiVT;
1138   SDLoc dl(MGT);
1139   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MGT->getValueType(0));
1140
1141   SDValue Ch = MGT->getChain();
1142   SDValue Ptr = MGT->getBasePtr();
1143   SDValue Mask = MGT->getMask();
1144   SDValue Src0 = MGT->getValue();
1145   SDValue Index = MGT->getIndex();
1146   unsigned Alignment = MGT->getOriginalAlignment();
1147
1148   // Split Mask operand
1149   SDValue MaskLo, MaskHi;
1150   if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
1151     GetSplitVector(Mask, MaskLo, MaskHi);
1152   else
1153     std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
1154
1155   EVT MemoryVT = MGT->getMemoryVT();
1156   EVT LoMemVT, HiMemVT;
1157   // Split MemoryVT
1158   std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1159
1160   SDValue Src0Lo, Src0Hi;
1161   if (getTypeAction(Src0.getValueType()) == TargetLowering::TypeSplitVector)
1162     GetSplitVector(Src0, Src0Lo, Src0Hi);
1163   else
1164     std::tie(Src0Lo, Src0Hi) = DAG.SplitVector(Src0, dl);
1165
1166   SDValue IndexHi, IndexLo;
1167   if (getTypeAction(Index.getValueType()) == TargetLowering::TypeSplitVector)
1168     GetSplitVector(Index, IndexLo, IndexHi);
1169   else
1170     std::tie(IndexLo, IndexHi) = DAG.SplitVector(Index, dl);
1171
1172   MachineMemOperand *MMO = DAG.getMachineFunction().
1173     getMachineMemOperand(MGT->getPointerInfo(),
1174                          MachineMemOperand::MOLoad,  LoMemVT.getStoreSize(),
1175                          Alignment, MGT->getAAInfo(), MGT->getRanges());
1176
1177   SDValue OpsLo[] = {Ch, Src0Lo, MaskLo, Ptr, IndexLo};
1178   Lo = DAG.getMaskedGather(DAG.getVTList(LoVT, MVT::Other), LoVT, dl, OpsLo,
1179                            MMO);
1180
1181   SDValue OpsHi[] = {Ch, Src0Hi, MaskHi, Ptr, IndexHi};
1182   Hi = DAG.getMaskedGather(DAG.getVTList(HiVT, MVT::Other), HiVT, dl, OpsHi,
1183                            MMO);
1184
1185   // Build a factor node to remember that this load is independent of the
1186   // other one.
1187   Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1188                    Hi.getValue(1));
1189
1190   // Legalize the chain result - switch anything that used the old chain to
1191   // use the new one.
1192   ReplaceValueWith(SDValue(MGT, 1), Ch);
1193 }
1194
1195
1196 void DAGTypeLegalizer::SplitVecRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
1197   assert(N->getValueType(0).isVector() &&
1198          N->getOperand(0).getValueType().isVector() &&
1199          "Operand types must be vectors");
1200
1201   EVT LoVT, HiVT;
1202   SDLoc DL(N);
1203   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1204
1205   // Split the input.
1206   SDValue LL, LH, RL, RH;
1207   std::tie(LL, LH) = DAG.SplitVectorOperand(N, 0);
1208   std::tie(RL, RH) = DAG.SplitVectorOperand(N, 1);
1209
1210   Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
1211   Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
1212 }
1213
1214 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
1215                                            SDValue &Hi) {
1216   // Get the dest types - they may not match the input types, e.g. int_to_fp.
1217   EVT LoVT, HiVT;
1218   SDLoc dl(N);
1219   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1220
1221   // If the input also splits, handle it directly for a compile time speedup.
1222   // Otherwise split it by hand.
1223   EVT InVT = N->getOperand(0).getValueType();
1224   if (getTypeAction(InVT) == TargetLowering::TypeSplitVector)
1225     GetSplitVector(N->getOperand(0), Lo, Hi);
1226   else
1227     std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0);
1228
1229   if (N->getOpcode() == ISD::FP_ROUND) {
1230     Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo, N->getOperand(1));
1231     Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi, N->getOperand(1));
1232   } else if (N->getOpcode() == ISD::CONVERT_RNDSAT) {
1233     SDValue DTyOpLo = DAG.getValueType(LoVT);
1234     SDValue DTyOpHi = DAG.getValueType(HiVT);
1235     SDValue STyOpLo = DAG.getValueType(Lo.getValueType());
1236     SDValue STyOpHi = DAG.getValueType(Hi.getValueType());
1237     SDValue RndOp = N->getOperand(3);
1238     SDValue SatOp = N->getOperand(4);
1239     ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
1240     Lo = DAG.getConvertRndSat(LoVT, dl, Lo, DTyOpLo, STyOpLo, RndOp, SatOp,
1241                               CvtCode);
1242     Hi = DAG.getConvertRndSat(HiVT, dl, Hi, DTyOpHi, STyOpHi, RndOp, SatOp,
1243                               CvtCode);
1244   } else {
1245     Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo);
1246     Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi);
1247   }
1248 }
1249
1250 void DAGTypeLegalizer::SplitVecRes_ExtendOp(SDNode *N, SDValue &Lo,
1251                                             SDValue &Hi) {
1252   SDLoc dl(N);
1253   EVT SrcVT = N->getOperand(0).getValueType();
1254   EVT DestVT = N->getValueType(0);
1255   EVT LoVT, HiVT;
1256   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(DestVT);
1257
1258   // We can do better than a generic split operation if the extend is doing
1259   // more than just doubling the width of the elements and the following are
1260   // true:
1261   //   - The number of vector elements is even,
1262   //   - the source type is legal,
1263   //   - the type of a split source is illegal,
1264   //   - the type of an extended (by doubling element size) source is legal, and
1265   //   - the type of that extended source when split is legal.
1266   //
1267   // This won't necessarily completely legalize the operation, but it will
1268   // more effectively move in the right direction and prevent falling down
1269   // to scalarization in many cases due to the input vector being split too
1270   // far.
1271   unsigned NumElements = SrcVT.getVectorNumElements();
1272   if ((NumElements & 1) == 0 &&
1273       SrcVT.getSizeInBits() * 2 < DestVT.getSizeInBits()) {
1274     LLVMContext &Ctx = *DAG.getContext();
1275     EVT NewSrcVT = EVT::getVectorVT(
1276         Ctx, EVT::getIntegerVT(
1277                  Ctx, SrcVT.getVectorElementType().getSizeInBits() * 2),
1278         NumElements);
1279     EVT SplitSrcVT =
1280         EVT::getVectorVT(Ctx, SrcVT.getVectorElementType(), NumElements / 2);
1281     EVT SplitLoVT, SplitHiVT;
1282     std::tie(SplitLoVT, SplitHiVT) = DAG.GetSplitDestVTs(NewSrcVT);
1283     if (TLI.isTypeLegal(SrcVT) && !TLI.isTypeLegal(SplitSrcVT) &&
1284         TLI.isTypeLegal(NewSrcVT) && TLI.isTypeLegal(SplitLoVT)) {
1285       DEBUG(dbgs() << "Split vector extend via incremental extend:";
1286             N->dump(&DAG); dbgs() << "\n");
1287       // Extend the source vector by one step.
1288       SDValue NewSrc =
1289           DAG.getNode(N->getOpcode(), dl, NewSrcVT, N->getOperand(0));
1290       // Get the low and high halves of the new, extended one step, vector.
1291       std::tie(Lo, Hi) = DAG.SplitVector(NewSrc, dl);
1292       // Extend those vector halves the rest of the way.
1293       Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo);
1294       Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi);
1295       return;
1296     }
1297   }
1298   // Fall back to the generic unary operator splitting otherwise.
1299   SplitVecRes_UnaryOp(N, Lo, Hi);
1300 }
1301
1302 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
1303                                                   SDValue &Lo, SDValue &Hi) {
1304   // The low and high parts of the original input give four input vectors.
1305   SDValue Inputs[4];
1306   SDLoc dl(N);
1307   GetSplitVector(N->getOperand(0), Inputs[0], Inputs[1]);
1308   GetSplitVector(N->getOperand(1), Inputs[2], Inputs[3]);
1309   EVT NewVT = Inputs[0].getValueType();
1310   unsigned NewElts = NewVT.getVectorNumElements();
1311
1312   // If Lo or Hi uses elements from at most two of the four input vectors, then
1313   // express it as a vector shuffle of those two inputs.  Otherwise extract the
1314   // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR.
1315   SmallVector<int, 16> Ops;
1316   for (unsigned High = 0; High < 2; ++High) {
1317     SDValue &Output = High ? Hi : Lo;
1318
1319     // Build a shuffle mask for the output, discovering on the fly which
1320     // input vectors to use as shuffle operands (recorded in InputUsed).
1321     // If building a suitable shuffle vector proves too hard, then bail
1322     // out with useBuildVector set.
1323     unsigned InputUsed[2] = { -1U, -1U }; // Not yet discovered.
1324     unsigned FirstMaskIdx = High * NewElts;
1325     bool useBuildVector = false;
1326     for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
1327       // The mask element.  This indexes into the input.
1328       int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
1329
1330       // The input vector this mask element indexes into.
1331       unsigned Input = (unsigned)Idx / NewElts;
1332
1333       if (Input >= array_lengthof(Inputs)) {
1334         // The mask element does not index into any input vector.
1335         Ops.push_back(-1);
1336         continue;
1337       }
1338
1339       // Turn the index into an offset from the start of the input vector.
1340       Idx -= Input * NewElts;
1341
1342       // Find or create a shuffle vector operand to hold this input.
1343       unsigned OpNo;
1344       for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
1345         if (InputUsed[OpNo] == Input) {
1346           // This input vector is already an operand.
1347           break;
1348         } else if (InputUsed[OpNo] == -1U) {
1349           // Create a new operand for this input vector.
1350           InputUsed[OpNo] = Input;
1351           break;
1352         }
1353       }
1354
1355       if (OpNo >= array_lengthof(InputUsed)) {
1356         // More than two input vectors used!  Give up on trying to create a
1357         // shuffle vector.  Insert all elements into a BUILD_VECTOR instead.
1358         useBuildVector = true;
1359         break;
1360       }
1361
1362       // Add the mask index for the new shuffle vector.
1363       Ops.push_back(Idx + OpNo * NewElts);
1364     }
1365
1366     if (useBuildVector) {
1367       EVT EltVT = NewVT.getVectorElementType();
1368       SmallVector<SDValue, 16> SVOps;
1369
1370       // Extract the input elements by hand.
1371       for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
1372         // The mask element.  This indexes into the input.
1373         int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
1374
1375         // The input vector this mask element indexes into.
1376         unsigned Input = (unsigned)Idx / NewElts;
1377
1378         if (Input >= array_lengthof(Inputs)) {
1379           // The mask element is "undef" or indexes off the end of the input.
1380           SVOps.push_back(DAG.getUNDEF(EltVT));
1381           continue;
1382         }
1383
1384         // Turn the index into an offset from the start of the input vector.
1385         Idx -= Input * NewElts;
1386
1387         // Extract the vector element by hand.
1388         SVOps.push_back(DAG.getNode(
1389             ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Inputs[Input],
1390             DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
1391       }
1392
1393       // Construct the Lo/Hi output using a BUILD_VECTOR.
1394       Output = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT, SVOps);
1395     } else if (InputUsed[0] == -1U) {
1396       // No input vectors were used!  The result is undefined.
1397       Output = DAG.getUNDEF(NewVT);
1398     } else {
1399       SDValue Op0 = Inputs[InputUsed[0]];
1400       // If only one input was used, use an undefined vector for the other.
1401       SDValue Op1 = InputUsed[1] == -1U ?
1402         DAG.getUNDEF(NewVT) : Inputs[InputUsed[1]];
1403       // At least one input vector was used.  Create a new shuffle vector.
1404       Output =  DAG.getVectorShuffle(NewVT, dl, Op0, Op1, &Ops[0]);
1405     }
1406
1407     Ops.clear();
1408   }
1409 }
1410
1411
1412 //===----------------------------------------------------------------------===//
1413 //  Operand Vector Splitting
1414 //===----------------------------------------------------------------------===//
1415
1416 /// SplitVectorOperand - This method is called when the specified operand of the
1417 /// specified node is found to need vector splitting.  At this point, all of the
1418 /// result types of the node are known to be legal, but other operands of the
1419 /// node may need legalization as well as the specified one.
1420 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
1421   DEBUG(dbgs() << "Split node operand: ";
1422         N->dump(&DAG);
1423         dbgs() << "\n");
1424   SDValue Res = SDValue();
1425
1426   // See if the target wants to custom split this node.
1427   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1428     return false;
1429
1430   if (!Res.getNode()) {
1431     switch (N->getOpcode()) {
1432     default:
1433 #ifndef NDEBUG
1434       dbgs() << "SplitVectorOperand Op #" << OpNo << ": ";
1435       N->dump(&DAG);
1436       dbgs() << "\n";
1437 #endif
1438       report_fatal_error("Do not know how to split this operator's "
1439                          "operand!\n");
1440
1441     case ISD::SETCC:             Res = SplitVecOp_VSETCC(N); break;
1442     case ISD::BITCAST:           Res = SplitVecOp_BITCAST(N); break;
1443     case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
1444     case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
1445     case ISD::CONCAT_VECTORS:    Res = SplitVecOp_CONCAT_VECTORS(N); break;
1446     case ISD::TRUNCATE:
1447       Res = SplitVecOp_TruncateHelper(N);
1448       break;
1449     case ISD::FP_ROUND:          Res = SplitVecOp_FP_ROUND(N); break;
1450     case ISD::FCOPYSIGN:         Res = SplitVecOp_FCOPYSIGN(N); break;
1451     case ISD::STORE:
1452       Res = SplitVecOp_STORE(cast<StoreSDNode>(N), OpNo);
1453       break;
1454     case ISD::MSTORE:
1455       Res = SplitVecOp_MSTORE(cast<MaskedStoreSDNode>(N), OpNo);
1456       break;
1457     case ISD::MSCATTER:
1458       Res = SplitVecOp_MSCATTER(cast<MaskedScatterSDNode>(N), OpNo);
1459       break;
1460     case ISD::MGATHER:
1461       Res = SplitVecOp_MGATHER(cast<MaskedGatherSDNode>(N), OpNo);
1462       break;
1463     case ISD::VSELECT:
1464       Res = SplitVecOp_VSELECT(N, OpNo);
1465       break;
1466     case ISD::FP_TO_SINT:
1467     case ISD::FP_TO_UINT:
1468       if (N->getValueType(0).bitsLT(N->getOperand(0)->getValueType(0)))
1469         Res = SplitVecOp_TruncateHelper(N);
1470       else
1471         Res = SplitVecOp_UnaryOp(N);
1472       break;
1473     case ISD::SINT_TO_FP:
1474     case ISD::UINT_TO_FP:
1475       if (N->getValueType(0).bitsLT(N->getOperand(0)->getValueType(0)))
1476         Res = SplitVecOp_TruncateHelper(N);
1477       else
1478         Res = SplitVecOp_UnaryOp(N);
1479       break;
1480     case ISD::CTTZ:
1481     case ISD::CTLZ:
1482     case ISD::CTPOP:
1483     case ISD::FP_EXTEND:
1484     case ISD::SIGN_EXTEND:
1485     case ISD::ZERO_EXTEND:
1486     case ISD::ANY_EXTEND:
1487     case ISD::FTRUNC:
1488       Res = SplitVecOp_UnaryOp(N);
1489       break;
1490     }
1491   }
1492
1493   // If the result is null, the sub-method took care of registering results etc.
1494   if (!Res.getNode()) return false;
1495
1496   // If the result is N, the sub-method updated N in place.  Tell the legalizer
1497   // core about this.
1498   if (Res.getNode() == N)
1499     return true;
1500
1501   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1502          "Invalid operand expansion");
1503
1504   ReplaceValueWith(SDValue(N, 0), Res);
1505   return false;
1506 }
1507
1508 SDValue DAGTypeLegalizer::SplitVecOp_VSELECT(SDNode *N, unsigned OpNo) {
1509   // The only possibility for an illegal operand is the mask, since result type
1510   // legalization would have handled this node already otherwise.
1511   assert(OpNo == 0 && "Illegal operand must be mask");
1512
1513   SDValue Mask = N->getOperand(0);
1514   SDValue Src0 = N->getOperand(1);
1515   SDValue Src1 = N->getOperand(2);
1516   EVT Src0VT = Src0.getValueType();
1517   SDLoc DL(N);
1518   assert(Mask.getValueType().isVector() && "VSELECT without a vector mask?");
1519
1520   SDValue Lo, Hi;
1521   GetSplitVector(N->getOperand(0), Lo, Hi);
1522   assert(Lo.getValueType() == Hi.getValueType() &&
1523          "Lo and Hi have differing types");
1524
1525   EVT LoOpVT, HiOpVT;
1526   std::tie(LoOpVT, HiOpVT) = DAG.GetSplitDestVTs(Src0VT);
1527   assert(LoOpVT == HiOpVT && "Asymmetric vector split?");
1528
1529   SDValue LoOp0, HiOp0, LoOp1, HiOp1, LoMask, HiMask;
1530   std::tie(LoOp0, HiOp0) = DAG.SplitVector(Src0, DL);
1531   std::tie(LoOp1, HiOp1) = DAG.SplitVector(Src1, DL);
1532   std::tie(LoMask, HiMask) = DAG.SplitVector(Mask, DL);
1533
1534   SDValue LoSelect =
1535     DAG.getNode(ISD::VSELECT, DL, LoOpVT, LoMask, LoOp0, LoOp1);
1536   SDValue HiSelect =
1537     DAG.getNode(ISD::VSELECT, DL, HiOpVT, HiMask, HiOp0, HiOp1);
1538
1539   return DAG.getNode(ISD::CONCAT_VECTORS, DL, Src0VT, LoSelect, HiSelect);
1540 }
1541
1542 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
1543   // The result has a legal vector type, but the input needs splitting.
1544   EVT ResVT = N->getValueType(0);
1545   SDValue Lo, Hi;
1546   SDLoc dl(N);
1547   GetSplitVector(N->getOperand(0), Lo, Hi);
1548   EVT InVT = Lo.getValueType();
1549
1550   EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
1551                                InVT.getVectorNumElements());
1552
1553   Lo = DAG.getNode(N->getOpcode(), dl, OutVT, Lo);
1554   Hi = DAG.getNode(N->getOpcode(), dl, OutVT, Hi);
1555
1556   return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
1557 }
1558
1559 SDValue DAGTypeLegalizer::SplitVecOp_BITCAST(SDNode *N) {
1560   // For example, i64 = BITCAST v4i16 on alpha.  Typically the vector will
1561   // end up being split all the way down to individual components.  Convert the
1562   // split pieces into integers and reassemble.
1563   SDValue Lo, Hi;
1564   GetSplitVector(N->getOperand(0), Lo, Hi);
1565   Lo = BitConvertToInteger(Lo);
1566   Hi = BitConvertToInteger(Hi);
1567
1568   if (DAG.getDataLayout().isBigEndian())
1569     std::swap(Lo, Hi);
1570
1571   return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
1572                      JoinIntegers(Lo, Hi));
1573 }
1574
1575 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
1576   // We know that the extracted result type is legal.
1577   EVT SubVT = N->getValueType(0);
1578   SDValue Idx = N->getOperand(1);
1579   SDLoc dl(N);
1580   SDValue Lo, Hi;
1581   GetSplitVector(N->getOperand(0), Lo, Hi);
1582
1583   uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1584   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1585
1586   if (IdxVal < LoElts) {
1587     assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
1588            "Extracted subvector crosses vector split!");
1589     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Lo, Idx);
1590   } else {
1591     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Hi,
1592                        DAG.getConstant(IdxVal - LoElts, dl,
1593                                        Idx.getValueType()));
1594   }
1595 }
1596
1597 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
1598   SDValue Vec = N->getOperand(0);
1599   SDValue Idx = N->getOperand(1);
1600   EVT VecVT = Vec.getValueType();
1601
1602   if (isa<ConstantSDNode>(Idx)) {
1603     uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1604     assert(IdxVal < VecVT.getVectorNumElements() && "Invalid vector index!");
1605
1606     SDValue Lo, Hi;
1607     GetSplitVector(Vec, Lo, Hi);
1608
1609     uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1610
1611     if (IdxVal < LoElts)
1612       return SDValue(DAG.UpdateNodeOperands(N, Lo, Idx), 0);
1613     return SDValue(DAG.UpdateNodeOperands(N, Hi,
1614                                   DAG.getConstant(IdxVal - LoElts, SDLoc(N),
1615                                                   Idx.getValueType())), 0);
1616   }
1617
1618   // See if the target wants to custom expand this node.
1619   if (CustomLowerNode(N, N->getValueType(0), true))
1620     return SDValue();
1621
1622   // Make the vector elements byte-addressable if they aren't already.
1623   SDLoc dl(N);
1624   EVT EltVT = VecVT.getVectorElementType();
1625   if (EltVT.getSizeInBits() < 8) {
1626     SmallVector<SDValue, 4> ElementOps;
1627     for (unsigned i = 0; i < VecVT.getVectorNumElements(); ++i) {
1628       ElementOps.push_back(DAG.getAnyExtOrTrunc(
1629           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Vec,
1630                       DAG.getConstant(i, dl, MVT::i8)),
1631           dl, MVT::i8));
1632     }
1633
1634     EltVT = MVT::i8;
1635     VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT,
1636                              VecVT.getVectorNumElements());
1637     Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, ElementOps);
1638   }
1639
1640   // Store the vector to the stack.
1641   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
1642   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1643                                MachinePointerInfo(), false, false, 0);
1644
1645   // Load back the required element.
1646   StackPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
1647   return DAG.getExtLoad(ISD::EXTLOAD, dl, N->getValueType(0), Store, StackPtr,
1648                         MachinePointerInfo(), EltVT, false, false, false, 0);
1649 }
1650
1651 SDValue DAGTypeLegalizer::SplitVecOp_MGATHER(MaskedGatherSDNode *MGT,
1652                                              unsigned OpNo) {
1653   EVT LoVT, HiVT;
1654   SDLoc dl(MGT);
1655   std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MGT->getValueType(0));
1656
1657   SDValue Ch = MGT->getChain();
1658   SDValue Ptr = MGT->getBasePtr();
1659   SDValue Index = MGT->getIndex();
1660   SDValue Mask = MGT->getMask();
1661   SDValue Src0 = MGT->getValue();
1662   unsigned Alignment = MGT->getOriginalAlignment();
1663
1664   SDValue MaskLo, MaskHi;
1665   if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
1666     // Split Mask operand
1667     GetSplitVector(Mask, MaskLo, MaskHi);
1668   else
1669     std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
1670
1671   EVT MemoryVT = MGT->getMemoryVT();
1672   EVT LoMemVT, HiMemVT;
1673   std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1674
1675   SDValue Src0Lo, Src0Hi;
1676   if (getTypeAction(Src0.getValueType()) == TargetLowering::TypeSplitVector)
1677     GetSplitVector(Src0, Src0Lo, Src0Hi);
1678   else
1679     std::tie(Src0Lo, Src0Hi) = DAG.SplitVector(Src0, dl);
1680
1681   SDValue IndexHi, IndexLo;
1682   if (getTypeAction(Index.getValueType()) == TargetLowering::TypeSplitVector)
1683     GetSplitVector(Index, IndexLo, IndexHi);
1684   else
1685     std::tie(IndexLo, IndexHi) = DAG.SplitVector(Index, dl);
1686
1687   MachineMemOperand *MMO = DAG.getMachineFunction().
1688     getMachineMemOperand(MGT->getPointerInfo(),
1689                          MachineMemOperand::MOLoad,  LoMemVT.getStoreSize(),
1690                          Alignment, MGT->getAAInfo(), MGT->getRanges());
1691
1692   SDValue OpsLo[] = {Ch, Src0Lo, MaskLo, Ptr, IndexLo};
1693   SDValue Lo = DAG.getMaskedGather(DAG.getVTList(LoVT, MVT::Other), LoVT, dl,
1694                                    OpsLo, MMO);
1695
1696   MMO = DAG.getMachineFunction().
1697     getMachineMemOperand(MGT->getPointerInfo(),
1698                          MachineMemOperand::MOLoad,  HiMemVT.getStoreSize(),
1699                          Alignment, MGT->getAAInfo(),
1700                          MGT->getRanges());
1701
1702   SDValue OpsHi[] = {Ch, Src0Hi, MaskHi, Ptr, IndexHi};
1703   SDValue Hi = DAG.getMaskedGather(DAG.getVTList(HiVT, MVT::Other), HiVT, dl,
1704                                    OpsHi, MMO);
1705
1706   // Build a factor node to remember that this load is independent of the
1707   // other one.
1708   Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1709                    Hi.getValue(1));
1710
1711   // Legalize the chain result - switch anything that used the old chain to
1712   // use the new one.
1713   ReplaceValueWith(SDValue(MGT, 1), Ch);
1714
1715   SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, MGT->getValueType(0), Lo,
1716                             Hi);
1717   ReplaceValueWith(SDValue(MGT, 0), Res);
1718   return SDValue();
1719 }
1720
1721 SDValue DAGTypeLegalizer::SplitVecOp_MSTORE(MaskedStoreSDNode *N,
1722                                             unsigned OpNo) {
1723   SDValue Ch  = N->getChain();
1724   SDValue Ptr = N->getBasePtr();
1725   SDValue Mask = N->getMask();
1726   SDValue Data = N->getValue();
1727   EVT MemoryVT = N->getMemoryVT();
1728   unsigned Alignment = N->getOriginalAlignment();
1729   SDLoc DL(N);
1730
1731   EVT LoMemVT, HiMemVT;
1732   std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1733
1734   SDValue DataLo, DataHi;
1735   if (getTypeAction(Data.getValueType()) == TargetLowering::TypeSplitVector)
1736     // Split Data operand
1737     GetSplitVector(Data, DataLo, DataHi);
1738   else
1739     std::tie(DataLo, DataHi) = DAG.SplitVector(Data, DL);
1740
1741   SDValue MaskLo, MaskHi;
1742   if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
1743     // Split Mask operand
1744     GetSplitVector(Mask, MaskLo, MaskHi);
1745   else
1746     std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, DL);
1747
1748   MaskLo = PromoteTargetBoolean(MaskLo, DataLo.getValueType());
1749   MaskHi = PromoteTargetBoolean(MaskHi, DataHi.getValueType());
1750
1751   // if Alignment is equal to the vector size,
1752   // take the half of it for the second part
1753   unsigned SecondHalfAlignment =
1754     (Alignment == Data->getValueType(0).getSizeInBits()/8) ?
1755        Alignment/2 : Alignment;
1756
1757   SDValue Lo, Hi;
1758   MachineMemOperand *MMO = DAG.getMachineFunction().
1759     getMachineMemOperand(N->getPointerInfo(),
1760                          MachineMemOperand::MOStore, LoMemVT.getStoreSize(),
1761                          Alignment, N->getAAInfo(), N->getRanges());
1762
1763   Lo = DAG.getMaskedStore(Ch, DL, DataLo, Ptr, MaskLo, LoMemVT, MMO,
1764                           N->isTruncatingStore());
1765
1766   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
1767   Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1768                     DAG.getConstant(IncrementSize, DL, Ptr.getValueType()));
1769
1770   MMO = DAG.getMachineFunction().
1771     getMachineMemOperand(N->getPointerInfo(),
1772                          MachineMemOperand::MOStore,  HiMemVT.getStoreSize(),
1773                          SecondHalfAlignment, N->getAAInfo(), N->getRanges());
1774
1775   Hi = DAG.getMaskedStore(Ch, DL, DataHi, Ptr, MaskHi, HiMemVT, MMO,
1776                           N->isTruncatingStore());
1777
1778   // Build a factor node to remember that this store is independent of the
1779   // other one.
1780   return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
1781 }
1782
1783 SDValue DAGTypeLegalizer::SplitVecOp_MSCATTER(MaskedScatterSDNode *N,
1784                                               unsigned OpNo) {
1785   SDValue Ch  = N->getChain();
1786   SDValue Ptr = N->getBasePtr();
1787   SDValue Mask = N->getMask();
1788   SDValue Index = N->getIndex();
1789   SDValue Data = N->getValue();
1790   EVT MemoryVT = N->getMemoryVT();
1791   unsigned Alignment = N->getOriginalAlignment();
1792   SDLoc DL(N);
1793
1794   // Split all operands
1795   EVT LoMemVT, HiMemVT;
1796   std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1797
1798   SDValue DataLo, DataHi;
1799   if (getTypeAction(Data.getValueType()) == TargetLowering::TypeSplitVector)
1800     // Split Data operand
1801     GetSplitVector(Data, DataLo, DataHi);
1802   else
1803     std::tie(DataLo, DataHi) = DAG.SplitVector(Data, DL);
1804
1805   SDValue MaskLo, MaskHi;
1806   if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
1807     // Split Mask operand
1808     GetSplitVector(Mask, MaskLo, MaskHi);
1809   else
1810     std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, DL);
1811
1812   SDValue IndexHi, IndexLo;
1813   if (getTypeAction(Index.getValueType()) == TargetLowering::TypeSplitVector)
1814     GetSplitVector(Index, IndexLo, IndexHi);
1815   else
1816     std::tie(IndexLo, IndexHi) = DAG.SplitVector(Index, DL);
1817
1818   SDValue Lo, Hi;
1819   MachineMemOperand *MMO = DAG.getMachineFunction().
1820     getMachineMemOperand(N->getPointerInfo(),
1821                          MachineMemOperand::MOStore, LoMemVT.getStoreSize(),
1822                          Alignment, N->getAAInfo(), N->getRanges());
1823
1824   SDValue OpsLo[] = {Ch, DataLo, MaskLo, Ptr, IndexLo};
1825   Lo = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), DataLo.getValueType(),
1826                             DL, OpsLo, MMO);
1827
1828   MMO = DAG.getMachineFunction().
1829     getMachineMemOperand(N->getPointerInfo(),
1830                          MachineMemOperand::MOStore,  HiMemVT.getStoreSize(),
1831                          Alignment, N->getAAInfo(), N->getRanges());
1832
1833   SDValue OpsHi[] = {Ch, DataHi, MaskHi, Ptr, IndexHi};
1834   Hi = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), DataHi.getValueType(),
1835                             DL, OpsHi, MMO);
1836
1837   // Build a factor node to remember that this store is independent of the
1838   // other one.
1839   return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
1840 }
1841
1842 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
1843   assert(N->isUnindexed() && "Indexed store of vector?");
1844   assert(OpNo == 1 && "Can only split the stored value");
1845   SDLoc DL(N);
1846
1847   bool isTruncating = N->isTruncatingStore();
1848   SDValue Ch  = N->getChain();
1849   SDValue Ptr = N->getBasePtr();
1850   EVT MemoryVT = N->getMemoryVT();
1851   unsigned Alignment = N->getOriginalAlignment();
1852   bool isVol = N->isVolatile();
1853   bool isNT = N->isNonTemporal();
1854   AAMDNodes AAInfo = N->getAAInfo();
1855   SDValue Lo, Hi;
1856   GetSplitVector(N->getOperand(1), Lo, Hi);
1857
1858   EVT LoMemVT, HiMemVT;
1859   std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1860
1861   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
1862
1863   if (isTruncating)
1864     Lo = DAG.getTruncStore(Ch, DL, Lo, Ptr, N->getPointerInfo(),
1865                            LoMemVT, isVol, isNT, Alignment, AAInfo);
1866   else
1867     Lo = DAG.getStore(Ch, DL, Lo, Ptr, N->getPointerInfo(),
1868                       isVol, isNT, Alignment, AAInfo);
1869
1870   // Increment the pointer to the other half.
1871   Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1872                     DAG.getConstant(IncrementSize, DL, Ptr.getValueType()));
1873
1874   if (isTruncating)
1875     Hi = DAG.getTruncStore(Ch, DL, Hi, Ptr,
1876                            N->getPointerInfo().getWithOffset(IncrementSize),
1877                            HiMemVT, isVol, isNT, Alignment, AAInfo);
1878   else
1879     Hi = DAG.getStore(Ch, DL, Hi, Ptr,
1880                       N->getPointerInfo().getWithOffset(IncrementSize),
1881                       isVol, isNT, Alignment, AAInfo);
1882
1883   return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
1884 }
1885
1886 SDValue DAGTypeLegalizer::SplitVecOp_CONCAT_VECTORS(SDNode *N) {
1887   SDLoc DL(N);
1888
1889   // The input operands all must have the same type, and we know the result
1890   // type is valid.  Convert this to a buildvector which extracts all the
1891   // input elements.
1892   // TODO: If the input elements are power-two vectors, we could convert this to
1893   // a new CONCAT_VECTORS node with elements that are half-wide.
1894   SmallVector<SDValue, 32> Elts;
1895   EVT EltVT = N->getValueType(0).getVectorElementType();
1896   for (const SDValue &Op : N->op_values()) {
1897     for (unsigned i = 0, e = Op.getValueType().getVectorNumElements();
1898          i != e; ++i) {
1899       Elts.push_back(DAG.getNode(
1900           ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Op,
1901           DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
1902     }
1903   }
1904
1905   return DAG.getNode(ISD::BUILD_VECTOR, DL, N->getValueType(0), Elts);
1906 }
1907
1908 SDValue DAGTypeLegalizer::SplitVecOp_TruncateHelper(SDNode *N) {
1909   // The result type is legal, but the input type is illegal.  If splitting
1910   // ends up with the result type of each half still being legal, just
1911   // do that.  If, however, that would result in an illegal result type,
1912   // we can try to get more clever with power-two vectors. Specifically,
1913   // split the input type, but also widen the result element size, then
1914   // concatenate the halves and truncate again.  For example, consider a target
1915   // where v8i8 is legal and v8i32 is not (ARM, which doesn't have 256-bit
1916   // vectors). To perform a "%res = v8i8 trunc v8i32 %in" we do:
1917   //   %inlo = v4i32 extract_subvector %in, 0
1918   //   %inhi = v4i32 extract_subvector %in, 4
1919   //   %lo16 = v4i16 trunc v4i32 %inlo
1920   //   %hi16 = v4i16 trunc v4i32 %inhi
1921   //   %in16 = v8i16 concat_vectors v4i16 %lo16, v4i16 %hi16
1922   //   %res = v8i8 trunc v8i16 %in16
1923   //
1924   // Without this transform, the original truncate would end up being
1925   // scalarized, which is pretty much always a last resort.
1926   SDValue InVec = N->getOperand(0);
1927   EVT InVT = InVec->getValueType(0);
1928   EVT OutVT = N->getValueType(0);
1929   unsigned NumElements = OutVT.getVectorNumElements();
1930   bool IsFloat = OutVT.isFloatingPoint();
1931
1932   // Widening should have already made sure this is a power-two vector
1933   // if we're trying to split it at all. assert() that's true, just in case.
1934   assert(!(NumElements & 1) && "Splitting vector, but not in half!");
1935
1936   unsigned InElementSize = InVT.getVectorElementType().getSizeInBits();
1937   unsigned OutElementSize = OutVT.getVectorElementType().getSizeInBits();
1938
1939   // If the input elements are only 1/2 the width of the result elements,
1940   // just use the normal splitting. Our trick only work if there's room
1941   // to split more than once.
1942   if (InElementSize <= OutElementSize * 2)
1943     return SplitVecOp_UnaryOp(N);
1944   SDLoc DL(N);
1945
1946   // Extract the halves of the input via extract_subvector.
1947   SDValue InLoVec, InHiVec;
1948   std::tie(InLoVec, InHiVec) = DAG.SplitVector(InVec, DL);
1949   // Truncate them to 1/2 the element size.
1950   EVT HalfElementVT = IsFloat ?
1951     EVT::getFloatingPointVT(InElementSize/2) :
1952     EVT::getIntegerVT(*DAG.getContext(), InElementSize/2);
1953   EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT,
1954                                 NumElements/2);
1955   SDValue HalfLo = DAG.getNode(N->getOpcode(), DL, HalfVT, InLoVec);
1956   SDValue HalfHi = DAG.getNode(N->getOpcode(), DL, HalfVT, InHiVec);
1957   // Concatenate them to get the full intermediate truncation result.
1958   EVT InterVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT, NumElements);
1959   SDValue InterVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InterVT, HalfLo,
1960                                  HalfHi);
1961   // Now finish up by truncating all the way down to the original result
1962   // type. This should normally be something that ends up being legal directly,
1963   // but in theory if a target has very wide vectors and an annoyingly
1964   // restricted set of legal types, this split can chain to build things up.
1965   return IsFloat
1966              ? DAG.getNode(ISD::FP_ROUND, DL, OutVT, InterVec,
1967                            DAG.getTargetConstant(
1968                                0, DL, TLI.getPointerTy(DAG.getDataLayout())))
1969              : DAG.getNode(ISD::TRUNCATE, DL, OutVT, InterVec);
1970 }
1971
1972 SDValue DAGTypeLegalizer::SplitVecOp_VSETCC(SDNode *N) {
1973   assert(N->getValueType(0).isVector() &&
1974          N->getOperand(0).getValueType().isVector() &&
1975          "Operand types must be vectors");
1976   // The result has a legal vector type, but the input needs splitting.
1977   SDValue Lo0, Hi0, Lo1, Hi1, LoRes, HiRes;
1978   SDLoc DL(N);
1979   GetSplitVector(N->getOperand(0), Lo0, Hi0);
1980   GetSplitVector(N->getOperand(1), Lo1, Hi1);
1981   unsigned PartElements = Lo0.getValueType().getVectorNumElements();
1982   EVT PartResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, PartElements);
1983   EVT WideResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, 2*PartElements);
1984
1985   LoRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Lo0, Lo1, N->getOperand(2));
1986   HiRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Hi0, Hi1, N->getOperand(2));
1987   SDValue Con = DAG.getNode(ISD::CONCAT_VECTORS, DL, WideResVT, LoRes, HiRes);
1988   return PromoteTargetBoolean(Con, N->getValueType(0));
1989 }
1990
1991
1992 SDValue DAGTypeLegalizer::SplitVecOp_FP_ROUND(SDNode *N) {
1993   // The result has a legal vector type, but the input needs splitting.
1994   EVT ResVT = N->getValueType(0);
1995   SDValue Lo, Hi;
1996   SDLoc DL(N);
1997   GetSplitVector(N->getOperand(0), Lo, Hi);
1998   EVT InVT = Lo.getValueType();
1999
2000   EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
2001                                InVT.getVectorNumElements());
2002
2003   Lo = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Lo, N->getOperand(1));
2004   Hi = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Hi, N->getOperand(1));
2005
2006   return DAG.getNode(ISD::CONCAT_VECTORS, DL, ResVT, Lo, Hi);
2007 }
2008
2009 SDValue DAGTypeLegalizer::SplitVecOp_FCOPYSIGN(SDNode *N) {
2010   // The result (and the first input) has a legal vector type, but the second
2011   // input needs splitting.
2012   return DAG.UnrollVectorOp(N, N->getValueType(0).getVectorNumElements());
2013 }
2014
2015
2016 //===----------------------------------------------------------------------===//
2017 //  Result Vector Widening
2018 //===----------------------------------------------------------------------===//
2019
2020 void DAGTypeLegalizer::WidenVectorResult(SDNode *N, unsigned ResNo) {
2021   DEBUG(dbgs() << "Widen node result " << ResNo << ": ";
2022         N->dump(&DAG);
2023         dbgs() << "\n");
2024
2025   // See if the target wants to custom widen this node.
2026   if (CustomWidenLowerNode(N, N->getValueType(ResNo)))
2027     return;
2028
2029   SDValue Res = SDValue();
2030   switch (N->getOpcode()) {
2031   default:
2032 #ifndef NDEBUG
2033     dbgs() << "WidenVectorResult #" << ResNo << ": ";
2034     N->dump(&DAG);
2035     dbgs() << "\n";
2036 #endif
2037     llvm_unreachable("Do not know how to widen the result of this operator!");
2038
2039   case ISD::MERGE_VALUES:      Res = WidenVecRes_MERGE_VALUES(N, ResNo); break;
2040   case ISD::BITCAST:           Res = WidenVecRes_BITCAST(N); break;
2041   case ISD::BUILD_VECTOR:      Res = WidenVecRes_BUILD_VECTOR(N); break;
2042   case ISD::CONCAT_VECTORS:    Res = WidenVecRes_CONCAT_VECTORS(N); break;
2043   case ISD::CONVERT_RNDSAT:    Res = WidenVecRes_CONVERT_RNDSAT(N); break;
2044   case ISD::EXTRACT_SUBVECTOR: Res = WidenVecRes_EXTRACT_SUBVECTOR(N); break;
2045   case ISD::FP_ROUND_INREG:    Res = WidenVecRes_InregOp(N); break;
2046   case ISD::INSERT_VECTOR_ELT: Res = WidenVecRes_INSERT_VECTOR_ELT(N); break;
2047   case ISD::LOAD:              Res = WidenVecRes_LOAD(N); break;
2048   case ISD::SCALAR_TO_VECTOR:  Res = WidenVecRes_SCALAR_TO_VECTOR(N); break;
2049   case ISD::SIGN_EXTEND_INREG: Res = WidenVecRes_InregOp(N); break;
2050   case ISD::VSELECT:
2051   case ISD::SELECT:            Res = WidenVecRes_SELECT(N); break;
2052   case ISD::SELECT_CC:         Res = WidenVecRes_SELECT_CC(N); break;
2053   case ISD::SETCC:             Res = WidenVecRes_SETCC(N); break;
2054   case ISD::UNDEF:             Res = WidenVecRes_UNDEF(N); break;
2055   case ISD::VECTOR_SHUFFLE:
2056     Res = WidenVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N));
2057     break;
2058   case ISD::MLOAD:
2059     Res = WidenVecRes_MLOAD(cast<MaskedLoadSDNode>(N));
2060     break;
2061   case ISD::MGATHER:
2062     Res = WidenVecRes_MGATHER(cast<MaskedGatherSDNode>(N));
2063     break;
2064
2065   case ISD::ADD:
2066   case ISD::AND:
2067   case ISD::MUL:
2068   case ISD::MULHS:
2069   case ISD::MULHU:
2070   case ISD::OR:
2071   case ISD::SUB:
2072   case ISD::XOR:
2073   case ISD::FMINNUM:
2074   case ISD::FMAXNUM:
2075   case ISD::FMINNAN:
2076   case ISD::FMAXNAN:
2077   case ISD::SMIN:
2078   case ISD::SMAX:
2079   case ISD::UMIN:
2080   case ISD::UMAX:
2081     Res = WidenVecRes_Binary(N);
2082     break;
2083
2084   case ISD::FADD:
2085   case ISD::FMUL:
2086   case ISD::FPOW:
2087   case ISD::FSUB:
2088   case ISD::FDIV:
2089   case ISD::FREM:
2090   case ISD::SDIV:
2091   case ISD::UDIV:
2092   case ISD::SREM:
2093   case ISD::UREM:
2094     Res = WidenVecRes_BinaryCanTrap(N);
2095     break;
2096
2097   case ISD::FCOPYSIGN:
2098     Res = WidenVecRes_FCOPYSIGN(N);
2099     break;
2100
2101   case ISD::FPOWI:
2102     Res = WidenVecRes_POWI(N);
2103     break;
2104
2105   case ISD::SHL:
2106   case ISD::SRA:
2107   case ISD::SRL:
2108     Res = WidenVecRes_Shift(N);
2109     break;
2110
2111   case ISD::ANY_EXTEND_VECTOR_INREG:
2112   case ISD::SIGN_EXTEND_VECTOR_INREG:
2113   case ISD::ZERO_EXTEND_VECTOR_INREG:
2114     Res = WidenVecRes_EXTEND_VECTOR_INREG(N);
2115     break;
2116
2117   case ISD::ANY_EXTEND:
2118   case ISD::FP_EXTEND:
2119   case ISD::FP_ROUND:
2120   case ISD::FP_TO_SINT:
2121   case ISD::FP_TO_UINT:
2122   case ISD::SIGN_EXTEND:
2123   case ISD::SINT_TO_FP:
2124   case ISD::TRUNCATE:
2125   case ISD::UINT_TO_FP:
2126   case ISD::ZERO_EXTEND:
2127     Res = WidenVecRes_Convert(N);
2128     break;
2129
2130   case ISD::BITREVERSE:
2131   case ISD::BSWAP:
2132   case ISD::CTLZ:
2133   case ISD::CTPOP:
2134   case ISD::CTTZ:
2135   case ISD::FABS:
2136   case ISD::FCEIL:
2137   case ISD::FCOS:
2138   case ISD::FEXP:
2139   case ISD::FEXP2:
2140   case ISD::FFLOOR:
2141   case ISD::FLOG:
2142   case ISD::FLOG10:
2143   case ISD::FLOG2:
2144   case ISD::FNEARBYINT:
2145   case ISD::FNEG:
2146   case ISD::FRINT:
2147   case ISD::FROUND:
2148   case ISD::FSIN:
2149   case ISD::FSQRT:
2150   case ISD::FTRUNC:
2151     Res = WidenVecRes_Unary(N);
2152     break;
2153   case ISD::FMA:
2154     Res = WidenVecRes_Ternary(N);
2155     break;
2156   }
2157
2158   // If Res is null, the sub-method took care of registering the result.
2159   if (Res.getNode())
2160     SetWidenedVector(SDValue(N, ResNo), Res);
2161 }
2162
2163 SDValue DAGTypeLegalizer::WidenVecRes_Ternary(SDNode *N) {
2164   // Ternary op widening.
2165   SDLoc dl(N);
2166   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2167   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2168   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2169   SDValue InOp3 = GetWidenedVector(N->getOperand(2));
2170   return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, InOp3);
2171 }
2172
2173 SDValue DAGTypeLegalizer::WidenVecRes_Binary(SDNode *N) {
2174   // Binary op widening.
2175   SDLoc dl(N);
2176   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2177   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2178   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2179   return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, N->getFlags());
2180 }
2181
2182 SDValue DAGTypeLegalizer::WidenVecRes_BinaryCanTrap(SDNode *N) {
2183   // Binary op widening for operations that can trap.
2184   unsigned Opcode = N->getOpcode();
2185   SDLoc dl(N);
2186   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2187   EVT WidenEltVT = WidenVT.getVectorElementType();
2188   EVT VT = WidenVT;
2189   unsigned NumElts =  VT.getVectorNumElements();
2190   const SDNodeFlags *Flags = N->getFlags();
2191   while (!TLI.isTypeLegal(VT) && NumElts != 1) {
2192     NumElts = NumElts / 2;
2193     VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
2194   }
2195
2196   if (NumElts != 1 && !TLI.canOpTrap(N->getOpcode(), VT)) {
2197     // Operation doesn't trap so just widen as normal.
2198     SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2199     SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2200     return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, Flags);
2201   }
2202
2203   // No legal vector version so unroll the vector operation and then widen.
2204   if (NumElts == 1)
2205     return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
2206
2207   // Since the operation can trap, apply operation on the original vector.
2208   EVT MaxVT = VT;
2209   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2210   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2211   unsigned CurNumElts = N->getValueType(0).getVectorNumElements();
2212
2213   SmallVector<SDValue, 16> ConcatOps(CurNumElts);
2214   unsigned ConcatEnd = 0;  // Current ConcatOps index.
2215   int Idx = 0;        // Current Idx into input vectors.
2216
2217   // NumElts := greatest legal vector size (at most WidenVT)
2218   // while (orig. vector has unhandled elements) {
2219   //   take munches of size NumElts from the beginning and add to ConcatOps
2220   //   NumElts := next smaller supported vector size or 1
2221   // }
2222   while (CurNumElts != 0) {
2223     while (CurNumElts >= NumElts) {
2224       SDValue EOp1 = DAG.getNode(
2225           ISD::EXTRACT_SUBVECTOR, dl, VT, InOp1,
2226           DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2227       SDValue EOp2 = DAG.getNode(
2228           ISD::EXTRACT_SUBVECTOR, dl, VT, InOp2,
2229           DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2230       ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, VT, EOp1, EOp2, Flags);
2231       Idx += NumElts;
2232       CurNumElts -= NumElts;
2233     }
2234     do {
2235       NumElts = NumElts / 2;
2236       VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
2237     } while (!TLI.isTypeLegal(VT) && NumElts != 1);
2238
2239     if (NumElts == 1) {
2240       for (unsigned i = 0; i != CurNumElts; ++i, ++Idx) {
2241         SDValue EOp1 = DAG.getNode(
2242             ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT, InOp1,
2243             DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2244         SDValue EOp2 = DAG.getNode(
2245             ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT, InOp2,
2246             DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2247         ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, WidenEltVT,
2248                                              EOp1, EOp2, Flags);
2249       }
2250       CurNumElts = 0;
2251     }
2252   }
2253
2254   // Check to see if we have a single operation with the widen type.
2255   if (ConcatEnd == 1) {
2256     VT = ConcatOps[0].getValueType();
2257     if (VT == WidenVT)
2258       return ConcatOps[0];
2259   }
2260
2261   // while (Some element of ConcatOps is not of type MaxVT) {
2262   //   From the end of ConcatOps, collect elements of the same type and put
2263   //   them into an op of the next larger supported type
2264   // }
2265   while (ConcatOps[ConcatEnd-1].getValueType() != MaxVT) {
2266     Idx = ConcatEnd - 1;
2267     VT = ConcatOps[Idx--].getValueType();
2268     while (Idx >= 0 && ConcatOps[Idx].getValueType() == VT)
2269       Idx--;
2270
2271     int NextSize = VT.isVector() ? VT.getVectorNumElements() : 1;
2272     EVT NextVT;
2273     do {
2274       NextSize *= 2;
2275       NextVT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NextSize);
2276     } while (!TLI.isTypeLegal(NextVT));
2277
2278     if (!VT.isVector()) {
2279       // Scalar type, create an INSERT_VECTOR_ELEMENT of type NextVT
2280       SDValue VecOp = DAG.getUNDEF(NextVT);
2281       unsigned NumToInsert = ConcatEnd - Idx - 1;
2282       for (unsigned i = 0, OpIdx = Idx+1; i < NumToInsert; i++, OpIdx++) {
2283         VecOp = DAG.getNode(
2284             ISD::INSERT_VECTOR_ELT, dl, NextVT, VecOp, ConcatOps[OpIdx],
2285             DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2286       }
2287       ConcatOps[Idx+1] = VecOp;
2288       ConcatEnd = Idx + 2;
2289     } else {
2290       // Vector type, create a CONCAT_VECTORS of type NextVT
2291       SDValue undefVec = DAG.getUNDEF(VT);
2292       unsigned OpsToConcat = NextSize/VT.getVectorNumElements();
2293       SmallVector<SDValue, 16> SubConcatOps(OpsToConcat);
2294       unsigned RealVals = ConcatEnd - Idx - 1;
2295       unsigned SubConcatEnd = 0;
2296       unsigned SubConcatIdx = Idx + 1;
2297       while (SubConcatEnd < RealVals)
2298         SubConcatOps[SubConcatEnd++] = ConcatOps[++Idx];
2299       while (SubConcatEnd < OpsToConcat)
2300         SubConcatOps[SubConcatEnd++] = undefVec;
2301       ConcatOps[SubConcatIdx] = DAG.getNode(ISD::CONCAT_VECTORS, dl,
2302                                             NextVT, SubConcatOps);
2303       ConcatEnd = SubConcatIdx + 1;
2304     }
2305   }
2306
2307   // Check to see if we have a single operation with the widen type.
2308   if (ConcatEnd == 1) {
2309     VT = ConcatOps[0].getValueType();
2310     if (VT == WidenVT)
2311       return ConcatOps[0];
2312   }
2313
2314   // add undefs of size MaxVT until ConcatOps grows to length of WidenVT
2315   unsigned NumOps = WidenVT.getVectorNumElements()/MaxVT.getVectorNumElements();
2316   if (NumOps != ConcatEnd ) {
2317     SDValue UndefVal = DAG.getUNDEF(MaxVT);
2318     for (unsigned j = ConcatEnd; j < NumOps; ++j)
2319       ConcatOps[j] = UndefVal;
2320   }
2321   return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
2322                      makeArrayRef(ConcatOps.data(), NumOps));
2323 }
2324
2325 SDValue DAGTypeLegalizer::WidenVecRes_Convert(SDNode *N) {
2326   SDValue InOp = N->getOperand(0);
2327   SDLoc DL(N);
2328
2329   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2330   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2331
2332   EVT InVT = InOp.getValueType();
2333   EVT InEltVT = InVT.getVectorElementType();
2334   EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
2335
2336   unsigned Opcode = N->getOpcode();
2337   unsigned InVTNumElts = InVT.getVectorNumElements();
2338   const SDNodeFlags *Flags = N->getFlags();
2339   if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
2340     InOp = GetWidenedVector(N->getOperand(0));
2341     InVT = InOp.getValueType();
2342     InVTNumElts = InVT.getVectorNumElements();
2343     if (InVTNumElts == WidenNumElts) {
2344       if (N->getNumOperands() == 1)
2345         return DAG.getNode(Opcode, DL, WidenVT, InOp);
2346       return DAG.getNode(Opcode, DL, WidenVT, InOp, N->getOperand(1), Flags);
2347     }
2348   }
2349
2350   if (TLI.isTypeLegal(InWidenVT)) {
2351     // Because the result and the input are different vector types, widening
2352     // the result could create a legal type but widening the input might make
2353     // it an illegal type that might lead to repeatedly splitting the input
2354     // and then widening it. To avoid this, we widen the input only if
2355     // it results in a legal type.
2356     if (WidenNumElts % InVTNumElts == 0) {
2357       // Widen the input and call convert on the widened input vector.
2358       unsigned NumConcat = WidenNumElts/InVTNumElts;
2359       SmallVector<SDValue, 16> Ops(NumConcat);
2360       Ops[0] = InOp;
2361       SDValue UndefVal = DAG.getUNDEF(InVT);
2362       for (unsigned i = 1; i != NumConcat; ++i)
2363         Ops[i] = UndefVal;
2364       SDValue InVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InWidenVT, Ops);
2365       if (N->getNumOperands() == 1)
2366         return DAG.getNode(Opcode, DL, WidenVT, InVec);
2367       return DAG.getNode(Opcode, DL, WidenVT, InVec, N->getOperand(1), Flags);
2368     }
2369
2370     if (InVTNumElts % WidenNumElts == 0) {
2371       SDValue InVal = DAG.getNode(
2372           ISD::EXTRACT_SUBVECTOR, DL, InWidenVT, InOp,
2373           DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
2374       // Extract the input and convert the shorten input vector.
2375       if (N->getNumOperands() == 1)
2376         return DAG.getNode(Opcode, DL, WidenVT, InVal);
2377       return DAG.getNode(Opcode, DL, WidenVT, InVal, N->getOperand(1), Flags);
2378     }
2379   }
2380
2381   // Otherwise unroll into some nasty scalar code and rebuild the vector.
2382   SmallVector<SDValue, 16> Ops(WidenNumElts);
2383   EVT EltVT = WidenVT.getVectorElementType();
2384   unsigned MinElts = std::min(InVTNumElts, WidenNumElts);
2385   unsigned i;
2386   for (i=0; i < MinElts; ++i) {
2387     SDValue Val = DAG.getNode(
2388         ISD::EXTRACT_VECTOR_ELT, DL, InEltVT, InOp,
2389         DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
2390     if (N->getNumOperands() == 1)
2391       Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val);
2392     else
2393       Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val, N->getOperand(1), Flags);
2394   }
2395
2396   SDValue UndefVal = DAG.getUNDEF(EltVT);
2397   for (; i < WidenNumElts; ++i)
2398     Ops[i] = UndefVal;
2399
2400   return DAG.getNode(ISD::BUILD_VECTOR, DL, WidenVT, Ops);
2401 }
2402
2403 SDValue DAGTypeLegalizer::WidenVecRes_EXTEND_VECTOR_INREG(SDNode *N) {
2404   unsigned Opcode = N->getOpcode();
2405   SDValue InOp = N->getOperand(0);
2406   SDLoc DL(N);
2407
2408   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2409   EVT WidenSVT = WidenVT.getVectorElementType();
2410   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2411
2412   EVT InVT = InOp.getValueType();
2413   EVT InSVT = InVT.getVectorElementType();
2414   unsigned InVTNumElts = InVT.getVectorNumElements();
2415
2416   if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
2417     InOp = GetWidenedVector(InOp);
2418     InVT = InOp.getValueType();
2419     if (InVT.getSizeInBits() == WidenVT.getSizeInBits()) {
2420       switch (Opcode) {
2421       case ISD::ANY_EXTEND_VECTOR_INREG:
2422         return DAG.getAnyExtendVectorInReg(InOp, DL, WidenVT);
2423       case ISD::SIGN_EXTEND_VECTOR_INREG:
2424         return DAG.getSignExtendVectorInReg(InOp, DL, WidenVT);
2425       case ISD::ZERO_EXTEND_VECTOR_INREG:
2426         return DAG.getZeroExtendVectorInReg(InOp, DL, WidenVT);
2427       }
2428     }
2429   }
2430
2431   // Unroll, extend the scalars and rebuild the vector.
2432   SmallVector<SDValue, 16> Ops;
2433   for (unsigned i = 0, e = std::min(InVTNumElts, WidenNumElts); i != e; ++i) {
2434     SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, InSVT, InOp,
2435       DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
2436     switch (Opcode) {
2437     case ISD::ANY_EXTEND_VECTOR_INREG:
2438       Val = DAG.getNode(ISD::ANY_EXTEND, DL, WidenSVT, Val);
2439       break;
2440     case ISD::SIGN_EXTEND_VECTOR_INREG:
2441       Val = DAG.getNode(ISD::SIGN_EXTEND, DL, WidenSVT, Val);
2442       break;
2443     case ISD::ZERO_EXTEND_VECTOR_INREG:
2444       Val = DAG.getNode(ISD::ZERO_EXTEND, DL, WidenSVT, Val);
2445       break;
2446     default:
2447       llvm_unreachable("A *_EXTEND_VECTOR_INREG node was expected");
2448     }
2449     Ops.push_back(Val);
2450   }
2451
2452   while (Ops.size() != WidenNumElts)
2453     Ops.push_back(DAG.getUNDEF(WidenSVT));
2454
2455   return DAG.getNode(ISD::BUILD_VECTOR, DL, WidenVT, Ops);
2456 }
2457
2458 SDValue DAGTypeLegalizer::WidenVecRes_FCOPYSIGN(SDNode *N) {
2459   // If this is an FCOPYSIGN with same input types, we can treat it as a
2460   // normal (can trap) binary op.
2461   if (N->getOperand(0).getValueType() == N->getOperand(1).getValueType())
2462     return WidenVecRes_BinaryCanTrap(N);
2463
2464   // If the types are different, fall back to unrolling.
2465   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2466   return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
2467 }
2468
2469 SDValue DAGTypeLegalizer::WidenVecRes_POWI(SDNode *N) {
2470   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2471   SDValue InOp = GetWidenedVector(N->getOperand(0));
2472   SDValue ShOp = N->getOperand(1);
2473   return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ShOp);
2474 }
2475
2476 SDValue DAGTypeLegalizer::WidenVecRes_Shift(SDNode *N) {
2477   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2478   SDValue InOp = GetWidenedVector(N->getOperand(0));
2479   SDValue ShOp = N->getOperand(1);
2480
2481   EVT ShVT = ShOp.getValueType();
2482   if (getTypeAction(ShVT) == TargetLowering::TypeWidenVector) {
2483     ShOp = GetWidenedVector(ShOp);
2484     ShVT = ShOp.getValueType();
2485   }
2486   EVT ShWidenVT = EVT::getVectorVT(*DAG.getContext(),
2487                                    ShVT.getVectorElementType(),
2488                                    WidenVT.getVectorNumElements());
2489   if (ShVT != ShWidenVT)
2490     ShOp = ModifyToType(ShOp, ShWidenVT);
2491
2492   return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ShOp);
2493 }
2494
2495 SDValue DAGTypeLegalizer::WidenVecRes_Unary(SDNode *N) {
2496   // Unary op widening.
2497   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2498   SDValue InOp = GetWidenedVector(N->getOperand(0));
2499   return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp);
2500 }
2501
2502 SDValue DAGTypeLegalizer::WidenVecRes_InregOp(SDNode *N) {
2503   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2504   EVT ExtVT = EVT::getVectorVT(*DAG.getContext(),
2505                                cast<VTSDNode>(N->getOperand(1))->getVT()
2506                                  .getVectorElementType(),
2507                                WidenVT.getVectorNumElements());
2508   SDValue WidenLHS = GetWidenedVector(N->getOperand(0));
2509   return DAG.getNode(N->getOpcode(), SDLoc(N),
2510                      WidenVT, WidenLHS, DAG.getValueType(ExtVT));
2511 }
2512
2513 SDValue DAGTypeLegalizer::WidenVecRes_MERGE_VALUES(SDNode *N, unsigned ResNo) {
2514   SDValue WidenVec = DisintegrateMERGE_VALUES(N, ResNo);
2515   return GetWidenedVector(WidenVec);
2516 }
2517
2518 SDValue DAGTypeLegalizer::WidenVecRes_BITCAST(SDNode *N) {
2519   SDValue InOp = N->getOperand(0);
2520   EVT InVT = InOp.getValueType();
2521   EVT VT = N->getValueType(0);
2522   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2523   SDLoc dl(N);
2524
2525   switch (getTypeAction(InVT)) {
2526   case TargetLowering::TypeLegal:
2527     break;
2528   case TargetLowering::TypePromoteInteger:
2529     // If the incoming type is a vector that is being promoted, then
2530     // we know that the elements are arranged differently and that we
2531     // must perform the conversion using a stack slot.
2532     if (InVT.isVector())
2533       break;
2534
2535     // If the InOp is promoted to the same size, convert it.  Otherwise,
2536     // fall out of the switch and widen the promoted input.
2537     InOp = GetPromotedInteger(InOp);
2538     InVT = InOp.getValueType();
2539     if (WidenVT.bitsEq(InVT))
2540       return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
2541     break;
2542   case TargetLowering::TypeSoftenFloat:
2543   case TargetLowering::TypePromoteFloat:
2544   case TargetLowering::TypeExpandInteger:
2545   case TargetLowering::TypeExpandFloat:
2546   case TargetLowering::TypeScalarizeVector:
2547   case TargetLowering::TypeSplitVector:
2548     break;
2549   case TargetLowering::TypeWidenVector:
2550     // If the InOp is widened to the same size, convert it.  Otherwise, fall
2551     // out of the switch and widen the widened input.
2552     InOp = GetWidenedVector(InOp);
2553     InVT = InOp.getValueType();
2554     if (WidenVT.bitsEq(InVT))
2555       // The input widens to the same size. Convert to the widen value.
2556       return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
2557     break;
2558   }
2559
2560   unsigned WidenSize = WidenVT.getSizeInBits();
2561   unsigned InSize = InVT.getSizeInBits();
2562   // x86mmx is not an acceptable vector element type, so don't try.
2563   if (WidenSize % InSize == 0 && InVT != MVT::x86mmx) {
2564     // Determine new input vector type.  The new input vector type will use
2565     // the same element type (if its a vector) or use the input type as a
2566     // vector.  It is the same size as the type to widen to.
2567     EVT NewInVT;
2568     unsigned NewNumElts = WidenSize / InSize;
2569     if (InVT.isVector()) {
2570       EVT InEltVT = InVT.getVectorElementType();
2571       NewInVT = EVT::getVectorVT(*DAG.getContext(), InEltVT,
2572                                  WidenSize / InEltVT.getSizeInBits());
2573     } else {
2574       NewInVT = EVT::getVectorVT(*DAG.getContext(), InVT, NewNumElts);
2575     }
2576
2577     if (TLI.isTypeLegal(NewInVT)) {
2578       // Because the result and the input are different vector types, widening
2579       // the result could create a legal type but widening the input might make
2580       // it an illegal type that might lead to repeatedly splitting the input
2581       // and then widening it. To avoid this, we widen the input only if
2582       // it results in a legal type.
2583       SmallVector<SDValue, 16> Ops(NewNumElts);
2584       SDValue UndefVal = DAG.getUNDEF(InVT);
2585       Ops[0] = InOp;
2586       for (unsigned i = 1; i < NewNumElts; ++i)
2587         Ops[i] = UndefVal;
2588
2589       SDValue NewVec;
2590       if (InVT.isVector())
2591         NewVec = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewInVT, Ops);
2592       else
2593         NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl, NewInVT, Ops);
2594       return DAG.getNode(ISD::BITCAST, dl, WidenVT, NewVec);
2595     }
2596   }
2597
2598   return CreateStackStoreLoad(InOp, WidenVT);
2599 }
2600
2601 SDValue DAGTypeLegalizer::WidenVecRes_BUILD_VECTOR(SDNode *N) {
2602   SDLoc dl(N);
2603   // Build a vector with undefined for the new nodes.
2604   EVT VT = N->getValueType(0);
2605
2606   // Integer BUILD_VECTOR operands may be larger than the node's vector element
2607   // type. The UNDEFs need to have the same type as the existing operands.
2608   EVT EltVT = N->getOperand(0).getValueType();
2609   unsigned NumElts = VT.getVectorNumElements();
2610
2611   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2612   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2613
2614   SmallVector<SDValue, 16> NewOps(N->op_begin(), N->op_end());
2615   assert(WidenNumElts >= NumElts && "Shrinking vector instead of widening!");
2616   NewOps.append(WidenNumElts - NumElts, DAG.getUNDEF(EltVT));
2617
2618   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, NewOps);
2619 }
2620
2621 SDValue DAGTypeLegalizer::WidenVecRes_CONCAT_VECTORS(SDNode *N) {
2622   EVT InVT = N->getOperand(0).getValueType();
2623   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2624   SDLoc dl(N);
2625   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2626   unsigned NumInElts = InVT.getVectorNumElements();
2627   unsigned NumOperands = N->getNumOperands();
2628
2629   bool InputWidened = false; // Indicates we need to widen the input.
2630   if (getTypeAction(InVT) != TargetLowering::TypeWidenVector) {
2631     if (WidenVT.getVectorNumElements() % InVT.getVectorNumElements() == 0) {
2632       // Add undef vectors to widen to correct length.
2633       unsigned NumConcat = WidenVT.getVectorNumElements() /
2634                            InVT.getVectorNumElements();
2635       SDValue UndefVal = DAG.getUNDEF(InVT);
2636       SmallVector<SDValue, 16> Ops(NumConcat);
2637       for (unsigned i=0; i < NumOperands; ++i)
2638         Ops[i] = N->getOperand(i);
2639       for (unsigned i = NumOperands; i != NumConcat; ++i)
2640         Ops[i] = UndefVal;
2641       return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, Ops);
2642     }
2643   } else {
2644     InputWidened = true;
2645     if (WidenVT == TLI.getTypeToTransformTo(*DAG.getContext(), InVT)) {
2646       // The inputs and the result are widen to the same value.
2647       unsigned i;
2648       for (i=1; i < NumOperands; ++i)
2649         if (!N->getOperand(i).isUndef())
2650           break;
2651
2652       if (i == NumOperands)
2653         // Everything but the first operand is an UNDEF so just return the
2654         // widened first operand.
2655         return GetWidenedVector(N->getOperand(0));
2656
2657       if (NumOperands == 2) {
2658         // Replace concat of two operands with a shuffle.
2659         SmallVector<int, 16> MaskOps(WidenNumElts, -1);
2660         for (unsigned i = 0; i < NumInElts; ++i) {
2661           MaskOps[i] = i;
2662           MaskOps[i + NumInElts] = i + WidenNumElts;
2663         }
2664         return DAG.getVectorShuffle(WidenVT, dl,
2665                                     GetWidenedVector(N->getOperand(0)),
2666                                     GetWidenedVector(N->getOperand(1)),
2667                                     &MaskOps[0]);
2668       }
2669     }
2670   }
2671
2672   // Fall back to use extracts and build vector.
2673   EVT EltVT = WidenVT.getVectorElementType();
2674   SmallVector<SDValue, 16> Ops(WidenNumElts);
2675   unsigned Idx = 0;
2676   for (unsigned i=0; i < NumOperands; ++i) {
2677     SDValue InOp = N->getOperand(i);
2678     if (InputWidened)
2679       InOp = GetWidenedVector(InOp);
2680     for (unsigned j=0; j < NumInElts; ++j)
2681       Ops[Idx++] = DAG.getNode(
2682           ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2683           DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2684   }
2685   SDValue UndefVal = DAG.getUNDEF(EltVT);
2686   for (; Idx < WidenNumElts; ++Idx)
2687     Ops[Idx] = UndefVal;
2688   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, Ops);
2689 }
2690
2691 SDValue DAGTypeLegalizer::WidenVecRes_CONVERT_RNDSAT(SDNode *N) {
2692   SDLoc dl(N);
2693   SDValue InOp  = N->getOperand(0);
2694   SDValue RndOp = N->getOperand(3);
2695   SDValue SatOp = N->getOperand(4);
2696
2697   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2698   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2699
2700   EVT InVT = InOp.getValueType();
2701   EVT InEltVT = InVT.getVectorElementType();
2702   EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
2703
2704   SDValue DTyOp = DAG.getValueType(WidenVT);
2705   SDValue STyOp = DAG.getValueType(InWidenVT);
2706   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
2707
2708   unsigned InVTNumElts = InVT.getVectorNumElements();
2709   if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
2710     InOp = GetWidenedVector(InOp);
2711     InVT = InOp.getValueType();
2712     InVTNumElts = InVT.getVectorNumElements();
2713     if (InVTNumElts == WidenNumElts)
2714       return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
2715                                   SatOp, CvtCode);
2716   }
2717
2718   if (TLI.isTypeLegal(InWidenVT)) {
2719     // Because the result and the input are different vector types, widening
2720     // the result could create a legal type but widening the input might make
2721     // it an illegal type that might lead to repeatedly splitting the input
2722     // and then widening it. To avoid this, we widen the input only if
2723     // it results in a legal type.
2724     if (WidenNumElts % InVTNumElts == 0) {
2725       // Widen the input and call convert on the widened input vector.
2726       unsigned NumConcat = WidenNumElts/InVTNumElts;
2727       SmallVector<SDValue, 16> Ops(NumConcat);
2728       Ops[0] = InOp;
2729       SDValue UndefVal = DAG.getUNDEF(InVT);
2730       for (unsigned i = 1; i != NumConcat; ++i)
2731         Ops[i] = UndefVal;
2732
2733       InOp = DAG.getNode(ISD::CONCAT_VECTORS, dl, InWidenVT, Ops);
2734       return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
2735                                   SatOp, CvtCode);
2736     }
2737
2738     if (InVTNumElts % WidenNumElts == 0) {
2739       // Extract the input and convert the shorten input vector.
2740       InOp = DAG.getNode(
2741           ISD::EXTRACT_SUBVECTOR, dl, InWidenVT, InOp,
2742           DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2743       return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
2744                                   SatOp, CvtCode);
2745     }
2746   }
2747
2748   // Otherwise unroll into some nasty scalar code and rebuild the vector.
2749   SmallVector<SDValue, 16> Ops(WidenNumElts);
2750   EVT EltVT = WidenVT.getVectorElementType();
2751   DTyOp = DAG.getValueType(EltVT);
2752   STyOp = DAG.getValueType(InEltVT);
2753
2754   unsigned MinElts = std::min(InVTNumElts, WidenNumElts);
2755   unsigned i;
2756   for (i=0; i < MinElts; ++i) {
2757     SDValue ExtVal = DAG.getNode(
2758         ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
2759         DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2760     Ops[i] = DAG.getConvertRndSat(WidenVT, dl, ExtVal, DTyOp, STyOp, RndOp,
2761                                   SatOp, CvtCode);
2762   }
2763
2764   SDValue UndefVal = DAG.getUNDEF(EltVT);
2765   for (; i < WidenNumElts; ++i)
2766     Ops[i] = UndefVal;
2767
2768   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, Ops);
2769 }
2770
2771 SDValue DAGTypeLegalizer::WidenVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
2772   EVT      VT = N->getValueType(0);
2773   EVT      WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2774   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2775   SDValue  InOp = N->getOperand(0);
2776   SDValue  Idx  = N->getOperand(1);
2777   SDLoc dl(N);
2778
2779   if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2780     InOp = GetWidenedVector(InOp);
2781
2782   EVT InVT = InOp.getValueType();
2783
2784   // Check if we can just return the input vector after widening.
2785   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
2786   if (IdxVal == 0 && InVT == WidenVT)
2787     return InOp;
2788
2789   // Check if we can extract from the vector.
2790   unsigned InNumElts = InVT.getVectorNumElements();
2791   if (IdxVal % WidenNumElts == 0 && IdxVal + WidenNumElts < InNumElts)
2792     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, InOp, Idx);
2793
2794   // We could try widening the input to the right length but for now, extract
2795   // the original elements, fill the rest with undefs and build a vector.
2796   SmallVector<SDValue, 16> Ops(WidenNumElts);
2797   EVT EltVT = VT.getVectorElementType();
2798   unsigned NumElts = VT.getVectorNumElements();
2799   unsigned i;
2800   for (i=0; i < NumElts; ++i)
2801     Ops[i] =
2802         DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2803                     DAG.getConstant(IdxVal + i, dl,
2804                                     TLI.getVectorIdxTy(DAG.getDataLayout())));
2805
2806   SDValue UndefVal = DAG.getUNDEF(EltVT);
2807   for (; i < WidenNumElts; ++i)
2808     Ops[i] = UndefVal;
2809   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, Ops);
2810 }
2811
2812 SDValue DAGTypeLegalizer::WidenVecRes_INSERT_VECTOR_ELT(SDNode *N) {
2813   SDValue InOp = GetWidenedVector(N->getOperand(0));
2814   return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N),
2815                      InOp.getValueType(), InOp,
2816                      N->getOperand(1), N->getOperand(2));
2817 }
2818
2819 SDValue DAGTypeLegalizer::WidenVecRes_LOAD(SDNode *N) {
2820   LoadSDNode *LD = cast<LoadSDNode>(N);
2821   ISD::LoadExtType ExtType = LD->getExtensionType();
2822
2823   SDValue Result;
2824   SmallVector<SDValue, 16> LdChain;  // Chain for the series of load
2825   if (ExtType != ISD::NON_EXTLOAD)
2826     Result = GenWidenVectorExtLoads(LdChain, LD, ExtType);
2827   else
2828     Result = GenWidenVectorLoads(LdChain, LD);
2829
2830   // If we generate a single load, we can use that for the chain.  Otherwise,
2831   // build a factor node to remember the multiple loads are independent and
2832   // chain to that.
2833   SDValue NewChain;
2834   if (LdChain.size() == 1)
2835     NewChain = LdChain[0];
2836   else
2837     NewChain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other, LdChain);
2838
2839   // Modified the chain - switch anything that used the old chain to use
2840   // the new one.
2841   ReplaceValueWith(SDValue(N, 1), NewChain);
2842
2843   return Result;
2844 }
2845
2846 SDValue DAGTypeLegalizer::WidenVecRes_MLOAD(MaskedLoadSDNode *N) {
2847
2848   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),N->getValueType(0));
2849   SDValue Mask = N->getMask();
2850   EVT MaskVT = Mask.getValueType();
2851   SDValue Src0 = GetWidenedVector(N->getSrc0());
2852   ISD::LoadExtType ExtType = N->getExtensionType();
2853   SDLoc dl(N);
2854
2855   if (getTypeAction(MaskVT) == TargetLowering::TypeWidenVector)
2856     Mask = GetWidenedVector(Mask);
2857   else {
2858     EVT BoolVT = getSetCCResultType(WidenVT);
2859
2860     // We can't use ModifyToType() because we should fill the mask with
2861     // zeroes
2862     unsigned WidenNumElts = BoolVT.getVectorNumElements();
2863     unsigned MaskNumElts = MaskVT.getVectorNumElements();
2864
2865     unsigned NumConcat = WidenNumElts / MaskNumElts;
2866     SmallVector<SDValue, 16> Ops(NumConcat);
2867     SDValue ZeroVal = DAG.getConstant(0, dl, MaskVT);
2868     Ops[0] = Mask;
2869     for (unsigned i = 1; i != NumConcat; ++i)
2870       Ops[i] = ZeroVal;
2871
2872     Mask = DAG.getNode(ISD::CONCAT_VECTORS, dl, BoolVT, Ops);
2873   }
2874
2875   SDValue Res = DAG.getMaskedLoad(WidenVT, dl, N->getChain(), N->getBasePtr(),
2876                                   Mask, Src0, N->getMemoryVT(),
2877                                   N->getMemOperand(), ExtType);
2878   // Legalize the chain result - switch anything that used the old chain to
2879   // use the new one.
2880   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
2881   return Res;
2882 }
2883
2884 SDValue DAGTypeLegalizer::WidenVecRes_MGATHER(MaskedGatherSDNode *N) {
2885
2886   EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2887   SDValue Mask = N->getMask();
2888   SDValue Src0 = GetWidenedVector(N->getValue());
2889   unsigned NumElts = WideVT.getVectorNumElements();
2890   SDLoc dl(N);
2891
2892   // The mask should be widened as well
2893   Mask = WidenTargetBoolean(Mask, WideVT, true);
2894
2895   // Widen the Index operand
2896   SDValue Index = N->getIndex();
2897   EVT WideIndexVT = EVT::getVectorVT(*DAG.getContext(),
2898                                      Index.getValueType().getScalarType(),
2899                                      NumElts);
2900   Index = ModifyToType(Index, WideIndexVT);
2901   SDValue Ops[] = { N->getChain(), Src0, Mask, N->getBasePtr(), Index };
2902   SDValue Res = DAG.getMaskedGather(DAG.getVTList(WideVT, MVT::Other),
2903                                     N->getMemoryVT(), dl, Ops,
2904                                     N->getMemOperand());
2905
2906   // Legalize the chain result - switch anything that used the old chain to
2907   // use the new one.
2908   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
2909   return Res;
2910 }
2911
2912 SDValue DAGTypeLegalizer::WidenVecRes_SCALAR_TO_VECTOR(SDNode *N) {
2913   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2914   return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N),
2915                      WidenVT, N->getOperand(0));
2916 }
2917
2918 SDValue DAGTypeLegalizer::WidenVecRes_SELECT(SDNode *N) {
2919   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2920   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2921
2922   SDValue Cond1 = N->getOperand(0);
2923   EVT CondVT = Cond1.getValueType();
2924   if (CondVT.isVector()) {
2925     EVT CondEltVT = CondVT.getVectorElementType();
2926     EVT CondWidenVT =  EVT::getVectorVT(*DAG.getContext(),
2927                                         CondEltVT, WidenNumElts);
2928     if (getTypeAction(CondVT) == TargetLowering::TypeWidenVector)
2929       Cond1 = GetWidenedVector(Cond1);
2930
2931     // If we have to split the condition there is no point in widening the
2932     // select. This would result in an cycle of widening the select ->
2933     // widening the condition operand -> splitting the condition operand ->
2934     // splitting the select -> widening the select. Instead split this select
2935     // further and widen the resulting type.
2936     if (getTypeAction(CondVT) == TargetLowering::TypeSplitVector) {
2937       SDValue SplitSelect = SplitVecOp_VSELECT(N, 0);
2938       SDValue Res = ModifyToType(SplitSelect, WidenVT);
2939       return Res;
2940     }
2941
2942     if (Cond1.getValueType() != CondWidenVT)
2943       Cond1 = ModifyToType(Cond1, CondWidenVT);
2944   }
2945
2946   SDValue InOp1 = GetWidenedVector(N->getOperand(1));
2947   SDValue InOp2 = GetWidenedVector(N->getOperand(2));
2948   assert(InOp1.getValueType() == WidenVT && InOp2.getValueType() == WidenVT);
2949   return DAG.getNode(N->getOpcode(), SDLoc(N),
2950                      WidenVT, Cond1, InOp1, InOp2);
2951 }
2952
2953 SDValue DAGTypeLegalizer::WidenVecRes_SELECT_CC(SDNode *N) {
2954   SDValue InOp1 = GetWidenedVector(N->getOperand(2));
2955   SDValue InOp2 = GetWidenedVector(N->getOperand(3));
2956   return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
2957                      InOp1.getValueType(), N->getOperand(0),
2958                      N->getOperand(1), InOp1, InOp2, N->getOperand(4));
2959 }
2960
2961 SDValue DAGTypeLegalizer::WidenVecRes_SETCC(SDNode *N) {
2962   assert(N->getValueType(0).isVector() ==
2963          N->getOperand(0).getValueType().isVector() &&
2964          "Scalar/Vector type mismatch");
2965   if (N->getValueType(0).isVector()) return WidenVecRes_VSETCC(N);
2966
2967   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2968   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2969   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2970   return DAG.getNode(ISD::SETCC, SDLoc(N), WidenVT,
2971                      InOp1, InOp2, N->getOperand(2));
2972 }
2973
2974 SDValue DAGTypeLegalizer::WidenVecRes_UNDEF(SDNode *N) {
2975  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2976  return DAG.getUNDEF(WidenVT);
2977 }
2978
2979 SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
2980   EVT VT = N->getValueType(0);
2981   SDLoc dl(N);
2982
2983   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2984   unsigned NumElts = VT.getVectorNumElements();
2985   unsigned WidenNumElts = WidenVT.getVectorNumElements();
2986
2987   SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2988   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2989
2990   // Adjust mask based on new input vector length.
2991   SmallVector<int, 16> NewMask;
2992   for (unsigned i = 0; i != NumElts; ++i) {
2993     int Idx = N->getMaskElt(i);
2994     if (Idx < (int)NumElts)
2995       NewMask.push_back(Idx);
2996     else
2997       NewMask.push_back(Idx - NumElts + WidenNumElts);
2998   }
2999   for (unsigned i = NumElts; i != WidenNumElts; ++i)
3000     NewMask.push_back(-1);
3001   return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, &NewMask[0]);
3002 }
3003
3004 SDValue DAGTypeLegalizer::WidenVecRes_VSETCC(SDNode *N) {
3005   assert(N->getValueType(0).isVector() &&
3006          N->getOperand(0).getValueType().isVector() &&
3007          "Operands must be vectors");
3008   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3009   unsigned WidenNumElts = WidenVT.getVectorNumElements();
3010
3011   SDValue InOp1 = N->getOperand(0);
3012   EVT InVT = InOp1.getValueType();
3013   assert(InVT.isVector() && "can not widen non-vector type");
3014   EVT WidenInVT = EVT::getVectorVT(*DAG.getContext(),
3015                                    InVT.getVectorElementType(), WidenNumElts);
3016
3017   // The input and output types often differ here, and it could be that while
3018   // we'd prefer to widen the result type, the input operands have been split.
3019   // In this case, we also need to split the result of this node as well.
3020   if (getTypeAction(InVT) == TargetLowering::TypeSplitVector) {
3021     SDValue SplitVSetCC = SplitVecOp_VSETCC(N);
3022     SDValue Res = ModifyToType(SplitVSetCC, WidenVT);
3023     return Res;
3024   }
3025
3026   InOp1 = GetWidenedVector(InOp1);
3027   SDValue InOp2 = GetWidenedVector(N->getOperand(1));
3028
3029   // Assume that the input and output will be widen appropriately.  If not,
3030   // we will have to unroll it at some point.
3031   assert(InOp1.getValueType() == WidenInVT &&
3032          InOp2.getValueType() == WidenInVT &&
3033          "Input not widened to expected type!");
3034   (void)WidenInVT;
3035   return DAG.getNode(ISD::SETCC, SDLoc(N),
3036                      WidenVT, InOp1, InOp2, N->getOperand(2));
3037 }
3038
3039
3040 //===----------------------------------------------------------------------===//
3041 // Widen Vector Operand
3042 //===----------------------------------------------------------------------===//
3043 bool DAGTypeLegalizer::WidenVectorOperand(SDNode *N, unsigned OpNo) {
3044   DEBUG(dbgs() << "Widen node operand " << OpNo << ": ";
3045         N->dump(&DAG);
3046         dbgs() << "\n");
3047   SDValue Res = SDValue();
3048
3049   // See if the target wants to custom widen this node.
3050   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
3051     return false;
3052
3053   switch (N->getOpcode()) {
3054   default:
3055 #ifndef NDEBUG
3056     dbgs() << "WidenVectorOperand op #" << OpNo << ": ";
3057     N->dump(&DAG);
3058     dbgs() << "\n";
3059 #endif
3060     llvm_unreachable("Do not know how to widen this operator's operand!");
3061
3062   case ISD::BITCAST:            Res = WidenVecOp_BITCAST(N); break;
3063   case ISD::CONCAT_VECTORS:     Res = WidenVecOp_CONCAT_VECTORS(N); break;
3064   case ISD::EXTRACT_SUBVECTOR:  Res = WidenVecOp_EXTRACT_SUBVECTOR(N); break;
3065   case ISD::EXTRACT_VECTOR_ELT: Res = WidenVecOp_EXTRACT_VECTOR_ELT(N); break;
3066   case ISD::STORE:              Res = WidenVecOp_STORE(N); break;
3067   case ISD::MSTORE:             Res = WidenVecOp_MSTORE(N, OpNo); break;
3068   case ISD::MSCATTER:           Res = WidenVecOp_MSCATTER(N, OpNo); break;
3069   case ISD::SETCC:              Res = WidenVecOp_SETCC(N); break;
3070   case ISD::FCOPYSIGN:          Res = WidenVecOp_FCOPYSIGN(N); break;
3071
3072   case ISD::ANY_EXTEND:
3073   case ISD::SIGN_EXTEND:
3074   case ISD::ZERO_EXTEND:
3075     Res = WidenVecOp_EXTEND(N);
3076     break;
3077
3078   case ISD::FP_EXTEND:
3079   case ISD::FP_TO_SINT:
3080   case ISD::FP_TO_UINT:
3081   case ISD::SINT_TO_FP:
3082   case ISD::UINT_TO_FP:
3083   case ISD::TRUNCATE:
3084     Res = WidenVecOp_Convert(N);
3085     break;
3086   }
3087
3088   // If Res is null, the sub-method took care of registering the result.
3089   if (!Res.getNode()) return false;
3090
3091   // If the result is N, the sub-method updated N in place.  Tell the legalizer
3092   // core about this.
3093   if (Res.getNode() == N)
3094     return true;
3095
3096
3097   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
3098          "Invalid operand expansion");
3099
3100   ReplaceValueWith(SDValue(N, 0), Res);
3101   return false;
3102 }
3103
3104 SDValue DAGTypeLegalizer::WidenVecOp_EXTEND(SDNode *N) {
3105   SDLoc DL(N);
3106   EVT VT = N->getValueType(0);
3107
3108   SDValue InOp = N->getOperand(0);
3109   // If some legalization strategy other than widening is used on the operand,
3110   // we can't safely assume that just extending the low lanes is the correct
3111   // transformation.
3112   if (getTypeAction(InOp.getValueType()) != TargetLowering::TypeWidenVector)
3113     return WidenVecOp_Convert(N);
3114   InOp = GetWidenedVector(InOp);
3115   assert(VT.getVectorNumElements() <
3116              InOp.getValueType().getVectorNumElements() &&
3117          "Input wasn't widened!");
3118
3119   // We may need to further widen the operand until it has the same total
3120   // vector size as the result.
3121   EVT InVT = InOp.getValueType();
3122   if (InVT.getSizeInBits() != VT.getSizeInBits()) {
3123     EVT InEltVT = InVT.getVectorElementType();
3124     for (int i = MVT::FIRST_VECTOR_VALUETYPE, e = MVT::LAST_VECTOR_VALUETYPE; i < e; ++i) {
3125       EVT FixedVT = (MVT::SimpleValueType)i;
3126       EVT FixedEltVT = FixedVT.getVectorElementType();
3127       if (TLI.isTypeLegal(FixedVT) &&
3128           FixedVT.getSizeInBits() == VT.getSizeInBits() &&
3129           FixedEltVT == InEltVT) {
3130         assert(FixedVT.getVectorNumElements() >= VT.getVectorNumElements() &&
3131                "Not enough elements in the fixed type for the operand!");
3132         assert(FixedVT.getVectorNumElements() != InVT.getVectorNumElements() &&
3133                "We can't have the same type as we started with!");
3134         if (FixedVT.getVectorNumElements() > InVT.getVectorNumElements())
3135           InOp = DAG.getNode(
3136               ISD::INSERT_SUBVECTOR, DL, FixedVT, DAG.getUNDEF(FixedVT), InOp,
3137               DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
3138         else
3139           InOp = DAG.getNode(
3140               ISD::EXTRACT_SUBVECTOR, DL, FixedVT, InOp,
3141               DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
3142         break;
3143       }
3144     }
3145     InVT = InOp.getValueType();
3146     if (InVT.getSizeInBits() != VT.getSizeInBits())
3147       // We couldn't find a legal vector type that was a widening of the input
3148       // and could be extended in-register to the result type, so we have to
3149       // scalarize.
3150       return WidenVecOp_Convert(N);
3151   }
3152
3153   // Use special DAG nodes to represent the operation of extending the
3154   // low lanes.
3155   switch (N->getOpcode()) {
3156   default:
3157     llvm_unreachable("Extend legalization on on extend operation!");
3158   case ISD::ANY_EXTEND:
3159     return DAG.getAnyExtendVectorInReg(InOp, DL, VT);
3160   case ISD::SIGN_EXTEND:
3161     return DAG.getSignExtendVectorInReg(InOp, DL, VT);
3162   case ISD::ZERO_EXTEND:
3163     return DAG.getZeroExtendVectorInReg(InOp, DL, VT);
3164   }
3165 }
3166
3167 SDValue DAGTypeLegalizer::WidenVecOp_FCOPYSIGN(SDNode *N) {
3168   // The result (and first input) is legal, but the second input is illegal.
3169   // We can't do much to fix that, so just unroll and let the extracts off of
3170   // the second input be widened as needed later.
3171   return DAG.UnrollVectorOp(N);
3172 }
3173
3174 SDValue DAGTypeLegalizer::WidenVecOp_Convert(SDNode *N) {
3175   // Since the result is legal and the input is illegal, it is unlikely that we
3176   // can fix the input to a legal type so unroll the convert into some scalar
3177   // code and create a nasty build vector.
3178   EVT VT = N->getValueType(0);
3179   EVT EltVT = VT.getVectorElementType();
3180   SDLoc dl(N);
3181   unsigned NumElts = VT.getVectorNumElements();
3182   SDValue InOp = N->getOperand(0);
3183   if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
3184     InOp = GetWidenedVector(InOp);
3185   EVT InVT = InOp.getValueType();
3186   EVT InEltVT = InVT.getVectorElementType();
3187
3188   unsigned Opcode = N->getOpcode();
3189   SmallVector<SDValue, 16> Ops(NumElts);
3190   for (unsigned i=0; i < NumElts; ++i)
3191     Ops[i] = DAG.getNode(
3192         Opcode, dl, EltVT,
3193         DAG.getNode(
3194             ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
3195             DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
3196
3197   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
3198 }
3199
3200 SDValue DAGTypeLegalizer::WidenVecOp_BITCAST(SDNode *N) {
3201   EVT VT = N->getValueType(0);
3202   SDValue InOp = GetWidenedVector(N->getOperand(0));
3203   EVT InWidenVT = InOp.getValueType();
3204   SDLoc dl(N);
3205
3206   // Check if we can convert between two legal vector types and extract.
3207   unsigned InWidenSize = InWidenVT.getSizeInBits();
3208   unsigned Size = VT.getSizeInBits();
3209   // x86mmx is not an acceptable vector element type, so don't try.
3210   if (InWidenSize % Size == 0 && !VT.isVector() && VT != MVT::x86mmx) {
3211     unsigned NewNumElts = InWidenSize / Size;
3212     EVT NewVT = EVT::getVectorVT(*DAG.getContext(), VT, NewNumElts);
3213     if (TLI.isTypeLegal(NewVT)) {
3214       SDValue BitOp = DAG.getNode(ISD::BITCAST, dl, NewVT, InOp);
3215       return DAG.getNode(
3216           ISD::EXTRACT_VECTOR_ELT, dl, VT, BitOp,
3217           DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3218     }
3219   }
3220
3221   return CreateStackStoreLoad(InOp, VT);
3222 }
3223
3224 SDValue DAGTypeLegalizer::WidenVecOp_CONCAT_VECTORS(SDNode *N) {
3225   // If the input vector is not legal, it is likely that we will not find a
3226   // legal vector of the same size. Replace the concatenate vector with a
3227   // nasty build vector.
3228   EVT VT = N->getValueType(0);
3229   EVT EltVT = VT.getVectorElementType();
3230   SDLoc dl(N);
3231   unsigned NumElts = VT.getVectorNumElements();
3232   SmallVector<SDValue, 16> Ops(NumElts);
3233
3234   EVT InVT = N->getOperand(0).getValueType();
3235   unsigned NumInElts = InVT.getVectorNumElements();
3236
3237   unsigned Idx = 0;
3238   unsigned NumOperands = N->getNumOperands();
3239   for (unsigned i=0; i < NumOperands; ++i) {
3240     SDValue InOp = N->getOperand(i);
3241     if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
3242       InOp = GetWidenedVector(InOp);
3243     for (unsigned j=0; j < NumInElts; ++j)
3244       Ops[Idx++] = DAG.getNode(
3245           ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
3246           DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3247   }
3248   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
3249 }
3250
3251 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
3252   SDValue InOp = GetWidenedVector(N->getOperand(0));
3253   return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N),
3254                      N->getValueType(0), InOp, N->getOperand(1));
3255 }
3256
3257 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
3258   SDValue InOp = GetWidenedVector(N->getOperand(0));
3259   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
3260                      N->getValueType(0), InOp, N->getOperand(1));
3261 }
3262
3263 SDValue DAGTypeLegalizer::WidenVecOp_STORE(SDNode *N) {
3264   // We have to widen the value, but we want only to store the original
3265   // vector type.
3266   StoreSDNode *ST = cast<StoreSDNode>(N);
3267
3268   SmallVector<SDValue, 16> StChain;
3269   if (ST->isTruncatingStore())
3270     GenWidenVectorTruncStores(StChain, ST);
3271   else
3272     GenWidenVectorStores(StChain, ST);
3273
3274   if (StChain.size() == 1)
3275     return StChain[0];
3276   else
3277     return DAG.getNode(ISD::TokenFactor, SDLoc(ST), MVT::Other, StChain);
3278 }
3279
3280 SDValue DAGTypeLegalizer::WidenVecOp_MSTORE(SDNode *N, unsigned OpNo) {
3281   MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
3282   SDValue Mask = MST->getMask();
3283   EVT MaskVT = Mask.getValueType();
3284   SDValue StVal = MST->getValue();
3285   // Widen the value
3286   SDValue WideVal = GetWidenedVector(StVal);
3287   SDLoc dl(N);
3288
3289   if (OpNo == 2 || getTypeAction(MaskVT) == TargetLowering::TypeWidenVector)
3290     Mask = GetWidenedVector(Mask);
3291   else {
3292     // The mask should be widened as well.
3293     EVT BoolVT = getSetCCResultType(WideVal.getValueType());
3294     // We can't use ModifyToType() because we should fill the mask with
3295     // zeroes.
3296     unsigned WidenNumElts = BoolVT.getVectorNumElements();
3297     unsigned MaskNumElts = MaskVT.getVectorNumElements();
3298
3299     unsigned NumConcat = WidenNumElts / MaskNumElts;
3300     SmallVector<SDValue, 16> Ops(NumConcat);
3301     SDValue ZeroVal = DAG.getConstant(0, dl, MaskVT);
3302     Ops[0] = Mask;
3303     for (unsigned i = 1; i != NumConcat; ++i)
3304       Ops[i] = ZeroVal;
3305
3306     Mask = DAG.getNode(ISD::CONCAT_VECTORS, dl, BoolVT, Ops);
3307   }
3308   assert(Mask.getValueType().getVectorNumElements() ==
3309          WideVal.getValueType().getVectorNumElements() &&
3310          "Mask and data vectors should have the same number of elements");
3311   return DAG.getMaskedStore(MST->getChain(), dl, WideVal, MST->getBasePtr(),
3312                             Mask, MST->getMemoryVT(), MST->getMemOperand(),
3313                             false);
3314 }
3315
3316 SDValue DAGTypeLegalizer::WidenVecOp_MSCATTER(SDNode *N, unsigned OpNo) {
3317   assert(OpNo == 1 && "Can widen only data operand of mscatter");
3318   MaskedScatterSDNode *MSC = cast<MaskedScatterSDNode>(N);
3319   SDValue DataOp = MSC->getValue();
3320   SDValue Mask = MSC->getMask();
3321
3322   // Widen the value.
3323   SDValue WideVal = GetWidenedVector(DataOp);
3324   EVT WideVT = WideVal.getValueType();
3325   unsigned NumElts = WideVal.getValueType().getVectorNumElements();
3326   SDLoc dl(N);
3327
3328   // The mask should be widened as well.
3329   Mask = WidenTargetBoolean(Mask, WideVT, true);
3330
3331   // Widen index.
3332   SDValue Index = MSC->getIndex();
3333   EVT WideIndexVT = EVT::getVectorVT(*DAG.getContext(),
3334                                      Index.getValueType().getScalarType(),
3335                                      NumElts);
3336   Index = ModifyToType(Index, WideIndexVT);
3337
3338   SDValue Ops[] = {MSC->getChain(), WideVal, Mask, MSC->getBasePtr(), Index};
3339   return DAG.getMaskedScatter(DAG.getVTList(MVT::Other),
3340                               MSC->getMemoryVT(), dl, Ops,
3341                               MSC->getMemOperand());
3342 }
3343
3344 SDValue DAGTypeLegalizer::WidenVecOp_SETCC(SDNode *N) {
3345   SDValue InOp0 = GetWidenedVector(N->getOperand(0));
3346   SDValue InOp1 = GetWidenedVector(N->getOperand(1));
3347   SDLoc dl(N);
3348
3349   // WARNING: In this code we widen the compare instruction with garbage.
3350   // This garbage may contain denormal floats which may be slow. Is this a real
3351   // concern ? Should we zero the unused lanes if this is a float compare ?
3352
3353   // Get a new SETCC node to compare the newly widened operands.
3354   // Only some of the compared elements are legal.
3355   EVT SVT = TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3356                                    InOp0.getValueType());
3357   SDValue WideSETCC = DAG.getNode(ISD::SETCC, SDLoc(N),
3358                      SVT, InOp0, InOp1, N->getOperand(2));
3359
3360   // Extract the needed results from the result vector.
3361   EVT ResVT = EVT::getVectorVT(*DAG.getContext(),
3362                                SVT.getVectorElementType(),
3363                                N->getValueType(0).getVectorNumElements());
3364   SDValue CC = DAG.getNode(
3365       ISD::EXTRACT_SUBVECTOR, dl, ResVT, WideSETCC,
3366       DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3367
3368   return PromoteTargetBoolean(CC, N->getValueType(0));
3369 }
3370
3371
3372 //===----------------------------------------------------------------------===//
3373 // Vector Widening Utilities
3374 //===----------------------------------------------------------------------===//
3375
3376 // Utility function to find the type to chop up a widen vector for load/store
3377 //  TLI:       Target lowering used to determine legal types.
3378 //  Width:     Width left need to load/store.
3379 //  WidenVT:   The widen vector type to load to/store from
3380 //  Align:     If 0, don't allow use of a wider type
3381 //  WidenEx:   If Align is not 0, the amount additional we can load/store from.
3382
3383 static EVT FindMemType(SelectionDAG& DAG, const TargetLowering &TLI,
3384                        unsigned Width, EVT WidenVT,
3385                        unsigned Align = 0, unsigned WidenEx = 0) {
3386   EVT WidenEltVT = WidenVT.getVectorElementType();
3387   unsigned WidenWidth = WidenVT.getSizeInBits();
3388   unsigned WidenEltWidth = WidenEltVT.getSizeInBits();
3389   unsigned AlignInBits = Align*8;
3390
3391   // If we have one element to load/store, return it.
3392   EVT RetVT = WidenEltVT;
3393   if (Width == WidenEltWidth)
3394     return RetVT;
3395
3396   // See if there is larger legal integer than the element type to load/store.
3397   unsigned VT;
3398   for (VT = (unsigned)MVT::LAST_INTEGER_VALUETYPE;
3399        VT >= (unsigned)MVT::FIRST_INTEGER_VALUETYPE; --VT) {
3400     EVT MemVT((MVT::SimpleValueType) VT);
3401     unsigned MemVTWidth = MemVT.getSizeInBits();
3402     if (MemVT.getSizeInBits() <= WidenEltWidth)
3403       break;
3404     auto Action = TLI.getTypeAction(*DAG.getContext(), MemVT);
3405     if ((Action == TargetLowering::TypeLegal ||
3406          Action == TargetLowering::TypePromoteInteger) &&
3407         (WidenWidth % MemVTWidth) == 0 &&
3408         isPowerOf2_32(WidenWidth / MemVTWidth) &&
3409         (MemVTWidth <= Width ||
3410          (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
3411       RetVT = MemVT;
3412       break;
3413     }
3414   }
3415
3416   // See if there is a larger vector type to load/store that has the same vector
3417   // element type and is evenly divisible with the WidenVT.
3418   for (VT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
3419        VT >= (unsigned)MVT::FIRST_VECTOR_VALUETYPE; --VT) {
3420     EVT MemVT = (MVT::SimpleValueType) VT;
3421     unsigned MemVTWidth = MemVT.getSizeInBits();
3422     if (TLI.isTypeLegal(MemVT) && WidenEltVT == MemVT.getVectorElementType() &&
3423         (WidenWidth % MemVTWidth) == 0 &&
3424         isPowerOf2_32(WidenWidth / MemVTWidth) &&
3425         (MemVTWidth <= Width ||
3426          (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
3427       if (RetVT.getSizeInBits() < MemVTWidth || MemVT == WidenVT)
3428         return MemVT;
3429     }
3430   }
3431
3432   return RetVT;
3433 }
3434
3435 // Builds a vector type from scalar loads
3436 //  VecTy: Resulting Vector type
3437 //  LDOps: Load operators to build a vector type
3438 //  [Start,End) the list of loads to use.
3439 static SDValue BuildVectorFromScalar(SelectionDAG& DAG, EVT VecTy,
3440                                      SmallVectorImpl<SDValue> &LdOps,
3441                                      unsigned Start, unsigned End) {
3442   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3443   SDLoc dl(LdOps[Start]);
3444   EVT LdTy = LdOps[Start].getValueType();
3445   unsigned Width = VecTy.getSizeInBits();
3446   unsigned NumElts = Width / LdTy.getSizeInBits();
3447   EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), LdTy, NumElts);
3448
3449   unsigned Idx = 1;
3450   SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT,LdOps[Start]);
3451
3452   for (unsigned i = Start + 1; i != End; ++i) {
3453     EVT NewLdTy = LdOps[i].getValueType();
3454     if (NewLdTy != LdTy) {
3455       NumElts = Width / NewLdTy.getSizeInBits();
3456       NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewLdTy, NumElts);
3457       VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, VecOp);
3458       // Readjust position and vector position based on new load type.
3459       Idx = Idx * LdTy.getSizeInBits() / NewLdTy.getSizeInBits();
3460       LdTy = NewLdTy;
3461     }
3462     VecOp = DAG.getNode(
3463         ISD::INSERT_VECTOR_ELT, dl, NewVecVT, VecOp, LdOps[i],
3464         DAG.getConstant(Idx++, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3465   }
3466   return DAG.getNode(ISD::BITCAST, dl, VecTy, VecOp);
3467 }
3468
3469 SDValue DAGTypeLegalizer::GenWidenVectorLoads(SmallVectorImpl<SDValue> &LdChain,
3470                                               LoadSDNode *LD) {
3471   // The strategy assumes that we can efficiently load power-of-two widths.
3472   // The routine chops the vector into the largest vector loads with the same
3473   // element type or scalar loads and then recombines it to the widen vector
3474   // type.
3475   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
3476   unsigned WidenWidth = WidenVT.getSizeInBits();
3477   EVT LdVT    = LD->getMemoryVT();
3478   SDLoc dl(LD);
3479   assert(LdVT.isVector() && WidenVT.isVector());
3480   assert(LdVT.getVectorElementType() == WidenVT.getVectorElementType());
3481
3482   // Load information
3483   SDValue   Chain = LD->getChain();
3484   SDValue   BasePtr = LD->getBasePtr();
3485   unsigned  Align    = LD->getAlignment();
3486   bool      isVolatile = LD->isVolatile();
3487   bool      isNonTemporal = LD->isNonTemporal();
3488   bool      isInvariant = LD->isInvariant();
3489   AAMDNodes AAInfo = LD->getAAInfo();
3490
3491   int LdWidth = LdVT.getSizeInBits();
3492   int WidthDiff = WidenWidth - LdWidth;
3493   unsigned LdAlign = (isVolatile) ? 0 : Align; // Allow wider loads.
3494
3495   // Find the vector type that can load from.
3496   EVT NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
3497   int NewVTWidth = NewVT.getSizeInBits();
3498   SDValue LdOp = DAG.getLoad(NewVT, dl, Chain, BasePtr, LD->getPointerInfo(),
3499                              isVolatile, isNonTemporal, isInvariant, Align,
3500                              AAInfo);
3501   LdChain.push_back(LdOp.getValue(1));
3502
3503   // Check if we can load the element with one instruction.
3504   if (LdWidth <= NewVTWidth) {
3505     if (!NewVT.isVector()) {
3506       unsigned NumElts = WidenWidth / NewVTWidth;
3507       EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
3508       SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT, LdOp);
3509       return DAG.getNode(ISD::BITCAST, dl, WidenVT, VecOp);
3510     }
3511     if (NewVT == WidenVT)
3512       return LdOp;
3513
3514     assert(WidenWidth % NewVTWidth == 0);
3515     unsigned NumConcat = WidenWidth / NewVTWidth;
3516     SmallVector<SDValue, 16> ConcatOps(NumConcat);
3517     SDValue UndefVal = DAG.getUNDEF(NewVT);
3518     ConcatOps[0] = LdOp;
3519     for (unsigned i = 1; i != NumConcat; ++i)
3520       ConcatOps[i] = UndefVal;
3521     return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, ConcatOps);
3522   }
3523
3524   // Load vector by using multiple loads from largest vector to scalar.
3525   SmallVector<SDValue, 16> LdOps;
3526   LdOps.push_back(LdOp);
3527
3528   LdWidth -= NewVTWidth;
3529   unsigned Offset = 0;
3530
3531   while (LdWidth > 0) {
3532     unsigned Increment = NewVTWidth / 8;
3533     Offset += Increment;
3534     BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
3535                           DAG.getConstant(Increment, dl, BasePtr.getValueType()));
3536
3537     SDValue L;
3538     if (LdWidth < NewVTWidth) {
3539       // The current type we are using is too large. Find a better size.
3540       NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
3541       NewVTWidth = NewVT.getSizeInBits();
3542       L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
3543                       LD->getPointerInfo().getWithOffset(Offset), isVolatile,
3544                       isNonTemporal, isInvariant, MinAlign(Align, Increment),
3545                       AAInfo);
3546       LdChain.push_back(L.getValue(1));
3547       if (L->getValueType(0).isVector()) {
3548         SmallVector<SDValue, 16> Loads;
3549         Loads.push_back(L);
3550         unsigned size = L->getValueSizeInBits(0);
3551         while (size < LdOp->getValueSizeInBits(0)) {
3552           Loads.push_back(DAG.getUNDEF(L->getValueType(0)));
3553           size += L->getValueSizeInBits(0);
3554         }
3555         L = DAG.getNode(ISD::CONCAT_VECTORS, dl, LdOp->getValueType(0), Loads);
3556       }
3557     } else {
3558       L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
3559                       LD->getPointerInfo().getWithOffset(Offset), isVolatile,
3560                       isNonTemporal, isInvariant, MinAlign(Align, Increment),
3561                       AAInfo);
3562       LdChain.push_back(L.getValue(1));
3563     }
3564
3565     LdOps.push_back(L);
3566
3567
3568     LdWidth -= NewVTWidth;
3569   }
3570
3571   // Build the vector from the load operations.
3572   unsigned End = LdOps.size();
3573   if (!LdOps[0].getValueType().isVector())
3574     // All the loads are scalar loads.
3575     return BuildVectorFromScalar(DAG, WidenVT, LdOps, 0, End);
3576
3577   // If the load contains vectors, build the vector using concat vector.
3578   // All of the vectors used to load are power-of-2, and the scalar loads can be
3579   // combined to make a power-of-2 vector.
3580   SmallVector<SDValue, 16> ConcatOps(End);
3581   int i = End - 1;
3582   int Idx = End;
3583   EVT LdTy = LdOps[i].getValueType();
3584   // First, combine the scalar loads to a vector.
3585   if (!LdTy.isVector())  {
3586     for (--i; i >= 0; --i) {
3587       LdTy = LdOps[i].getValueType();
3588       if (LdTy.isVector())
3589         break;
3590     }
3591     ConcatOps[--Idx] = BuildVectorFromScalar(DAG, LdTy, LdOps, i + 1, End);
3592   }
3593   ConcatOps[--Idx] = LdOps[i];
3594   for (--i; i >= 0; --i) {
3595     EVT NewLdTy = LdOps[i].getValueType();
3596     if (NewLdTy != LdTy) {
3597       // Create a larger vector.
3598       ConcatOps[End-1] = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewLdTy,
3599                                      makeArrayRef(&ConcatOps[Idx], End - Idx));
3600       Idx = End - 1;
3601       LdTy = NewLdTy;
3602     }
3603     ConcatOps[--Idx] = LdOps[i];
3604   }
3605
3606   if (WidenWidth == LdTy.getSizeInBits() * (End - Idx))
3607     return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
3608                        makeArrayRef(&ConcatOps[Idx], End - Idx));
3609
3610   // We need to fill the rest with undefs to build the vector.
3611   unsigned NumOps = WidenWidth / LdTy.getSizeInBits();
3612   SmallVector<SDValue, 16> WidenOps(NumOps);
3613   SDValue UndefVal = DAG.getUNDEF(LdTy);
3614   {
3615     unsigned i = 0;
3616     for (; i != End-Idx; ++i)
3617       WidenOps[i] = ConcatOps[Idx+i];
3618     for (; i != NumOps; ++i)
3619       WidenOps[i] = UndefVal;
3620   }
3621   return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, WidenOps);
3622 }
3623
3624 SDValue
3625 DAGTypeLegalizer::GenWidenVectorExtLoads(SmallVectorImpl<SDValue> &LdChain,
3626                                          LoadSDNode *LD,
3627                                          ISD::LoadExtType ExtType) {
3628   // For extension loads, it may not be more efficient to chop up the vector
3629   // and then extend it. Instead, we unroll the load and build a new vector.
3630   EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
3631   EVT LdVT    = LD->getMemoryVT();
3632   SDLoc dl(LD);
3633   assert(LdVT.isVector() && WidenVT.isVector());
3634
3635   // Load information
3636   SDValue   Chain = LD->getChain();
3637   SDValue   BasePtr = LD->getBasePtr();
3638   unsigned  Align    = LD->getAlignment();
3639   bool      isVolatile = LD->isVolatile();
3640   bool      isNonTemporal = LD->isNonTemporal();
3641   bool      isInvariant = LD->isInvariant();
3642   AAMDNodes AAInfo = LD->getAAInfo();
3643
3644   EVT EltVT = WidenVT.getVectorElementType();
3645   EVT LdEltVT = LdVT.getVectorElementType();
3646   unsigned NumElts = LdVT.getVectorNumElements();
3647
3648   // Load each element and widen.
3649   unsigned WidenNumElts = WidenVT.getVectorNumElements();
3650   SmallVector<SDValue, 16> Ops(WidenNumElts);
3651   unsigned Increment = LdEltVT.getSizeInBits() / 8;
3652   Ops[0] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, BasePtr,
3653                           LD->getPointerInfo(),
3654                           LdEltVT, isVolatile, isNonTemporal, isInvariant,
3655                           Align, AAInfo);
3656   LdChain.push_back(Ops[0].getValue(1));
3657   unsigned i = 0, Offset = Increment;
3658   for (i=1; i < NumElts; ++i, Offset += Increment) {
3659     SDValue NewBasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
3660                                      BasePtr,
3661                                      DAG.getConstant(Offset, dl,
3662                                                      BasePtr.getValueType()));
3663     Ops[i] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, NewBasePtr,
3664                             LD->getPointerInfo().getWithOffset(Offset), LdEltVT,
3665                             isVolatile, isNonTemporal, isInvariant, Align,
3666                             AAInfo);
3667     LdChain.push_back(Ops[i].getValue(1));
3668   }
3669
3670   // Fill the rest with undefs.
3671   SDValue UndefVal = DAG.getUNDEF(EltVT);
3672   for (; i != WidenNumElts; ++i)
3673     Ops[i] = UndefVal;
3674
3675   return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, Ops);
3676 }
3677
3678
3679 void DAGTypeLegalizer::GenWidenVectorStores(SmallVectorImpl<SDValue> &StChain,
3680                                             StoreSDNode *ST) {
3681   // The strategy assumes that we can efficiently store power-of-two widths.
3682   // The routine chops the vector into the largest vector stores with the same
3683   // element type or scalar stores.
3684   SDValue  Chain = ST->getChain();
3685   SDValue  BasePtr = ST->getBasePtr();
3686   unsigned Align = ST->getAlignment();
3687   bool     isVolatile = ST->isVolatile();
3688   bool     isNonTemporal = ST->isNonTemporal();
3689   AAMDNodes AAInfo = ST->getAAInfo();
3690   SDValue  ValOp = GetWidenedVector(ST->getValue());
3691   SDLoc dl(ST);
3692
3693   EVT StVT = ST->getMemoryVT();
3694   unsigned StWidth = StVT.getSizeInBits();
3695   EVT ValVT = ValOp.getValueType();
3696   unsigned ValWidth = ValVT.getSizeInBits();
3697   EVT ValEltVT = ValVT.getVectorElementType();
3698   unsigned ValEltWidth = ValEltVT.getSizeInBits();
3699   assert(StVT.getVectorElementType() == ValEltVT);
3700
3701   int Idx = 0;          // current index to store
3702   unsigned Offset = 0;  // offset from base to store
3703   while (StWidth != 0) {
3704     // Find the largest vector type we can store with.
3705     EVT NewVT = FindMemType(DAG, TLI, StWidth, ValVT);
3706     unsigned NewVTWidth = NewVT.getSizeInBits();
3707     unsigned Increment = NewVTWidth / 8;
3708     if (NewVT.isVector()) {
3709       unsigned NumVTElts = NewVT.getVectorNumElements();
3710       do {
3711         SDValue EOp = DAG.getNode(
3712             ISD::EXTRACT_SUBVECTOR, dl, NewVT, ValOp,
3713             DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3714         StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr,
3715                                     ST->getPointerInfo().getWithOffset(Offset),
3716                                        isVolatile, isNonTemporal,
3717                                        MinAlign(Align, Offset), AAInfo));
3718         StWidth -= NewVTWidth;
3719         Offset += Increment;
3720         Idx += NumVTElts;
3721         BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
3722                               DAG.getConstant(Increment, dl,
3723                                               BasePtr.getValueType()));
3724       } while (StWidth != 0 && StWidth >= NewVTWidth);
3725     } else {
3726       // Cast the vector to the scalar type we can store.
3727       unsigned NumElts = ValWidth / NewVTWidth;
3728       EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
3729       SDValue VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, ValOp);
3730       // Readjust index position based on new vector type.
3731       Idx = Idx * ValEltWidth / NewVTWidth;
3732       do {
3733         SDValue EOp = DAG.getNode(
3734             ISD::EXTRACT_VECTOR_ELT, dl, NewVT, VecOp,
3735             DAG.getConstant(Idx++, dl,
3736                             TLI.getVectorIdxTy(DAG.getDataLayout())));
3737         StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr,
3738                                     ST->getPointerInfo().getWithOffset(Offset),
3739                                        isVolatile, isNonTemporal,
3740                                        MinAlign(Align, Offset), AAInfo));
3741         StWidth -= NewVTWidth;
3742         Offset += Increment;
3743         BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
3744                               DAG.getConstant(Increment, dl,
3745                                               BasePtr.getValueType()));
3746       } while (StWidth != 0 && StWidth >= NewVTWidth);
3747       // Restore index back to be relative to the original widen element type.
3748       Idx = Idx * NewVTWidth / ValEltWidth;
3749     }
3750   }
3751 }
3752
3753 void
3754 DAGTypeLegalizer::GenWidenVectorTruncStores(SmallVectorImpl<SDValue> &StChain,
3755                                             StoreSDNode *ST) {
3756   // For extension loads, it may not be more efficient to truncate the vector
3757   // and then store it. Instead, we extract each element and then store it.
3758   SDValue  Chain = ST->getChain();
3759   SDValue  BasePtr = ST->getBasePtr();
3760   unsigned Align = ST->getAlignment();
3761   bool     isVolatile = ST->isVolatile();
3762   bool     isNonTemporal = ST->isNonTemporal();
3763   AAMDNodes AAInfo = ST->getAAInfo();
3764   SDValue  ValOp = GetWidenedVector(ST->getValue());
3765   SDLoc dl(ST);
3766
3767   EVT StVT = ST->getMemoryVT();
3768   EVT ValVT = ValOp.getValueType();
3769
3770   // It must be true that the wide vector type is bigger than where we need to
3771   // store.
3772   assert(StVT.isVector() && ValOp.getValueType().isVector());
3773   assert(StVT.bitsLT(ValOp.getValueType()));
3774
3775   // For truncating stores, we can not play the tricks of chopping legal vector
3776   // types and bitcast it to the right type. Instead, we unroll the store.
3777   EVT StEltVT  = StVT.getVectorElementType();
3778   EVT ValEltVT = ValVT.getVectorElementType();
3779   unsigned Increment = ValEltVT.getSizeInBits() / 8;
3780   unsigned NumElts = StVT.getVectorNumElements();
3781   SDValue EOp = DAG.getNode(
3782       ISD::EXTRACT_VECTOR_ELT, dl, ValEltVT, ValOp,
3783       DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3784   StChain.push_back(DAG.getTruncStore(Chain, dl, EOp, BasePtr,
3785                                       ST->getPointerInfo(), StEltVT,
3786                                       isVolatile, isNonTemporal, Align,
3787                                       AAInfo));
3788   unsigned Offset = Increment;
3789   for (unsigned i=1; i < NumElts; ++i, Offset += Increment) {
3790     SDValue NewBasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
3791                                      BasePtr,
3792                                      DAG.getConstant(Offset, dl,
3793                                                      BasePtr.getValueType()));
3794     SDValue EOp = DAG.getNode(
3795         ISD::EXTRACT_VECTOR_ELT, dl, ValEltVT, ValOp,
3796         DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3797     StChain.push_back(DAG.getTruncStore(Chain, dl, EOp, NewBasePtr,
3798                                       ST->getPointerInfo().getWithOffset(Offset),
3799                                         StEltVT, isVolatile, isNonTemporal,
3800                                         MinAlign(Align, Offset), AAInfo));
3801   }
3802 }
3803
3804 /// Modifies a vector input (widen or narrows) to a vector of NVT.  The
3805 /// input vector must have the same element type as NVT.
3806 /// FillWithZeroes specifies that the vector should be widened with zeroes.
3807 SDValue DAGTypeLegalizer::ModifyToType(SDValue InOp, EVT NVT,
3808                                        bool FillWithZeroes) {
3809   // Note that InOp might have been widened so it might already have
3810   // the right width or it might need be narrowed.
3811   EVT InVT = InOp.getValueType();
3812   assert(InVT.getVectorElementType() == NVT.getVectorElementType() &&
3813          "input and widen element type must match");
3814   SDLoc dl(InOp);
3815
3816   // Check if InOp already has the right width.
3817   if (InVT == NVT)
3818     return InOp;
3819
3820   unsigned InNumElts = InVT.getVectorNumElements();
3821   unsigned WidenNumElts = NVT.getVectorNumElements();
3822   if (WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0) {
3823     unsigned NumConcat = WidenNumElts / InNumElts;
3824     SmallVector<SDValue, 16> Ops(NumConcat);
3825     SDValue FillVal = FillWithZeroes ? DAG.getConstant(0, dl, InVT) :
3826       DAG.getUNDEF(InVT);
3827     Ops[0] = InOp;
3828     for (unsigned i = 1; i != NumConcat; ++i)
3829       Ops[i] = FillVal;
3830
3831     return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, Ops);
3832   }
3833
3834   if (WidenNumElts < InNumElts && InNumElts % WidenNumElts)
3835     return DAG.getNode(
3836         ISD::EXTRACT_SUBVECTOR, dl, NVT, InOp,
3837         DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3838
3839   // Fall back to extract and build.
3840   SmallVector<SDValue, 16> Ops(WidenNumElts);
3841   EVT EltVT = NVT.getVectorElementType();
3842   unsigned MinNumElts = std::min(WidenNumElts, InNumElts);
3843   unsigned Idx;
3844   for (Idx = 0; Idx < MinNumElts; ++Idx)
3845     Ops[Idx] = DAG.getNode(
3846         ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
3847         DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3848
3849   SDValue FillVal = FillWithZeroes ? DAG.getConstant(0, dl, EltVT) :
3850     DAG.getUNDEF(EltVT);
3851   for ( ; Idx < WidenNumElts; ++Idx)
3852     Ops[Idx] = FillVal;
3853   return DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, Ops);
3854 }