LLVM  10.0.0svn
LegalizeVectorTypes.cpp
Go to the documentation of this file.
1 //===------- LegalizeVectorTypes.cpp - Legalization of vector types -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file performs vector type splitting and scalarization for LegalizeTypes.
10 // Scalarization is the act of changing a computation in an illegal one-element
11 // vector type to be a computation in its scalar element type. For example,
12 // implementing <1 x f32> arithmetic in a scalar f32 register. This is needed
13 // as a base case when scalarizing vector arithmetic like <4 x f32>, which
14 // eventually decomposes to scalars if the target doesn't support v4f32 or v2f32
15 // types.
16 // Splitting is the act of changing a computation in an invalid vector type to
17 // be a computation in two vectors of half the size. For example, implementing
18 // <128 x f32> operations in terms of two <64 x f32> operations.
19 //
20 //===----------------------------------------------------------------------===//
21 
22 #include "LegalizeTypes.h"
23 #include "llvm/IR/DataLayout.h"
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "legalize-types"
29 
30 //===----------------------------------------------------------------------===//
31 // Result Vector Scalarization: <1 x ty> -> ty.
32 //===----------------------------------------------------------------------===//
33 
34 void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
35  LLVM_DEBUG(dbgs() << "Scalarize node result " << ResNo << ": "; N->dump(&DAG);
36  dbgs() << "\n");
37  SDValue R = SDValue();
38 
39  switch (N->getOpcode()) {
40  default:
41 #ifndef NDEBUG
42  dbgs() << "ScalarizeVectorResult #" << ResNo << ": ";
43  N->dump(&DAG);
44  dbgs() << "\n";
45 #endif
46  report_fatal_error("Do not know how to scalarize the result of this "
47  "operator!\n");
48 
49  case ISD::MERGE_VALUES: R = ScalarizeVecRes_MERGE_VALUES(N, ResNo);break;
50  case ISD::BITCAST: R = ScalarizeVecRes_BITCAST(N); break;
51  case ISD::BUILD_VECTOR: R = ScalarizeVecRes_BUILD_VECTOR(N); break;
52  case ISD::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
53  case ISD::STRICT_FP_ROUND: R = ScalarizeVecRes_STRICT_FP_ROUND(N); break;
54  case ISD::FP_ROUND: R = ScalarizeVecRes_FP_ROUND(N); break;
55  case ISD::FPOWI: R = ScalarizeVecRes_FPOWI(N); break;
56  case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
57  case ISD::LOAD: R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
58  case ISD::SCALAR_TO_VECTOR: R = ScalarizeVecRes_SCALAR_TO_VECTOR(N); break;
59  case ISD::SIGN_EXTEND_INREG: R = ScalarizeVecRes_InregOp(N); break;
60  case ISD::VSELECT: R = ScalarizeVecRes_VSELECT(N); break;
61  case ISD::SELECT: R = ScalarizeVecRes_SELECT(N); break;
62  case ISD::SELECT_CC: R = ScalarizeVecRes_SELECT_CC(N); break;
63  case ISD::SETCC: R = ScalarizeVecRes_SETCC(N); break;
64  case ISD::UNDEF: R = ScalarizeVecRes_UNDEF(N); break;
65  case ISD::VECTOR_SHUFFLE: R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
69  R = ScalarizeVecRes_VecInregOp(N);
70  break;
71  case ISD::ABS:
72  case ISD::ANY_EXTEND:
73  case ISD::BITREVERSE:
74  case ISD::BSWAP:
75  case ISD::CTLZ:
77  case ISD::CTPOP:
78  case ISD::CTTZ:
80  case ISD::FABS:
81  case ISD::FCEIL:
82  case ISD::FCOS:
83  case ISD::FEXP:
84  case ISD::FEXP2:
85  case ISD::FFLOOR:
86  case ISD::FLOG:
87  case ISD::FLOG10:
88  case ISD::FLOG2:
89  case ISD::FNEARBYINT:
90  case ISD::FNEG:
91  case ISD::FP_EXTEND:
92  case ISD::FP_TO_SINT:
93  case ISD::FP_TO_UINT:
94  case ISD::FRINT:
95  case ISD::FROUND:
96  case ISD::FSIN:
97  case ISD::FSQRT:
98  case ISD::FTRUNC:
99  case ISD::SIGN_EXTEND:
100  case ISD::SINT_TO_FP:
101  case ISD::TRUNCATE:
102  case ISD::UINT_TO_FP:
103  case ISD::ZERO_EXTEND:
104  case ISD::FCANONICALIZE:
105  R = ScalarizeVecRes_UnaryOp(N);
106  break;
107 
108  case ISD::ADD:
109  case ISD::AND:
110  case ISD::FADD:
111  case ISD::FCOPYSIGN:
112  case ISD::FDIV:
113  case ISD::FMUL:
114  case ISD::FMINNUM:
115  case ISD::FMAXNUM:
116  case ISD::FMINNUM_IEEE:
117  case ISD::FMAXNUM_IEEE:
118  case ISD::FMINIMUM:
119  case ISD::FMAXIMUM:
120  case ISD::SMIN:
121  case ISD::SMAX:
122  case ISD::UMIN:
123  case ISD::UMAX:
124 
125  case ISD::SADDSAT:
126  case ISD::UADDSAT:
127  case ISD::SSUBSAT:
128  case ISD::USUBSAT:
129 
130  case ISD::FPOW:
131  case ISD::FREM:
132  case ISD::FSUB:
133  case ISD::MUL:
134  case ISD::OR:
135  case ISD::SDIV:
136  case ISD::SREM:
137  case ISD::SUB:
138  case ISD::UDIV:
139  case ISD::UREM:
140  case ISD::XOR:
141  case ISD::SHL:
142  case ISD::SRA:
143  case ISD::SRL:
144  R = ScalarizeVecRes_BinOp(N);
145  break;
146  case ISD::FMA:
147  R = ScalarizeVecRes_TernaryOp(N);
148  break;
149  case ISD::STRICT_FADD:
150  case ISD::STRICT_FSUB:
151  case ISD::STRICT_FMUL:
152  case ISD::STRICT_FDIV:
153  case ISD::STRICT_FREM:
154  case ISD::STRICT_FSQRT:
155  case ISD::STRICT_FMA:
156  case ISD::STRICT_FPOW:
157  case ISD::STRICT_FPOWI:
158  case ISD::STRICT_FSIN:
159  case ISD::STRICT_FCOS:
160  case ISD::STRICT_FEXP:
161  case ISD::STRICT_FEXP2:
162  case ISD::STRICT_FLOG:
163  case ISD::STRICT_FLOG10:
164  case ISD::STRICT_FLOG2:
165  case ISD::STRICT_FRINT:
167  case ISD::STRICT_FMAXNUM:
168  case ISD::STRICT_FMINNUM:
169  case ISD::STRICT_FCEIL:
170  case ISD::STRICT_FFLOOR:
171  case ISD::STRICT_FROUND:
172  case ISD::STRICT_FTRUNC:
176  R = ScalarizeVecRes_StrictFPOp(N);
177  break;
178  case ISD::UADDO:
179  case ISD::SADDO:
180  case ISD::USUBO:
181  case ISD::SSUBO:
182  case ISD::UMULO:
183  case ISD::SMULO:
184  R = ScalarizeVecRes_OverflowOp(N, ResNo);
185  break;
186  case ISD::SMULFIX:
187  case ISD::SMULFIXSAT:
188  case ISD::UMULFIX:
189  case ISD::UMULFIXSAT:
190  R = ScalarizeVecRes_MULFIX(N);
191  break;
192  }
193 
194  // If R is null, the sub-method took care of registering the result.
195  if (R.getNode())
196  SetScalarizedVector(SDValue(N, ResNo), R);
197 }
198 
199 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
200  SDValue LHS = GetScalarizedVector(N->getOperand(0));
201  SDValue RHS = GetScalarizedVector(N->getOperand(1));
202  return DAG.getNode(N->getOpcode(), SDLoc(N),
203  LHS.getValueType(), LHS, RHS, N->getFlags());
204 }
205 
206 SDValue DAGTypeLegalizer::ScalarizeVecRes_TernaryOp(SDNode *N) {
207  SDValue Op0 = GetScalarizedVector(N->getOperand(0));
208  SDValue Op1 = GetScalarizedVector(N->getOperand(1));
209  SDValue Op2 = GetScalarizedVector(N->getOperand(2));
210  return DAG.getNode(N->getOpcode(), SDLoc(N),
211  Op0.getValueType(), Op0, Op1, Op2);
212 }
213 
214 SDValue DAGTypeLegalizer::ScalarizeVecRes_MULFIX(SDNode *N) {
215  SDValue Op0 = GetScalarizedVector(N->getOperand(0));
216  SDValue Op1 = GetScalarizedVector(N->getOperand(1));
217  SDValue Op2 = N->getOperand(2);
218  return DAG.getNode(N->getOpcode(), SDLoc(N), Op0.getValueType(), Op0, Op1,
219  Op2);
220 }
221 
222 SDValue DAGTypeLegalizer::ScalarizeVecRes_StrictFPOp(SDNode *N) {
224  unsigned NumOpers = N->getNumOperands();
225  SDValue Chain = N->getOperand(0);
226  EVT ValueVTs[] = {VT, MVT::Other};
227  SDLoc dl(N);
228 
230 
231  // The Chain is the first operand.
232  Opers.push_back(Chain);
233 
234  // Now process the remaining operands.
235  for (unsigned i = 1; i < NumOpers; ++i) {
236  SDValue Oper = N->getOperand(i);
237 
238  if (Oper.getValueType().isVector())
239  Oper = GetScalarizedVector(Oper);
240 
241  Opers.push_back(Oper);
242  }
243 
244  SDValue Result = DAG.getNode(N->getOpcode(), dl, ValueVTs, Opers);
245 
246  // Legalize the chain result - switch anything that used the old chain to
247  // use the new one.
248  ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
249  return Result;
250 }
251 
252 SDValue DAGTypeLegalizer::ScalarizeVecRes_OverflowOp(SDNode *N,
253  unsigned ResNo) {
254  SDLoc DL(N);
255  EVT ResVT = N->getValueType(0);
256  EVT OvVT = N->getValueType(1);
257 
258  SDValue ScalarLHS, ScalarRHS;
259  if (getTypeAction(ResVT) == TargetLowering::TypeScalarizeVector) {
260  ScalarLHS = GetScalarizedVector(N->getOperand(0));
261  ScalarRHS = GetScalarizedVector(N->getOperand(1));
262  } else {
263  SmallVector<SDValue, 1> ElemsLHS, ElemsRHS;
264  DAG.ExtractVectorElements(N->getOperand(0), ElemsLHS);
265  DAG.ExtractVectorElements(N->getOperand(1), ElemsRHS);
266  ScalarLHS = ElemsLHS[0];
267  ScalarRHS = ElemsRHS[0];
268  }
269 
270  SDVTList ScalarVTs = DAG.getVTList(
272  SDNode *ScalarNode = DAG.getNode(
273  N->getOpcode(), DL, ScalarVTs, ScalarLHS, ScalarRHS).getNode();
274 
275  // Replace the other vector result not being explicitly scalarized here.
276  unsigned OtherNo = 1 - ResNo;
277  EVT OtherVT = N->getValueType(OtherNo);
278  if (getTypeAction(OtherVT) == TargetLowering::TypeScalarizeVector) {
279  SetScalarizedVector(SDValue(N, OtherNo), SDValue(ScalarNode, OtherNo));
280  } else {
281  SDValue OtherVal = DAG.getNode(
282  ISD::SCALAR_TO_VECTOR, DL, OtherVT, SDValue(ScalarNode, OtherNo));
283  ReplaceValueWith(SDValue(N, OtherNo), OtherVal);
284  }
285 
286  return SDValue(ScalarNode, ResNo);
287 }
288 
289 SDValue DAGTypeLegalizer::ScalarizeVecRes_MERGE_VALUES(SDNode *N,
290  unsigned ResNo) {
291  SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
292  return GetScalarizedVector(Op);
293 }
294 
295 SDValue DAGTypeLegalizer::ScalarizeVecRes_BITCAST(SDNode *N) {
296  SDValue Op = N->getOperand(0);
297  if (Op.getValueType().isVector()
298  && Op.getValueType().getVectorNumElements() == 1
299  && !isSimpleLegalType(Op.getValueType()))
300  Op = GetScalarizedVector(Op);
301  EVT NewVT = N->getValueType(0).getVectorElementType();
302  return DAG.getNode(ISD::BITCAST, SDLoc(N),
303  NewVT, Op);
304 }
305 
306 SDValue DAGTypeLegalizer::ScalarizeVecRes_BUILD_VECTOR(SDNode *N) {
307  EVT EltVT = N->getValueType(0).getVectorElementType();
308  SDValue InOp = N->getOperand(0);
309  // The BUILD_VECTOR operands may be of wider element types and
310  // we may need to truncate them back to the requested return type.
311  if (EltVT.isInteger())
312  return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
313  return InOp;
314 }
315 
316 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
317  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
319  N->getOperand(0), N->getOperand(1));
320 }
321 
322 SDValue DAGTypeLegalizer::ScalarizeVecRes_FP_ROUND(SDNode *N) {
323  EVT NewVT = N->getValueType(0).getVectorElementType();
324  SDValue Op = GetScalarizedVector(N->getOperand(0));
325  return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
326  NewVT, Op, N->getOperand(1));
327 }
328 
329 SDValue DAGTypeLegalizer::ScalarizeVecRes_STRICT_FP_ROUND(SDNode *N) {
330  EVT NewVT = N->getValueType(0).getVectorElementType();
331  SDValue Op = GetScalarizedVector(N->getOperand(1));
333  { NewVT, MVT::Other },
334  { N->getOperand(0), Op, N->getOperand(2) });
335  // Legalize the chain result - switch anything that used the old chain to
336  // use the new one.
337  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
338  return Res;
339 }
340 
341 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
342  SDValue Op = GetScalarizedVector(N->getOperand(0));
343  return DAG.getNode(ISD::FPOWI, SDLoc(N),
344  Op.getValueType(), Op, N->getOperand(1));
345 }
346 
347 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
348  // The value to insert may have a wider type than the vector element type,
349  // so be sure to truncate it to the element type if necessary.
350  SDValue Op = N->getOperand(1);
351  EVT EltVT = N->getValueType(0).getVectorElementType();
352  if (Op.getValueType() != EltVT)
353  // FIXME: Can this happen for floating point types?
354  Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, Op);
355  return Op;
356 }
357 
358 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
359  assert(N->isUnindexed() && "Indexed vector load?");
360 
361  SDValue Result = DAG.getLoad(
364  N->getBasePtr(), DAG.getUNDEF(N->getBasePtr().getValueType()),
367  N->getAAInfo());
368 
369  // Legalize the chain result - switch anything that used the old chain to
370  // use the new one.
371  ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
372  return Result;
373 }
374 
375 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
376  // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
377  EVT DestVT = N->getValueType(0).getVectorElementType();
378  SDValue Op = N->getOperand(0);
379  EVT OpVT = Op.getValueType();
380  SDLoc DL(N);
381  // The result needs scalarizing, but it's not a given that the source does.
382  // This is a workaround for targets where it's impossible to scalarize the
383  // result of a conversion, because the source type is legal.
384  // For instance, this happens on AArch64: v1i1 is illegal but v1i{8,16,32}
385  // are widened to v8i8, v4i16, and v2i32, which is legal, because v1i64 is
386  // legal and was not scalarized.
387  // See the similar logic in ScalarizeVecRes_SETCC
388  if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
389  Op = GetScalarizedVector(Op);
390  } else {
391  EVT VT = OpVT.getVectorElementType();
392  Op = DAG.getNode(
393  ISD::EXTRACT_VECTOR_ELT, DL, VT, Op,
394  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
395  }
396  return DAG.getNode(N->getOpcode(), SDLoc(N), DestVT, Op);
397 }
398 
399 SDValue DAGTypeLegalizer::ScalarizeVecRes_InregOp(SDNode *N) {
400  EVT EltVT = N->getValueType(0).getVectorElementType();
401  EVT ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT().getVectorElementType();
402  SDValue LHS = GetScalarizedVector(N->getOperand(0));
403  return DAG.getNode(N->getOpcode(), SDLoc(N), EltVT,
404  LHS, DAG.getValueType(ExtVT));
405 }
406 
407 SDValue DAGTypeLegalizer::ScalarizeVecRes_VecInregOp(SDNode *N) {
408  SDLoc DL(N);
409  SDValue Op = N->getOperand(0);
410 
411  EVT OpVT = Op.getValueType();
412  EVT OpEltVT = OpVT.getVectorElementType();
413  EVT EltVT = N->getValueType(0).getVectorElementType();
414 
415  if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
416  Op = GetScalarizedVector(Op);
417  } else {
418  Op = DAG.getNode(
419  ISD::EXTRACT_VECTOR_ELT, DL, OpEltVT, Op,
420  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
421  }
422 
423  switch (N->getOpcode()) {
425  return DAG.getNode(ISD::ANY_EXTEND, DL, EltVT, Op);
427  return DAG.getNode(ISD::SIGN_EXTEND, DL, EltVT, Op);
429  return DAG.getNode(ISD::ZERO_EXTEND, DL, EltVT, Op);
430  }
431 
432  llvm_unreachable("Illegal extend_vector_inreg opcode");
433 }
434 
435 SDValue DAGTypeLegalizer::ScalarizeVecRes_SCALAR_TO_VECTOR(SDNode *N) {
436  // If the operand is wider than the vector element type then it is implicitly
437  // truncated. Make that explicit here.
438  EVT EltVT = N->getValueType(0).getVectorElementType();
439  SDValue InOp = N->getOperand(0);
440  if (InOp.getValueType() != EltVT)
441  return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
442  return InOp;
443 }
444 
445 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSELECT(SDNode *N) {
446  SDValue Cond = N->getOperand(0);
447  EVT OpVT = Cond.getValueType();
448  SDLoc DL(N);
449  // The vselect result and true/value operands needs scalarizing, but it's
450  // not a given that the Cond does. For instance, in AVX512 v1i1 is legal.
451  // See the similar logic in ScalarizeVecRes_SETCC
452  if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
453  Cond = GetScalarizedVector(Cond);
454  } else {
455  EVT VT = OpVT.getVectorElementType();
456  Cond = DAG.getNode(
457  ISD::EXTRACT_VECTOR_ELT, DL, VT, Cond,
458  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
459  }
460 
461  SDValue LHS = GetScalarizedVector(N->getOperand(1));
462  TargetLowering::BooleanContent ScalarBool =
463  TLI.getBooleanContents(false, false);
464  TargetLowering::BooleanContent VecBool = TLI.getBooleanContents(true, false);
465 
466  // If integer and float booleans have different contents then we can't
467  // reliably optimize in all cases. There is a full explanation for this in
468  // DAGCombiner::visitSELECT() where the same issue affects folding
469  // (select C, 0, 1) to (xor C, 1).
470  if (TLI.getBooleanContents(false, false) !=
471  TLI.getBooleanContents(false, true)) {
472  // At least try the common case where the boolean is generated by a
473  // comparison.
474  if (Cond->getOpcode() == ISD::SETCC) {
475  EVT OpVT = Cond->getOperand(0).getValueType();
476  ScalarBool = TLI.getBooleanContents(OpVT.getScalarType());
477  VecBool = TLI.getBooleanContents(OpVT);
478  } else
480  }
481 
482  EVT CondVT = Cond.getValueType();
483  if (ScalarBool != VecBool) {
484  switch (ScalarBool) {
486  break;
490  // Vector read from all ones, scalar expects a single 1 so mask.
491  Cond = DAG.getNode(ISD::AND, SDLoc(N), CondVT,
492  Cond, DAG.getConstant(1, SDLoc(N), CondVT));
493  break;
497  // Vector reads from a one, scalar from all ones so sign extend.
498  Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), CondVT,
499  Cond, DAG.getValueType(MVT::i1));
500  break;
501  }
502  }
503 
504  // Truncate the condition if needed
505  auto BoolVT = getSetCCResultType(CondVT);
506  if (BoolVT.bitsLT(CondVT))
507  Cond = DAG.getNode(ISD::TRUNCATE, SDLoc(N), BoolVT, Cond);
508 
509  return DAG.getSelect(SDLoc(N),
510  LHS.getValueType(), Cond, LHS,
511  GetScalarizedVector(N->getOperand(2)));
512 }
513 
514 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
515  SDValue LHS = GetScalarizedVector(N->getOperand(1));
516  return DAG.getSelect(SDLoc(N),
517  LHS.getValueType(), N->getOperand(0), LHS,
518  GetScalarizedVector(N->getOperand(2)));
519 }
520 
521 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
522  SDValue LHS = GetScalarizedVector(N->getOperand(2));
523  return DAG.getNode(ISD::SELECT_CC, SDLoc(N), LHS.getValueType(),
524  N->getOperand(0), N->getOperand(1),
525  LHS, GetScalarizedVector(N->getOperand(3)),
526  N->getOperand(4));
527 }
528 
529 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
530  return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
531 }
532 
533 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
534  // Figure out if the scalar is the LHS or RHS and return it.
535  SDValue Arg = N->getOperand(2).getOperand(0);
536  if (Arg.isUndef())
537  return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
538  unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
539  return GetScalarizedVector(N->getOperand(Op));
540 }
541 
542 SDValue DAGTypeLegalizer::ScalarizeVecRes_SETCC(SDNode *N) {
543  assert(N->getValueType(0).isVector() &&
544  N->getOperand(0).getValueType().isVector() &&
545  "Operand types must be vectors");
546  SDValue LHS = N->getOperand(0);
547  SDValue RHS = N->getOperand(1);
548  EVT OpVT = LHS.getValueType();
549  EVT NVT = N->getValueType(0).getVectorElementType();
550  SDLoc DL(N);
551 
552  // The result needs scalarizing, but it's not a given that the source does.
553  if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
554  LHS = GetScalarizedVector(LHS);
555  RHS = GetScalarizedVector(RHS);
556  } else {
557  EVT VT = OpVT.getVectorElementType();
558  LHS = DAG.getNode(
559  ISD::EXTRACT_VECTOR_ELT, DL, VT, LHS,
560  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
561  RHS = DAG.getNode(
562  ISD::EXTRACT_VECTOR_ELT, DL, VT, RHS,
563  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
564  }
565 
566  // Turn it into a scalar SETCC.
567  SDValue Res = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS,
568  N->getOperand(2));
569  // Vectors may have a different boolean contents to scalars. Promote the
570  // value appropriately.
571  ISD::NodeType ExtendCode =
573  return DAG.getNode(ExtendCode, DL, NVT, Res);
574 }
575 
576 
577 //===----------------------------------------------------------------------===//
578 // Operand Vector Scalarization <1 x ty> -> ty.
579 //===----------------------------------------------------------------------===//
580 
581 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
582  LLVM_DEBUG(dbgs() << "Scalarize node operand " << OpNo << ": "; N->dump(&DAG);
583  dbgs() << "\n");
584  SDValue Res = SDValue();
585 
586  if (!Res.getNode()) {
587  switch (N->getOpcode()) {
588  default:
589 #ifndef NDEBUG
590  dbgs() << "ScalarizeVectorOperand Op #" << OpNo << ": ";
591  N->dump(&DAG);
592  dbgs() << "\n";
593 #endif
594  report_fatal_error("Do not know how to scalarize this operator's "
595  "operand!\n");
596  case ISD::BITCAST:
597  Res = ScalarizeVecOp_BITCAST(N);
598  break;
599  case ISD::ANY_EXTEND:
600  case ISD::ZERO_EXTEND:
601  case ISD::SIGN_EXTEND:
602  case ISD::TRUNCATE:
603  case ISD::FP_TO_SINT:
604  case ISD::FP_TO_UINT:
605  case ISD::SINT_TO_FP:
606  case ISD::UINT_TO_FP:
607  Res = ScalarizeVecOp_UnaryOp(N);
608  break;
611  Res = ScalarizeVecOp_UnaryOp_StrictFP(N);
612  break;
613  case ISD::CONCAT_VECTORS:
614  Res = ScalarizeVecOp_CONCAT_VECTORS(N);
615  break;
617  Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N);
618  break;
619  case ISD::VSELECT:
620  Res = ScalarizeVecOp_VSELECT(N);
621  break;
622  case ISD::SETCC:
623  Res = ScalarizeVecOp_VSETCC(N);
624  break;
625  case ISD::STORE:
626  Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo);
627  break;
629  Res = ScalarizeVecOp_STRICT_FP_ROUND(N, OpNo);
630  break;
631  case ISD::FP_ROUND:
632  Res = ScalarizeVecOp_FP_ROUND(N, OpNo);
633  break;
634  case ISD::VECREDUCE_FADD:
635  case ISD::VECREDUCE_FMUL:
636  case ISD::VECREDUCE_ADD:
637  case ISD::VECREDUCE_MUL:
638  case ISD::VECREDUCE_AND:
639  case ISD::VECREDUCE_OR:
640  case ISD::VECREDUCE_XOR:
641  case ISD::VECREDUCE_SMAX:
642  case ISD::VECREDUCE_SMIN:
643  case ISD::VECREDUCE_UMAX:
644  case ISD::VECREDUCE_UMIN:
645  case ISD::VECREDUCE_FMAX:
646  case ISD::VECREDUCE_FMIN:
647  Res = ScalarizeVecOp_VECREDUCE(N);
648  break;
649  }
650  }
651 
652  // If the result is null, the sub-method took care of registering results etc.
653  if (!Res.getNode()) return false;
654 
655  // If the result is N, the sub-method updated N in place. Tell the legalizer
656  // core about this.
657  if (Res.getNode() == N)
658  return true;
659 
660  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
661  "Invalid operand expansion");
662 
663  ReplaceValueWith(SDValue(N, 0), Res);
664  return false;
665 }
666 
667 /// If the value to convert is a vector that needs to be scalarized, it must be
668 /// <1 x ty>. Convert the element instead.
669 SDValue DAGTypeLegalizer::ScalarizeVecOp_BITCAST(SDNode *N) {
670  SDValue Elt = GetScalarizedVector(N->getOperand(0));
671  return DAG.getNode(ISD::BITCAST, SDLoc(N),
672  N->getValueType(0), Elt);
673 }
674 
675 /// If the input is a vector that needs to be scalarized, it must be <1 x ty>.
676 /// Do the operation on the element instead.
677 SDValue DAGTypeLegalizer::ScalarizeVecOp_UnaryOp(SDNode *N) {
679  "Unexpected vector type!");
680  SDValue Elt = GetScalarizedVector(N->getOperand(0));
681  SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N),
682  N->getValueType(0).getScalarType(), Elt);
683  // Revectorize the result so the types line up with what the uses of this
684  // expression expect.
685  return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Op);
686 }
687 
688 /// If the input is a vector that needs to be scalarized, it must be <1 x ty>.
689 /// Do the strict FP operation on the element instead.
690 SDValue DAGTypeLegalizer::ScalarizeVecOp_UnaryOp_StrictFP(SDNode *N) {
692  "Unexpected vector type!");
693  SDValue Elt = GetScalarizedVector(N->getOperand(1));
694  SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N),
696  { N->getOperand(0), Elt });
697  // Legalize the chain result - switch anything that used the old chain to
698  // use the new one.
699  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
700  // Revectorize the result so the types line up with what the uses of this
701  // expression expect.
702  return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
703 }
704 
705 /// The vectors to concatenate have length one - use a BUILD_VECTOR instead.
706 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
708  for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
709  Ops[i] = GetScalarizedVector(N->getOperand(i));
710  return DAG.getBuildVector(N->getValueType(0), SDLoc(N), Ops);
711 }
712 
713 /// If the input is a vector that needs to be scalarized, it must be <1 x ty>,
714 /// so just return the element, ignoring the index.
715 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
716  EVT VT = N->getValueType(0);
717  SDValue Res = GetScalarizedVector(N->getOperand(0));
718  if (Res.getValueType() != VT)
719  Res = VT.isFloatingPoint()
720  ? DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Res)
721  : DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Res);
722  return Res;
723 }
724 
725 /// If the input condition is a vector that needs to be scalarized, it must be
726 /// <1 x i1>, so just convert to a normal ISD::SELECT
727 /// (still with vector output type since that was acceptable if we got here).
728 SDValue DAGTypeLegalizer::ScalarizeVecOp_VSELECT(SDNode *N) {
729  SDValue ScalarCond = GetScalarizedVector(N->getOperand(0));
730  EVT VT = N->getValueType(0);
731 
732  return DAG.getNode(ISD::SELECT, SDLoc(N), VT, ScalarCond, N->getOperand(1),
733  N->getOperand(2));
734 }
735 
736 /// If the operand is a vector that needs to be scalarized then the
737 /// result must be v1i1, so just convert to a scalar SETCC and wrap
738 /// with a scalar_to_vector since the res type is legal if we got here
739 SDValue DAGTypeLegalizer::ScalarizeVecOp_VSETCC(SDNode *N) {
740  assert(N->getValueType(0).isVector() &&
741  N->getOperand(0).getValueType().isVector() &&
742  "Operand types must be vectors");
743  assert(N->getValueType(0) == MVT::v1i1 && "Expected v1i1 type");
744 
745  EVT VT = N->getValueType(0);
746  SDValue LHS = GetScalarizedVector(N->getOperand(0));
747  SDValue RHS = GetScalarizedVector(N->getOperand(1));
748 
749  EVT OpVT = N->getOperand(0).getValueType();
750  EVT NVT = VT.getVectorElementType();
751  SDLoc DL(N);
752  // Turn it into a scalar SETCC.
753  SDValue Res = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS,
754  N->getOperand(2));
755 
756  // Vectors may have a different boolean contents to scalars. Promote the
757  // value appropriately.
758  ISD::NodeType ExtendCode =
760 
761  Res = DAG.getNode(ExtendCode, DL, NVT, Res);
762 
763  return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Res);
764 }
765 
766 /// If the value to store is a vector that needs to be scalarized, it must be
767 /// <1 x ty>. Just store the element.
768 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
769  assert(N->isUnindexed() && "Indexed store of one-element vector?");
770  assert(OpNo == 1 && "Do not know how to scalarize this operand!");
771  SDLoc dl(N);
772 
773  if (N->isTruncatingStore())
774  return DAG.getTruncStore(
775  N->getChain(), dl, GetScalarizedVector(N->getOperand(1)),
776  N->getBasePtr(), N->getPointerInfo(),
778  N->getMemOperand()->getFlags(), N->getAAInfo());
779 
780  return DAG.getStore(N->getChain(), dl, GetScalarizedVector(N->getOperand(1)),
781  N->getBasePtr(), N->getPointerInfo(),
783  N->getAAInfo());
784 }
785 
786 /// If the value to round is a vector that needs to be scalarized, it must be
787 /// <1 x ty>. Convert the element instead.
788 SDValue DAGTypeLegalizer::ScalarizeVecOp_FP_ROUND(SDNode *N, unsigned OpNo) {
789  SDValue Elt = GetScalarizedVector(N->getOperand(0));
790  SDValue Res = DAG.getNode(ISD::FP_ROUND, SDLoc(N),
791  N->getValueType(0).getVectorElementType(), Elt,
792  N->getOperand(1));
793  return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
794 }
795 
796 SDValue DAGTypeLegalizer::ScalarizeVecOp_STRICT_FP_ROUND(SDNode *N,
797  unsigned OpNo) {
798  assert(OpNo == 1 && "Wrong operand for scalarization!");
799  SDValue Elt = GetScalarizedVector(N->getOperand(1));
802  MVT::Other },
803  { N->getOperand(0), Elt, N->getOperand(2) });
804  // Legalize the chain result - switch anything that used the old chain to
805  // use the new one.
806  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
807  return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
808 }
809 
810 SDValue DAGTypeLegalizer::ScalarizeVecOp_VECREDUCE(SDNode *N) {
811  SDValue Res = GetScalarizedVector(N->getOperand(0));
812  // Result type may be wider than element type.
813  if (Res.getValueType() != N->getValueType(0))
814  Res = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Res);
815  return Res;
816 }
817 
818 //===----------------------------------------------------------------------===//
819 // Result Vector Splitting
820 //===----------------------------------------------------------------------===//
821 
822 /// This method is called when the specified result of the specified node is
823 /// found to need vector splitting. At this point, the node may also have
824 /// invalid operands or may have other results that need legalization, we just
825 /// know that (at least) one result needs vector splitting.
826 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
827  LLVM_DEBUG(dbgs() << "Split node result: "; N->dump(&DAG); dbgs() << "\n");
828  SDValue Lo, Hi;
829 
830  // See if the target wants to custom expand this node.
831  if (CustomLowerNode(N, N->getValueType(ResNo), true))
832  return;
833 
834  switch (N->getOpcode()) {
835  default:
836 #ifndef NDEBUG
837  dbgs() << "SplitVectorResult #" << ResNo << ": ";
838  N->dump(&DAG);
839  dbgs() << "\n";
840 #endif
841  report_fatal_error("Do not know how to split the result of this "
842  "operator!\n");
843 
844  case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
845  case ISD::VSELECT:
846  case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
847  case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
848  case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
849  case ISD::BITCAST: SplitVecRes_BITCAST(N, Lo, Hi); break;
850  case ISD::BUILD_VECTOR: SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
851  case ISD::CONCAT_VECTORS: SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
852  case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
853  case ISD::INSERT_SUBVECTOR: SplitVecRes_INSERT_SUBVECTOR(N, Lo, Hi); break;
854  case ISD::FPOWI: SplitVecRes_FPOWI(N, Lo, Hi); break;
855  case ISD::FCOPYSIGN: SplitVecRes_FCOPYSIGN(N, Lo, Hi); break;
856  case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
857  case ISD::SCALAR_TO_VECTOR: SplitVecRes_SCALAR_TO_VECTOR(N, Lo, Hi); break;
858  case ISD::SIGN_EXTEND_INREG: SplitVecRes_InregOp(N, Lo, Hi); break;
859  case ISD::LOAD:
860  SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);
861  break;
862  case ISD::MLOAD:
863  SplitVecRes_MLOAD(cast<MaskedLoadSDNode>(N), Lo, Hi);
864  break;
865  case ISD::MGATHER:
866  SplitVecRes_MGATHER(cast<MaskedGatherSDNode>(N), Lo, Hi);
867  break;
868  case ISD::SETCC:
869  SplitVecRes_SETCC(N, Lo, Hi);
870  break;
871  case ISD::VECTOR_SHUFFLE:
872  SplitVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N), Lo, Hi);
873  break;
874  case ISD::VAARG:
875  SplitVecRes_VAARG(N, Lo, Hi);
876  break;
877 
881  SplitVecRes_ExtVecInRegOp(N, Lo, Hi);
882  break;
883 
884  case ISD::ABS:
885  case ISD::BITREVERSE:
886  case ISD::BSWAP:
887  case ISD::CTLZ:
888  case ISD::CTTZ:
891  case ISD::CTPOP:
892  case ISD::FABS:
893  case ISD::FCEIL:
894  case ISD::FCOS:
895  case ISD::FEXP:
896  case ISD::FEXP2:
897  case ISD::FFLOOR:
898  case ISD::FLOG:
899  case ISD::FLOG10:
900  case ISD::FLOG2:
901  case ISD::FNEARBYINT:
902  case ISD::FNEG:
903  case ISD::FP_EXTEND:
905  case ISD::FP_ROUND:
907  case ISD::FP_TO_SINT:
909  case ISD::FP_TO_UINT:
911  case ISD::FRINT:
912  case ISD::FROUND:
913  case ISD::FSIN:
914  case ISD::FSQRT:
915  case ISD::FTRUNC:
916  case ISD::SINT_TO_FP:
917  case ISD::TRUNCATE:
918  case ISD::UINT_TO_FP:
919  case ISD::FCANONICALIZE:
920  SplitVecRes_UnaryOp(N, Lo, Hi);
921  break;
922 
923  case ISD::ANY_EXTEND:
924  case ISD::SIGN_EXTEND:
925  case ISD::ZERO_EXTEND:
926  SplitVecRes_ExtendOp(N, Lo, Hi);
927  break;
928 
929  case ISD::ADD:
930  case ISD::SUB:
931  case ISD::MUL:
932  case ISD::MULHS:
933  case ISD::MULHU:
934  case ISD::FADD:
935  case ISD::FSUB:
936  case ISD::FMUL:
937  case ISD::FMINNUM:
938  case ISD::FMAXNUM:
939  case ISD::FMINIMUM:
940  case ISD::FMAXIMUM:
941  case ISD::SDIV:
942  case ISD::UDIV:
943  case ISD::FDIV:
944  case ISD::FPOW:
945  case ISD::AND:
946  case ISD::OR:
947  case ISD::XOR:
948  case ISD::SHL:
949  case ISD::SRA:
950  case ISD::SRL:
951  case ISD::UREM:
952  case ISD::SREM:
953  case ISD::FREM:
954  case ISD::SMIN:
955  case ISD::SMAX:
956  case ISD::UMIN:
957  case ISD::UMAX:
958  case ISD::SADDSAT:
959  case ISD::UADDSAT:
960  case ISD::SSUBSAT:
961  case ISD::USUBSAT:
962  SplitVecRes_BinOp(N, Lo, Hi);
963  break;
964  case ISD::FMA:
965  SplitVecRes_TernaryOp(N, Lo, Hi);
966  break;
967  case ISD::STRICT_FADD:
968  case ISD::STRICT_FSUB:
969  case ISD::STRICT_FMUL:
970  case ISD::STRICT_FDIV:
971  case ISD::STRICT_FREM:
972  case ISD::STRICT_FSQRT:
973  case ISD::STRICT_FMA:
974  case ISD::STRICT_FPOW:
975  case ISD::STRICT_FPOWI:
976  case ISD::STRICT_FSIN:
977  case ISD::STRICT_FCOS:
978  case ISD::STRICT_FEXP:
979  case ISD::STRICT_FEXP2:
980  case ISD::STRICT_FLOG:
981  case ISD::STRICT_FLOG10:
982  case ISD::STRICT_FLOG2:
983  case ISD::STRICT_FRINT:
985  case ISD::STRICT_FMAXNUM:
986  case ISD::STRICT_FMINNUM:
987  case ISD::STRICT_FCEIL:
988  case ISD::STRICT_FFLOOR:
989  case ISD::STRICT_FROUND:
990  case ISD::STRICT_FTRUNC:
991  SplitVecRes_StrictFPOp(N, Lo, Hi);
992  break;
993  case ISD::UADDO:
994  case ISD::SADDO:
995  case ISD::USUBO:
996  case ISD::SSUBO:
997  case ISD::UMULO:
998  case ISD::SMULO:
999  SplitVecRes_OverflowOp(N, ResNo, Lo, Hi);
1000  break;
1001  case ISD::SMULFIX:
1002  case ISD::SMULFIXSAT:
1003  case ISD::UMULFIX:
1004  case ISD::UMULFIXSAT:
1005  SplitVecRes_MULFIX(N, Lo, Hi);
1006  break;
1007  }
1008 
1009  // If Lo/Hi is null, the sub-method took care of registering results etc.
1010  if (Lo.getNode())
1011  SetSplitVector(SDValue(N, ResNo), Lo, Hi);
1012 }
1013 
1014 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
1015  SDValue &Hi) {
1016  SDValue LHSLo, LHSHi;
1017  GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
1018  SDValue RHSLo, RHSHi;
1019  GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
1020  SDLoc dl(N);
1021 
1022  const SDNodeFlags Flags = N->getFlags();
1023  unsigned Opcode = N->getOpcode();
1024  Lo = DAG.getNode(Opcode, dl, LHSLo.getValueType(), LHSLo, RHSLo, Flags);
1025  Hi = DAG.getNode(Opcode, dl, LHSHi.getValueType(), LHSHi, RHSHi, Flags);
1026 }
1027 
1028 void DAGTypeLegalizer::SplitVecRes_TernaryOp(SDNode *N, SDValue &Lo,
1029  SDValue &Hi) {
1030  SDValue Op0Lo, Op0Hi;
1031  GetSplitVector(N->getOperand(0), Op0Lo, Op0Hi);
1032  SDValue Op1Lo, Op1Hi;
1033  GetSplitVector(N->getOperand(1), Op1Lo, Op1Hi);
1034  SDValue Op2Lo, Op2Hi;
1035  GetSplitVector(N->getOperand(2), Op2Lo, Op2Hi);
1036  SDLoc dl(N);
1037 
1038  Lo = DAG.getNode(N->getOpcode(), dl, Op0Lo.getValueType(),
1039  Op0Lo, Op1Lo, Op2Lo);
1040  Hi = DAG.getNode(N->getOpcode(), dl, Op0Hi.getValueType(),
1041  Op0Hi, Op1Hi, Op2Hi);
1042 }
1043 
1044 void DAGTypeLegalizer::SplitVecRes_MULFIX(SDNode *N, SDValue &Lo, SDValue &Hi) {
1045  SDValue LHSLo, LHSHi;
1046  GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
1047  SDValue RHSLo, RHSHi;
1048  GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
1049  SDLoc dl(N);
1050  SDValue Op2 = N->getOperand(2);
1051 
1052  unsigned Opcode = N->getOpcode();
1053  Lo = DAG.getNode(Opcode, dl, LHSLo.getValueType(), LHSLo, RHSLo, Op2);
1054  Hi = DAG.getNode(Opcode, dl, LHSHi.getValueType(), LHSHi, RHSHi, Op2);
1055 }
1056 
1057 void DAGTypeLegalizer::SplitVecRes_BITCAST(SDNode *N, SDValue &Lo,
1058  SDValue &Hi) {
1059  // We know the result is a vector. The input may be either a vector or a
1060  // scalar value.
1061  EVT LoVT, HiVT;
1062  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1063  SDLoc dl(N);
1064 
1065  SDValue InOp = N->getOperand(0);
1066  EVT InVT = InOp.getValueType();
1067 
1068  // Handle some special cases efficiently.
1069  switch (getTypeAction(InVT)) {
1076  break;
1079  // A scalar to vector conversion, where the scalar needs expansion.
1080  // If the vector is being split in two then we can just convert the
1081  // expanded pieces.
1082  if (LoVT == HiVT) {
1083  GetExpandedOp(InOp, Lo, Hi);
1084  if (DAG.getDataLayout().isBigEndian())
1085  std::swap(Lo, Hi);
1086  Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
1087  Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
1088  return;
1089  }
1090  break;
1092  // If the input is a vector that needs to be split, convert each split
1093  // piece of the input now.
1094  GetSplitVector(InOp, Lo, Hi);
1095  Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
1096  Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
1097  return;
1098  }
1099 
1100  // In the general case, convert the input to an integer and split it by hand.
1101  EVT LoIntVT = EVT::getIntegerVT(*DAG.getContext(), LoVT.getSizeInBits());
1102  EVT HiIntVT = EVT::getIntegerVT(*DAG.getContext(), HiVT.getSizeInBits());
1103  if (DAG.getDataLayout().isBigEndian())
1104  std::swap(LoIntVT, HiIntVT);
1105 
1106  SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
1107 
1108  if (DAG.getDataLayout().isBigEndian())
1109  std::swap(Lo, Hi);
1110  Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
1111  Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
1112 }
1113 
1114 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
1115  SDValue &Hi) {
1116  EVT LoVT, HiVT;
1117  SDLoc dl(N);
1118  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1119  unsigned LoNumElts = LoVT.getVectorNumElements();
1120  SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
1121  Lo = DAG.getBuildVector(LoVT, dl, LoOps);
1122 
1123  SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
1124  Hi = DAG.getBuildVector(HiVT, dl, HiOps);
1125 }
1126 
1127 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
1128  SDValue &Hi) {
1129  assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
1130  SDLoc dl(N);
1131  unsigned NumSubvectors = N->getNumOperands() / 2;
1132  if (NumSubvectors == 1) {
1133  Lo = N->getOperand(0);
1134  Hi = N->getOperand(1);
1135  return;
1136  }
1137 
1138  EVT LoVT, HiVT;
1139  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1140 
1141  SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
1142  Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, LoVT, LoOps);
1143 
1144  SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
1145  Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HiVT, HiOps);
1146 }
1147 
1148 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
1149  SDValue &Hi) {
1150  SDValue Vec = N->getOperand(0);
1151  SDValue Idx = N->getOperand(1);
1152  SDLoc dl(N);
1153 
1154  EVT LoVT, HiVT;
1155  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1156 
1157  Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, LoVT, Vec, Idx);
1158  uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1159  Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, HiVT, Vec,
1160  DAG.getConstant(IdxVal + LoVT.getVectorNumElements(), dl,
1161  TLI.getVectorIdxTy(DAG.getDataLayout())));
1162 }
1163 
1164 void DAGTypeLegalizer::SplitVecRes_INSERT_SUBVECTOR(SDNode *N, SDValue &Lo,
1165  SDValue &Hi) {
1166  SDValue Vec = N->getOperand(0);
1167  SDValue SubVec = N->getOperand(1);
1168  SDValue Idx = N->getOperand(2);
1169  SDLoc dl(N);
1170  GetSplitVector(Vec, Lo, Hi);
1171 
1172  EVT VecVT = Vec.getValueType();
1173  unsigned VecElems = VecVT.getVectorNumElements();
1174  unsigned SubElems = SubVec.getValueType().getVectorNumElements();
1175 
1176  // If we know the index is 0, and we know the subvector doesn't cross the
1177  // boundary between the halves, we can avoid spilling the vector, and insert
1178  // into the lower half of the split vector directly.
1179  // TODO: The IdxVal == 0 constraint is artificial, we could do this whenever
1180  // the index is constant and there is no boundary crossing. But those cases
1181  // don't seem to get hit in practice.
1182  if (ConstantSDNode *ConstIdx = dyn_cast<ConstantSDNode>(Idx)) {
1183  unsigned IdxVal = ConstIdx->getZExtValue();
1184  if ((IdxVal == 0) && (IdxVal + SubElems <= VecElems / 2)) {
1185  EVT LoVT, HiVT;
1186  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1187  Lo = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, LoVT, Lo, SubVec, Idx);
1188  return;
1189  }
1190  }
1191 
1192  // Spill the vector to the stack.
1193  SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
1194  SDValue Store =
1195  DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, MachinePointerInfo());
1196 
1197  // Store the new subvector into the specified index.
1198  SDValue SubVecPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1199  Type *VecType = VecVT.getTypeForEVT(*DAG.getContext());
1200  unsigned Alignment = DAG.getDataLayout().getPrefTypeAlignment(VecType);
1201  Store = DAG.getStore(Store, dl, SubVec, SubVecPtr, MachinePointerInfo());
1202 
1203  // Load the Lo part from the stack slot.
1204  Lo =
1205  DAG.getLoad(Lo.getValueType(), dl, Store, StackPtr, MachinePointerInfo());
1206 
1207  // Increment the pointer to the other part.
1208  unsigned IncrementSize = Lo.getValueSizeInBits() / 8;
1209  StackPtr =
1210  DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
1211  DAG.getConstant(IncrementSize, dl, StackPtr.getValueType()));
1212 
1213  // Load the Hi part from the stack slot.
1214  Hi = DAG.getLoad(Hi.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
1215  MinAlign(Alignment, IncrementSize));
1216 }
1217 
1218 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
1219  SDValue &Hi) {
1220  SDLoc dl(N);
1221  GetSplitVector(N->getOperand(0), Lo, Hi);
1222  Lo = DAG.getNode(ISD::FPOWI, dl, Lo.getValueType(), Lo, N->getOperand(1));
1223  Hi = DAG.getNode(ISD::FPOWI, dl, Hi.getValueType(), Hi, N->getOperand(1));
1224 }
1225 
1226 void DAGTypeLegalizer::SplitVecRes_FCOPYSIGN(SDNode *N, SDValue &Lo,
1227  SDValue &Hi) {
1228  SDValue LHSLo, LHSHi;
1229  GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
1230  SDLoc DL(N);
1231 
1232  SDValue RHSLo, RHSHi;
1233  SDValue RHS = N->getOperand(1);
1234  EVT RHSVT = RHS.getValueType();
1235  if (getTypeAction(RHSVT) == TargetLowering::TypeSplitVector)
1236  GetSplitVector(RHS, RHSLo, RHSHi);
1237  else
1238  std::tie(RHSLo, RHSHi) = DAG.SplitVector(RHS, SDLoc(RHS));
1239 
1240 
1241  Lo = DAG.getNode(ISD::FCOPYSIGN, DL, LHSLo.getValueType(), LHSLo, RHSLo);
1242  Hi = DAG.getNode(ISD::FCOPYSIGN, DL, LHSHi.getValueType(), LHSHi, RHSHi);
1243 }
1244 
1245 void DAGTypeLegalizer::SplitVecRes_InregOp(SDNode *N, SDValue &Lo,
1246  SDValue &Hi) {
1247  SDValue LHSLo, LHSHi;
1248  GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
1249  SDLoc dl(N);
1250 
1251  EVT LoVT, HiVT;
1252  std::tie(LoVT, HiVT) =
1253  DAG.GetSplitDestVTs(cast<VTSDNode>(N->getOperand(1))->getVT());
1254 
1255  Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo,
1256  DAG.getValueType(LoVT));
1257  Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi,
1258  DAG.getValueType(HiVT));
1259 }
1260 
1261 void DAGTypeLegalizer::SplitVecRes_ExtVecInRegOp(SDNode *N, SDValue &Lo,
1262  SDValue &Hi) {
1263  unsigned Opcode = N->getOpcode();
1264  SDValue N0 = N->getOperand(0);
1265 
1266  SDLoc dl(N);
1267  SDValue InLo, InHi;
1268 
1269  if (getTypeAction(N0.getValueType()) == TargetLowering::TypeSplitVector)
1270  GetSplitVector(N0, InLo, InHi);
1271  else
1272  std::tie(InLo, InHi) = DAG.SplitVectorOperand(N, 0);
1273 
1274  EVT InLoVT = InLo.getValueType();
1275  unsigned InNumElements = InLoVT.getVectorNumElements();
1276 
1277  EVT OutLoVT, OutHiVT;
1278  std::tie(OutLoVT, OutHiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1279  unsigned OutNumElements = OutLoVT.getVectorNumElements();
1280  assert((2 * OutNumElements) <= InNumElements &&
1281  "Illegal extend vector in reg split");
1282 
1283  // *_EXTEND_VECTOR_INREG instructions extend the lowest elements of the
1284  // input vector (i.e. we only use InLo):
1285  // OutLo will extend the first OutNumElements from InLo.
1286  // OutHi will extend the next OutNumElements from InLo.
1287 
1288  // Shuffle the elements from InLo for OutHi into the bottom elements to
1289  // create a 'fake' InHi.
1290  SmallVector<int, 8> SplitHi(InNumElements, -1);
1291  for (unsigned i = 0; i != OutNumElements; ++i)
1292  SplitHi[i] = i + OutNumElements;
1293  InHi = DAG.getVectorShuffle(InLoVT, dl, InLo, DAG.getUNDEF(InLoVT), SplitHi);
1294 
1295  Lo = DAG.getNode(Opcode, dl, OutLoVT, InLo);
1296  Hi = DAG.getNode(Opcode, dl, OutHiVT, InHi);
1297 }
1298 
1299 void DAGTypeLegalizer::SplitVecRes_StrictFPOp(SDNode *N, SDValue &Lo,
1300  SDValue &Hi) {
1301  unsigned NumOps = N->getNumOperands();
1302  SDValue Chain = N->getOperand(0);
1303  EVT LoVT, HiVT;
1304  SDLoc dl(N);
1305  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1306 
1309 
1310  // The Chain is the first operand.
1311  OpsLo.push_back(Chain);
1312  OpsHi.push_back(Chain);
1313 
1314  // Now process the remaining operands.
1315  for (unsigned i = 1; i < NumOps; ++i) {
1316  SDValue Op = N->getOperand(i);
1317  SDValue OpLo = Op;
1318  SDValue OpHi = Op;
1319 
1320  EVT InVT = Op.getValueType();
1321  if (InVT.isVector()) {
1322  // If the input also splits, handle it directly for a
1323  // compile time speedup. Otherwise split it by hand.
1324  if (getTypeAction(InVT) == TargetLowering::TypeSplitVector)
1325  GetSplitVector(Op, OpLo, OpHi);
1326  else
1327  std::tie(OpLo, OpHi) = DAG.SplitVectorOperand(N, i);
1328  }
1329 
1330  OpsLo.push_back(OpLo);
1331  OpsHi.push_back(OpHi);
1332  }
1333 
1334  EVT LoValueVTs[] = {LoVT, MVT::Other};
1335  EVT HiValueVTs[] = {HiVT, MVT::Other};
1336  Lo = DAG.getNode(N->getOpcode(), dl, LoValueVTs, OpsLo);
1337  Hi = DAG.getNode(N->getOpcode(), dl, HiValueVTs, OpsHi);
1338 
1339  // Build a factor node to remember that this Op is independent of the
1340  // other one.
1341  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1342  Lo.getValue(1), Hi.getValue(1));
1343 
1344  // Legalize the chain result - switch anything that used the old chain to
1345  // use the new one.
1346  ReplaceValueWith(SDValue(N, 1), Chain);
1347 }
1348 
1349 SDValue DAGTypeLegalizer::UnrollVectorOp_StrictFP(SDNode *N, unsigned ResNE) {
1350  SDValue Chain = N->getOperand(0);
1351  EVT VT = N->getValueType(0);
1352  unsigned NE = VT.getVectorNumElements();
1353  EVT EltVT = VT.getVectorElementType();
1354  SDLoc dl(N);
1355 
1356  SmallVector<SDValue, 8> Scalars;
1358 
1359  // If ResNE is 0, fully unroll the vector op.
1360  if (ResNE == 0)
1361  ResNE = NE;
1362  else if (NE > ResNE)
1363  NE = ResNE;
1364 
1365  //The results of each unrolled operation, including the chain.
1366  EVT ChainVTs[] = {EltVT, MVT::Other};
1367  SmallVector<SDValue, 8> Chains;
1368 
1369  unsigned i;
1370  for (i = 0; i != NE; ++i) {
1371  Operands[0] = Chain;
1372  for (unsigned j = 1, e = N->getNumOperands(); j != e; ++j) {
1373  SDValue Operand = N->getOperand(j);
1374  EVT OperandVT = Operand.getValueType();
1375  if (OperandVT.isVector()) {
1376  EVT OperandEltVT = OperandVT.getVectorElementType();
1377  Operands[j] =
1378  DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT, Operand,
1379  DAG.getConstant(i, dl, TLI.getVectorIdxTy(
1380  DAG.getDataLayout())));
1381  } else {
1382  Operands[j] = Operand;
1383  }
1384  }
1385  SDValue Scalar = DAG.getNode(N->getOpcode(), dl, ChainVTs, Operands);
1386  Scalar.getNode()->setFlags(N->getFlags());
1387 
1388  //Add in the scalar as well as its chain value to the
1389  //result vectors.
1390  Scalars.push_back(Scalar);
1391  Chains.push_back(Scalar.getValue(1));
1392  }
1393 
1394  for (; i < ResNE; ++i)
1395  Scalars.push_back(DAG.getUNDEF(EltVT));
1396 
1397  // Build a new factor node to connect the chain back together.
1398  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1399  ReplaceValueWith(SDValue(N, 1), Chain);
1400 
1401  // Create a new BUILD_VECTOR node
1402  EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, ResNE);
1403  return DAG.getBuildVector(VecVT, dl, Scalars);
1404 }
1405 
1406 void DAGTypeLegalizer::SplitVecRes_OverflowOp(SDNode *N, unsigned ResNo,
1407  SDValue &Lo, SDValue &Hi) {
1408  SDLoc dl(N);
1409  EVT ResVT = N->getValueType(0);
1410  EVT OvVT = N->getValueType(1);
1411  EVT LoResVT, HiResVT, LoOvVT, HiOvVT;
1412  std::tie(LoResVT, HiResVT) = DAG.GetSplitDestVTs(ResVT);
1413  std::tie(LoOvVT, HiOvVT) = DAG.GetSplitDestVTs(OvVT);
1414 
1415  SDValue LoLHS, HiLHS, LoRHS, HiRHS;
1416  if (getTypeAction(ResVT) == TargetLowering::TypeSplitVector) {
1417  GetSplitVector(N->getOperand(0), LoLHS, HiLHS);
1418  GetSplitVector(N->getOperand(1), LoRHS, HiRHS);
1419  } else {
1420  std::tie(LoLHS, HiLHS) = DAG.SplitVectorOperand(N, 0);
1421  std::tie(LoRHS, HiRHS) = DAG.SplitVectorOperand(N, 1);
1422  }
1423 
1424  unsigned Opcode = N->getOpcode();
1425  SDVTList LoVTs = DAG.getVTList(LoResVT, LoOvVT);
1426  SDVTList HiVTs = DAG.getVTList(HiResVT, HiOvVT);
1427  SDNode *LoNode = DAG.getNode(Opcode, dl, LoVTs, LoLHS, LoRHS).getNode();
1428  SDNode *HiNode = DAG.getNode(Opcode, dl, HiVTs, HiLHS, HiRHS).getNode();
1429 
1430  Lo = SDValue(LoNode, ResNo);
1431  Hi = SDValue(HiNode, ResNo);
1432 
1433  // Replace the other vector result not being explicitly split here.
1434  unsigned OtherNo = 1 - ResNo;
1435  EVT OtherVT = N->getValueType(OtherNo);
1436  if (getTypeAction(OtherVT) == TargetLowering::TypeSplitVector) {
1437  SetSplitVector(SDValue(N, OtherNo),
1438  SDValue(LoNode, OtherNo), SDValue(HiNode, OtherNo));
1439  } else {
1440  SDValue OtherVal = DAG.getNode(
1441  ISD::CONCAT_VECTORS, dl, OtherVT,
1442  SDValue(LoNode, OtherNo), SDValue(HiNode, OtherNo));
1443  ReplaceValueWith(SDValue(N, OtherNo), OtherVal);
1444  }
1445 }
1446 
1447 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
1448  SDValue &Hi) {
1449  SDValue Vec = N->getOperand(0);
1450  SDValue Elt = N->getOperand(1);
1451  SDValue Idx = N->getOperand(2);
1452  SDLoc dl(N);
1453  GetSplitVector(Vec, Lo, Hi);
1454 
1455  if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
1456  unsigned IdxVal = CIdx->getZExtValue();
1457  unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
1458  if (IdxVal < LoNumElts)
1459  Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
1460  Lo.getValueType(), Lo, Elt, Idx);
1461  else
1462  Hi =
1463  DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, Hi.getValueType(), Hi, Elt,
1464  DAG.getConstant(IdxVal - LoNumElts, dl,
1465  TLI.getVectorIdxTy(DAG.getDataLayout())));
1466  return;
1467  }
1468 
1469  // See if the target wants to custom expand this node.
1470  if (CustomLowerNode(N, N->getValueType(0), true))
1471  return;
1472 
1473  // Make the vector elements byte-addressable if they aren't already.
1474  EVT VecVT = Vec.getValueType();
1475  EVT EltVT = VecVT.getVectorElementType();
1476  if (VecVT.getScalarSizeInBits() < 8) {
1477  EltVT = MVT::i8;
1478  VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT,
1479  VecVT.getVectorNumElements());
1480  Vec = DAG.getNode(ISD::ANY_EXTEND, dl, VecVT, Vec);
1481  // Extend the element type to match if needed.
1482  if (EltVT.bitsGT(Elt.getValueType()))
1483  Elt = DAG.getNode(ISD::ANY_EXTEND, dl, EltVT, Elt);
1484  }
1485 
1486  // Spill the vector to the stack.
1487  SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
1488  auto &MF = DAG.getMachineFunction();
1489  auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1490  auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
1491  SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo);
1492 
1493  // Store the new element. This may be larger than the vector element type,
1494  // so use a truncating store.
1495  SDValue EltPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1496  Type *VecType = VecVT.getTypeForEVT(*DAG.getContext());
1497  unsigned Alignment = DAG.getDataLayout().getPrefTypeAlignment(VecType);
1498  Store = DAG.getTruncStore(Store, dl, Elt, EltPtr,
1500 
1501  EVT LoVT, HiVT;
1502  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VecVT);
1503 
1504  // Load the Lo part from the stack slot.
1505  Lo = DAG.getLoad(LoVT, dl, Store, StackPtr, PtrInfo);
1506 
1507  // Increment the pointer to the other part.
1508  unsigned IncrementSize = LoVT.getSizeInBits() / 8;
1509  StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
1510  DAG.getConstant(IncrementSize, dl,
1511  StackPtr.getValueType()));
1512 
1513  // Load the Hi part from the stack slot.
1514  Hi = DAG.getLoad(HiVT, dl, Store, StackPtr,
1515  PtrInfo.getWithOffset(IncrementSize),
1516  MinAlign(Alignment, IncrementSize));
1517 
1518  // If we adjusted the original type, we need to truncate the results.
1519  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1520  if (LoVT != Lo.getValueType())
1521  Lo = DAG.getNode(ISD::TRUNCATE, dl, LoVT, Lo);
1522  if (HiVT != Hi.getValueType())
1523  Hi = DAG.getNode(ISD::TRUNCATE, dl, HiVT, Hi);
1524 }
1525 
1526 void DAGTypeLegalizer::SplitVecRes_SCALAR_TO_VECTOR(SDNode *N, SDValue &Lo,
1527  SDValue &Hi) {
1528  EVT LoVT, HiVT;
1529  SDLoc dl(N);
1530  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1531  Lo = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoVT, N->getOperand(0));
1532  Hi = DAG.getUNDEF(HiVT);
1533 }
1534 
1535 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
1536  SDValue &Hi) {
1537  assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
1538  EVT LoVT, HiVT;
1539  SDLoc dl(LD);
1540  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(LD->getValueType(0));
1541 
1543  SDValue Ch = LD->getChain();
1544  SDValue Ptr = LD->getBasePtr();
1545  SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
1546  EVT MemoryVT = LD->getMemoryVT();
1547  unsigned Alignment = LD->getOriginalAlignment();
1548  MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
1549  AAMDNodes AAInfo = LD->getAAInfo();
1550 
1551  EVT LoMemVT, HiMemVT;
1552  std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1553 
1554  Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, dl, Ch, Ptr, Offset,
1555  LD->getPointerInfo(), LoMemVT, Alignment, MMOFlags, AAInfo);
1556 
1557  unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
1558  Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
1559  Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, dl, Ch, Ptr, Offset,
1560  LD->getPointerInfo().getWithOffset(IncrementSize), HiMemVT,
1561  Alignment, MMOFlags, AAInfo);
1562 
1563  // Build a factor node to remember that this load is independent of the
1564  // other one.
1565  Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1566  Hi.getValue(1));
1567 
1568  // Legalize the chain result - switch anything that used the old chain to
1569  // use the new one.
1570  ReplaceValueWith(SDValue(LD, 1), Ch);
1571 }
1572 
1573 void DAGTypeLegalizer::SplitVecRes_MLOAD(MaskedLoadSDNode *MLD,
1574  SDValue &Lo, SDValue &Hi) {
1575  EVT LoVT, HiVT;
1576  SDLoc dl(MLD);
1577  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MLD->getValueType(0));
1578 
1579  SDValue Ch = MLD->getChain();
1580  SDValue Ptr = MLD->getBasePtr();
1581  SDValue Mask = MLD->getMask();
1582  SDValue PassThru = MLD->getPassThru();
1583  unsigned Alignment = MLD->getOriginalAlignment();
1585 
1586  // Split Mask operand
1587  SDValue MaskLo, MaskHi;
1588  if (Mask.getOpcode() == ISD::SETCC) {
1589  SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
1590  } else {
1591  if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
1592  GetSplitVector(Mask, MaskLo, MaskHi);
1593  else
1594  std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
1595  }
1596 
1597  EVT MemoryVT = MLD->getMemoryVT();
1598  EVT LoMemVT, HiMemVT;
1599  std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1600 
1601  SDValue PassThruLo, PassThruHi;
1602  if (getTypeAction(PassThru.getValueType()) == TargetLowering::TypeSplitVector)
1603  GetSplitVector(PassThru, PassThruLo, PassThruHi);
1604  else
1605  std::tie(PassThruLo, PassThruHi) = DAG.SplitVector(PassThru, dl);
1606 
1610  Alignment, MLD->getAAInfo(), MLD->getRanges());
1611 
1612  Lo = DAG.getMaskedLoad(LoVT, dl, Ch, Ptr, MaskLo, PassThruLo, LoMemVT, MMO,
1613  ExtType, MLD->isExpandingLoad());
1614 
1615  Ptr = TLI.IncrementMemoryAddress(Ptr, MaskLo, dl, LoMemVT, DAG,
1616  MLD->isExpandingLoad());
1617  unsigned HiOffset = LoMemVT.getStoreSize();
1618 
1621  HiMemVT.getStoreSize(), Alignment, MLD->getAAInfo(),
1622  MLD->getRanges());
1623 
1624  Hi = DAG.getMaskedLoad(HiVT, dl, Ch, Ptr, MaskHi, PassThruHi, HiMemVT, MMO,
1625  ExtType, MLD->isExpandingLoad());
1626 
1627  // Build a factor node to remember that this load is independent of the
1628  // other one.
1629  Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1630  Hi.getValue(1));
1631 
1632  // Legalize the chain result - switch anything that used the old chain to
1633  // use the new one.
1634  ReplaceValueWith(SDValue(MLD, 1), Ch);
1635 
1636 }
1637 
1638 void DAGTypeLegalizer::SplitVecRes_MGATHER(MaskedGatherSDNode *MGT,
1639  SDValue &Lo, SDValue &Hi) {
1640  EVT LoVT, HiVT;
1641  SDLoc dl(MGT);
1642  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MGT->getValueType(0));
1643 
1644  SDValue Ch = MGT->getChain();
1645  SDValue Ptr = MGT->getBasePtr();
1646  SDValue Mask = MGT->getMask();
1647  SDValue PassThru = MGT->getPassThru();
1648  SDValue Index = MGT->getIndex();
1649  SDValue Scale = MGT->getScale();
1650  unsigned Alignment = MGT->getOriginalAlignment();
1651 
1652  // Split Mask operand
1653  SDValue MaskLo, MaskHi;
1654  if (Mask.getOpcode() == ISD::SETCC) {
1655  SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
1656  } else {
1657  if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
1658  GetSplitVector(Mask, MaskLo, MaskHi);
1659  else
1660  std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
1661  }
1662 
1663  EVT MemoryVT = MGT->getMemoryVT();
1664  EVT LoMemVT, HiMemVT;
1665  // Split MemoryVT
1666  std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
1667 
1668  SDValue PassThruLo, PassThruHi;
1669  if (getTypeAction(PassThru.getValueType()) == TargetLowering::TypeSplitVector)
1670  GetSplitVector(PassThru, PassThruLo, PassThruHi);
1671  else
1672  std::tie(PassThruLo, PassThruHi) = DAG.SplitVector(PassThru, dl);
1673 
1674  SDValue IndexHi, IndexLo;
1675  if (getTypeAction(Index.getValueType()) == TargetLowering::TypeSplitVector)
1676  GetSplitVector(Index, IndexLo, IndexHi);
1677  else
1678  std::tie(IndexLo, IndexHi) = DAG.SplitVector(Index, dl);
1679 
1683  Alignment, MGT->getAAInfo(), MGT->getRanges());
1684 
1685  SDValue OpsLo[] = {Ch, PassThruLo, MaskLo, Ptr, IndexLo, Scale};
1686  Lo = DAG.getMaskedGather(DAG.getVTList(LoVT, MVT::Other), LoVT, dl, OpsLo,
1687  MMO, MGT->getIndexType());
1688 
1689  SDValue OpsHi[] = {Ch, PassThruHi, MaskHi, Ptr, IndexHi, Scale};
1690  Hi = DAG.getMaskedGather(DAG.getVTList(HiVT, MVT::Other), HiVT, dl, OpsHi,
1691  MMO, MGT->getIndexType());
1692 
1693  // Build a factor node to remember that this load is independent of the
1694  // other one.
1695  Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1696  Hi.getValue(1));
1697 
1698  // Legalize the chain result - switch anything that used the old chain to
1699  // use the new one.
1700  ReplaceValueWith(SDValue(MGT, 1), Ch);
1701 }
1702 
1703 
1704 void DAGTypeLegalizer::SplitVecRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
1705  assert(N->getValueType(0).isVector() &&
1706  N->getOperand(0).getValueType().isVector() &&
1707  "Operand types must be vectors");
1708 
1709  EVT LoVT, HiVT;
1710  SDLoc DL(N);
1711  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1712 
1713  // If the input also splits, handle it directly. Otherwise split it by hand.
1714  SDValue LL, LH, RL, RH;
1715  if (getTypeAction(N->getOperand(0).getValueType()) ==
1717  GetSplitVector(N->getOperand(0), LL, LH);
1718  else
1719  std::tie(LL, LH) = DAG.SplitVectorOperand(N, 0);
1720 
1721  if (getTypeAction(N->getOperand(1).getValueType()) ==
1723  GetSplitVector(N->getOperand(1), RL, RH);
1724  else
1725  std::tie(RL, RH) = DAG.SplitVectorOperand(N, 1);
1726 
1727  Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
1728  Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
1729 }
1730 
1731 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
1732  SDValue &Hi) {
1733  // Get the dest types - they may not match the input types, e.g. int_to_fp.
1734  EVT LoVT, HiVT;
1735  SDLoc dl(N);
1736  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1737 
1738  // If the input also splits, handle it directly for a compile time speedup.
1739  // Otherwise split it by hand.
1740  unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
1741  EVT InVT = N->getOperand(OpNo).getValueType();
1742  if (getTypeAction(InVT) == TargetLowering::TypeSplitVector)
1743  GetSplitVector(N->getOperand(OpNo), Lo, Hi);
1744  else
1745  std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, OpNo);
1746 
1747  if (N->getOpcode() == ISD::FP_ROUND) {
1748  Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo, N->getOperand(1));
1749  Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi, N->getOperand(1));
1750  } else if (N->getOpcode() == ISD::STRICT_FP_ROUND) {
1751  Lo = DAG.getNode(N->getOpcode(), dl, { LoVT, MVT::Other },
1752  { N->getOperand(0), Lo, N->getOperand(2) });
1753  Hi = DAG.getNode(N->getOpcode(), dl, { HiVT, MVT::Other },
1754  { N->getOperand(0), Hi, N->getOperand(2) });
1755  SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1756  Lo.getValue(1), Hi.getValue(1));
1757  ReplaceValueWith(SDValue(N, 1), NewChain);
1758  } else if (N->isStrictFPOpcode()) {
1759  Lo = DAG.getNode(N->getOpcode(), dl, { LoVT, MVT::Other },
1760  { N->getOperand(0), Lo });
1761  Hi = DAG.getNode(N->getOpcode(), dl, { HiVT, MVT::Other },
1762  { N->getOperand(0), Hi });
1763  // Legalize the chain result - switch anything that used the old chain to
1764  // use the new one.
1765  SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1766  Lo.getValue(1), Hi.getValue(1));
1767  ReplaceValueWith(SDValue(N, 1), NewChain);
1768  } else {
1769  Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo);
1770  Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi);
1771  }
1772 }
1773 
1774 void DAGTypeLegalizer::SplitVecRes_ExtendOp(SDNode *N, SDValue &Lo,
1775  SDValue &Hi) {
1776  SDLoc dl(N);
1777  EVT SrcVT = N->getOperand(0).getValueType();
1778  EVT DestVT = N->getValueType(0);
1779  EVT LoVT, HiVT;
1780  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(DestVT);
1781 
1782  // We can do better than a generic split operation if the extend is doing
1783  // more than just doubling the width of the elements and the following are
1784  // true:
1785  // - The number of vector elements is even,
1786  // - the source type is legal,
1787  // - the type of a split source is illegal,
1788  // - the type of an extended (by doubling element size) source is legal, and
1789  // - the type of that extended source when split is legal.
1790  //
1791  // This won't necessarily completely legalize the operation, but it will
1792  // more effectively move in the right direction and prevent falling down
1793  // to scalarization in many cases due to the input vector being split too
1794  // far.
1795  unsigned NumElements = SrcVT.getVectorNumElements();
1796  if ((NumElements & 1) == 0 &&
1797  SrcVT.getSizeInBits() * 2 < DestVT.getSizeInBits()) {
1798  LLVMContext &Ctx = *DAG.getContext();
1799  EVT NewSrcVT = SrcVT.widenIntegerVectorElementType(Ctx);
1800  EVT SplitSrcVT = SrcVT.getHalfNumVectorElementsVT(Ctx);
1801 
1802  EVT SplitLoVT, SplitHiVT;
1803  std::tie(SplitLoVT, SplitHiVT) = DAG.GetSplitDestVTs(NewSrcVT);
1804  if (TLI.isTypeLegal(SrcVT) && !TLI.isTypeLegal(SplitSrcVT) &&
1805  TLI.isTypeLegal(NewSrcVT) && TLI.isTypeLegal(SplitLoVT)) {
1806  LLVM_DEBUG(dbgs() << "Split vector extend via incremental extend:";
1807  N->dump(&DAG); dbgs() << "\n");
1808  // Extend the source vector by one step.
1809  SDValue NewSrc =
1810  DAG.getNode(N->getOpcode(), dl, NewSrcVT, N->getOperand(0));
1811  // Get the low and high halves of the new, extended one step, vector.
1812  std::tie(Lo, Hi) = DAG.SplitVector(NewSrc, dl);
1813  // Extend those vector halves the rest of the way.
1814  Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo);
1815  Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi);
1816  return;
1817  }
1818  }
1819  // Fall back to the generic unary operator splitting otherwise.
1820  SplitVecRes_UnaryOp(N, Lo, Hi);
1821 }
1822 
1823 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
1824  SDValue &Lo, SDValue &Hi) {
1825  // The low and high parts of the original input give four input vectors.
1826  SDValue Inputs[4];
1827  SDLoc dl(N);
1828  GetSplitVector(N->getOperand(0), Inputs[0], Inputs[1]);
1829  GetSplitVector(N->getOperand(1), Inputs[2], Inputs[3]);
1830  EVT NewVT = Inputs[0].getValueType();
1831  unsigned NewElts = NewVT.getVectorNumElements();
1832 
1833  // If Lo or Hi uses elements from at most two of the four input vectors, then
1834  // express it as a vector shuffle of those two inputs. Otherwise extract the
1835  // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR.
1837  for (unsigned High = 0; High < 2; ++High) {
1838  SDValue &Output = High ? Hi : Lo;
1839 
1840  // Build a shuffle mask for the output, discovering on the fly which
1841  // input vectors to use as shuffle operands (recorded in InputUsed).
1842  // If building a suitable shuffle vector proves too hard, then bail
1843  // out with useBuildVector set.
1844  unsigned InputUsed[2] = { -1U, -1U }; // Not yet discovered.
1845  unsigned FirstMaskIdx = High * NewElts;
1846  bool useBuildVector = false;
1847  for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
1848  // The mask element. This indexes into the input.
1849  int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
1850 
1851  // The input vector this mask element indexes into.
1852  unsigned Input = (unsigned)Idx / NewElts;
1853 
1854  if (Input >= array_lengthof(Inputs)) {
1855  // The mask element does not index into any input vector.
1856  Ops.push_back(-1);
1857  continue;
1858  }
1859 
1860  // Turn the index into an offset from the start of the input vector.
1861  Idx -= Input * NewElts;
1862 
1863  // Find or create a shuffle vector operand to hold this input.
1864  unsigned OpNo;
1865  for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
1866  if (InputUsed[OpNo] == Input) {
1867  // This input vector is already an operand.
1868  break;
1869  } else if (InputUsed[OpNo] == -1U) {
1870  // Create a new operand for this input vector.
1871  InputUsed[OpNo] = Input;
1872  break;
1873  }
1874  }
1875 
1876  if (OpNo >= array_lengthof(InputUsed)) {
1877  // More than two input vectors used! Give up on trying to create a
1878  // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
1879  useBuildVector = true;
1880  break;
1881  }
1882 
1883  // Add the mask index for the new shuffle vector.
1884  Ops.push_back(Idx + OpNo * NewElts);
1885  }
1886 
1887  if (useBuildVector) {
1888  EVT EltVT = NewVT.getVectorElementType();
1890 
1891  // Extract the input elements by hand.
1892  for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
1893  // The mask element. This indexes into the input.
1894  int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
1895 
1896  // The input vector this mask element indexes into.
1897  unsigned Input = (unsigned)Idx / NewElts;
1898 
1899  if (Input >= array_lengthof(Inputs)) {
1900  // The mask element is "undef" or indexes off the end of the input.
1901  SVOps.push_back(DAG.getUNDEF(EltVT));
1902  continue;
1903  }
1904 
1905  // Turn the index into an offset from the start of the input vector.
1906  Idx -= Input * NewElts;
1907 
1908  // Extract the vector element by hand.
1909  SVOps.push_back(DAG.getNode(
1910  ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Inputs[Input],
1911  DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
1912  }
1913 
1914  // Construct the Lo/Hi output using a BUILD_VECTOR.
1915  Output = DAG.getBuildVector(NewVT, dl, SVOps);
1916  } else if (InputUsed[0] == -1U) {
1917  // No input vectors were used! The result is undefined.
1918  Output = DAG.getUNDEF(NewVT);
1919  } else {
1920  SDValue Op0 = Inputs[InputUsed[0]];
1921  // If only one input was used, use an undefined vector for the other.
1922  SDValue Op1 = InputUsed[1] == -1U ?
1923  DAG.getUNDEF(NewVT) : Inputs[InputUsed[1]];
1924  // At least one input vector was used. Create a new shuffle vector.
1925  Output = DAG.getVectorShuffle(NewVT, dl, Op0, Op1, Ops);
1926  }
1927 
1928  Ops.clear();
1929  }
1930 }
1931 
1932 void DAGTypeLegalizer::SplitVecRes_VAARG(SDNode *N, SDValue &Lo, SDValue &Hi) {
1933  EVT OVT = N->getValueType(0);
1934  EVT NVT = OVT.getHalfNumVectorElementsVT(*DAG.getContext());
1935  SDValue Chain = N->getOperand(0);
1936  SDValue Ptr = N->getOperand(1);
1937  SDValue SV = N->getOperand(2);
1938  SDLoc dl(N);
1939 
1940  const unsigned Alignment = DAG.getDataLayout().getABITypeAlignment(
1941  NVT.getTypeForEVT(*DAG.getContext()));
1942 
1943  Lo = DAG.getVAArg(NVT, dl, Chain, Ptr, SV, Alignment);
1944  Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, SV, Alignment);
1945  Chain = Hi.getValue(1);
1946 
1947  // Modified the chain - switch anything that used the old chain to use
1948  // the new one.
1949  ReplaceValueWith(SDValue(N, 1), Chain);
1950 }
1951 
1952 
1953 //===----------------------------------------------------------------------===//
1954 // Operand Vector Splitting
1955 //===----------------------------------------------------------------------===//
1956 
1957 /// This method is called when the specified operand of the specified node is
1958 /// found to need vector splitting. At this point, all of the result types of
1959 /// the node are known to be legal, but other operands of the node may need
1960 /// legalization as well as the specified one.
1961 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
1962  LLVM_DEBUG(dbgs() << "Split node operand: "; N->dump(&DAG); dbgs() << "\n");
1963  SDValue Res = SDValue();
1964 
1965  // See if the target wants to custom split this node.
1966  if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1967  return false;
1968 
1969  if (!Res.getNode()) {
1970  switch (N->getOpcode()) {
1971  default:
1972 #ifndef NDEBUG
1973  dbgs() << "SplitVectorOperand Op #" << OpNo << ": ";
1974  N->dump(&DAG);
1975  dbgs() << "\n";
1976 #endif
1977  report_fatal_error("Do not know how to split this operator's "
1978  "operand!\n");
1979 
1980  case ISD::SETCC: Res = SplitVecOp_VSETCC(N); break;
1981  case ISD::BITCAST: Res = SplitVecOp_BITCAST(N); break;
1982  case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
1983  case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
1984  case ISD::CONCAT_VECTORS: Res = SplitVecOp_CONCAT_VECTORS(N); break;
1985  case ISD::TRUNCATE:
1986  Res = SplitVecOp_TruncateHelper(N);
1987  break;
1988  case ISD::STRICT_FP_ROUND:
1989  case ISD::FP_ROUND: Res = SplitVecOp_FP_ROUND(N); break;
1990  case ISD::FCOPYSIGN: Res = SplitVecOp_FCOPYSIGN(N); break;
1991  case ISD::STORE:
1992  Res = SplitVecOp_STORE(cast<StoreSDNode>(N), OpNo);
1993  break;
1994  case ISD::MSTORE:
1995  Res = SplitVecOp_MSTORE(cast<MaskedStoreSDNode>(N), OpNo);
1996  break;
1997  case ISD::MSCATTER:
1998  Res = SplitVecOp_MSCATTER(cast<MaskedScatterSDNode>(N), OpNo);
1999  break;
2000  case ISD::MGATHER:
2001  Res = SplitVecOp_MGATHER(cast<MaskedGatherSDNode>(N), OpNo);
2002  break;
2003  case ISD::VSELECT:
2004  Res = SplitVecOp_VSELECT(N, OpNo);
2005  break;
2006  case ISD::SINT_TO_FP:
2007  case ISD::UINT_TO_FP:
2008  if (N->getValueType(0).bitsLT(N->getOperand(0).getValueType()))
2009  Res = SplitVecOp_TruncateHelper(N);
2010  else
2011  Res = SplitVecOp_UnaryOp(N);
2012  break;
2013  case ISD::FP_TO_SINT:
2014  case ISD::FP_TO_UINT:
2017  case ISD::CTTZ:
2018  case ISD::CTLZ:
2019  case ISD::CTPOP:
2020  case ISD::STRICT_FP_EXTEND:
2021  case ISD::FP_EXTEND:
2022  case ISD::SIGN_EXTEND:
2023  case ISD::ZERO_EXTEND:
2024  case ISD::ANY_EXTEND:
2025  case ISD::FTRUNC:
2026  case ISD::FCANONICALIZE:
2027  Res = SplitVecOp_UnaryOp(N);
2028  break;
2029 
2033  Res = SplitVecOp_ExtVecInRegOp(N);
2034  break;
2035 
2036  case ISD::VECREDUCE_FADD:
2037  case ISD::VECREDUCE_FMUL:
2038  case ISD::VECREDUCE_ADD:
2039  case ISD::VECREDUCE_MUL:
2040  case ISD::VECREDUCE_AND:
2041  case ISD::VECREDUCE_OR:
2042  case ISD::VECREDUCE_XOR:
2043  case ISD::VECREDUCE_SMAX:
2044  case ISD::VECREDUCE_SMIN:
2045  case ISD::VECREDUCE_UMAX:
2046  case ISD::VECREDUCE_UMIN:
2047  case ISD::VECREDUCE_FMAX:
2048  case ISD::VECREDUCE_FMIN:
2049  Res = SplitVecOp_VECREDUCE(N, OpNo);
2050  break;
2051  }
2052  }
2053 
2054  // If the result is null, the sub-method took care of registering results etc.
2055  if (!Res.getNode()) return false;
2056 
2057  // If the result is N, the sub-method updated N in place. Tell the legalizer
2058  // core about this.
2059  if (Res.getNode() == N)
2060  return true;
2061 
2062  if (N->isStrictFPOpcode())
2063  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 2 &&
2064  "Invalid operand expansion");
2065  else
2066  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
2067  "Invalid operand expansion");
2068 
2069  ReplaceValueWith(SDValue(N, 0), Res);
2070  return false;
2071 }
2072 
2073 SDValue DAGTypeLegalizer::SplitVecOp_VSELECT(SDNode *N, unsigned OpNo) {
2074  // The only possibility for an illegal operand is the mask, since result type
2075  // legalization would have handled this node already otherwise.
2076  assert(OpNo == 0 && "Illegal operand must be mask");
2077 
2078  SDValue Mask = N->getOperand(0);
2079  SDValue Src0 = N->getOperand(1);
2080  SDValue Src1 = N->getOperand(2);
2081  EVT Src0VT = Src0.getValueType();
2082  SDLoc DL(N);
2083  assert(Mask.getValueType().isVector() && "VSELECT without a vector mask?");
2084 
2085  SDValue Lo, Hi;
2086  GetSplitVector(N->getOperand(0), Lo, Hi);
2087  assert(Lo.getValueType() == Hi.getValueType() &&
2088  "Lo and Hi have differing types");
2089 
2090  EVT LoOpVT, HiOpVT;
2091  std::tie(LoOpVT, HiOpVT) = DAG.GetSplitDestVTs(Src0VT);
2092  assert(LoOpVT == HiOpVT && "Asymmetric vector split?");
2093 
2094  SDValue LoOp0, HiOp0, LoOp1, HiOp1, LoMask, HiMask;
2095  std::tie(LoOp0, HiOp0) = DAG.SplitVector(Src0, DL);
2096  std::tie(LoOp1, HiOp1) = DAG.SplitVector(Src1, DL);
2097  std::tie(LoMask, HiMask) = DAG.SplitVector(Mask, DL);
2098 
2099  SDValue LoSelect =
2100  DAG.getNode(ISD::VSELECT, DL, LoOpVT, LoMask, LoOp0, LoOp1);
2101  SDValue HiSelect =
2102  DAG.getNode(ISD::VSELECT, DL, HiOpVT, HiMask, HiOp0, HiOp1);
2103 
2104  return DAG.getNode(ISD::CONCAT_VECTORS, DL, Src0VT, LoSelect, HiSelect);
2105 }
2106 
2107 SDValue DAGTypeLegalizer::SplitVecOp_VECREDUCE(SDNode *N, unsigned OpNo) {
2108  EVT ResVT = N->getValueType(0);
2109  SDValue Lo, Hi;
2110  SDLoc dl(N);
2111 
2112  SDValue VecOp = N->getOperand(OpNo);
2113  EVT VecVT = VecOp.getValueType();
2114  assert(VecVT.isVector() && "Can only split reduce vector operand");
2115  GetSplitVector(VecOp, Lo, Hi);
2116  EVT LoOpVT, HiOpVT;
2117  std::tie(LoOpVT, HiOpVT) = DAG.GetSplitDestVTs(VecVT);
2118 
2119  bool NoNaN = N->getFlags().hasNoNaNs();
2120  unsigned CombineOpc = 0;
2121  switch (N->getOpcode()) {
2122  case ISD::VECREDUCE_FADD: CombineOpc = ISD::FADD; break;
2123  case ISD::VECREDUCE_FMUL: CombineOpc = ISD::FMUL; break;
2124  case ISD::VECREDUCE_ADD: CombineOpc = ISD::ADD; break;
2125  case ISD::VECREDUCE_MUL: CombineOpc = ISD::MUL; break;
2126  case ISD::VECREDUCE_AND: CombineOpc = ISD::AND; break;
2127  case ISD::VECREDUCE_OR: CombineOpc = ISD::OR; break;
2128  case ISD::VECREDUCE_XOR: CombineOpc = ISD::XOR; break;
2129  case ISD::VECREDUCE_SMAX: CombineOpc = ISD::SMAX; break;
2130  case ISD::VECREDUCE_SMIN: CombineOpc = ISD::SMIN; break;
2131  case ISD::VECREDUCE_UMAX: CombineOpc = ISD::UMAX; break;
2132  case ISD::VECREDUCE_UMIN: CombineOpc = ISD::UMIN; break;
2133  case ISD::VECREDUCE_FMAX:
2134  CombineOpc = NoNaN ? ISD::FMAXNUM : ISD::FMAXIMUM;
2135  break;
2136  case ISD::VECREDUCE_FMIN:
2137  CombineOpc = NoNaN ? ISD::FMINNUM : ISD::FMINIMUM;
2138  break;
2139  default:
2140  llvm_unreachable("Unexpected reduce ISD node");
2141  }
2142 
2143  // Use the appropriate scalar instruction on the split subvectors before
2144  // reducing the now partially reduced smaller vector.
2145  SDValue Partial = DAG.getNode(CombineOpc, dl, LoOpVT, Lo, Hi, N->getFlags());
2146  return DAG.getNode(N->getOpcode(), dl, ResVT, Partial, N->getFlags());
2147 }
2148 
2149 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
2150  // The result has a legal vector type, but the input needs splitting.
2151  EVT ResVT = N->getValueType(0);
2152  SDValue Lo, Hi;
2153  SDLoc dl(N);
2154  GetSplitVector(N->getOperand(N->isStrictFPOpcode() ? 1 : 0), Lo, Hi);
2155  EVT InVT = Lo.getValueType();
2156 
2157  EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
2158  InVT.getVectorNumElements());
2159 
2160  if (N->isStrictFPOpcode()) {
2161  Lo = DAG.getNode(N->getOpcode(), dl, { OutVT, MVT::Other },
2162  { N->getOperand(0), Lo });
2163  Hi = DAG.getNode(N->getOpcode(), dl, { OutVT, MVT::Other },
2164  { N->getOperand(0), Hi });
2165 
2166  // Build a factor node to remember that this operation is independent
2167  // of the other one.
2168  SDValue Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2169  Hi.getValue(1));
2170 
2171  // Legalize the chain result - switch anything that used the old chain to
2172  // use the new one.
2173  ReplaceValueWith(SDValue(N, 1), Ch);
2174  } else {
2175  Lo = DAG.getNode(N->getOpcode(), dl, OutVT, Lo);
2176  Hi = DAG.getNode(N->getOpcode(), dl, OutVT, Hi);
2177  }
2178 
2179  return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
2180 }
2181 
2182 SDValue DAGTypeLegalizer::SplitVecOp_BITCAST(SDNode *N) {
2183  // For example, i64 = BITCAST v4i16 on alpha. Typically the vector will
2184  // end up being split all the way down to individual components. Convert the
2185  // split pieces into integers and reassemble.
2186  SDValue Lo, Hi;
2187  GetSplitVector(N->getOperand(0), Lo, Hi);
2188  Lo = BitConvertToInteger(Lo);
2189  Hi = BitConvertToInteger(Hi);
2190 
2191  if (DAG.getDataLayout().isBigEndian())
2192  std::swap(Lo, Hi);
2193 
2194  return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
2195  JoinIntegers(Lo, Hi));
2196 }
2197 
2198 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
2199  // We know that the extracted result type is legal.
2200  EVT SubVT = N->getValueType(0);
2201  SDValue Idx = N->getOperand(1);
2202  SDLoc dl(N);
2203  SDValue Lo, Hi;
2204  GetSplitVector(N->getOperand(0), Lo, Hi);
2205 
2206  uint64_t LoElts = Lo.getValueType().getVectorNumElements();
2207  uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
2208 
2209  if (IdxVal < LoElts) {
2210  assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
2211  "Extracted subvector crosses vector split!");
2212  return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Lo, Idx);
2213  } else {
2214  return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Hi,
2215  DAG.getConstant(IdxVal - LoElts, dl,
2216  Idx.getValueType()));
2217  }
2218 }
2219 
2220 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
2221  SDValue Vec = N->getOperand(0);
2222  SDValue Idx = N->getOperand(1);
2223  EVT VecVT = Vec.getValueType();
2224 
2225  if (isa<ConstantSDNode>(Idx)) {
2226  uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
2227 
2228  SDValue Lo, Hi;
2229  GetSplitVector(Vec, Lo, Hi);
2230 
2231  uint64_t LoElts = Lo.getValueType().getVectorNumElements();
2232 
2233  if (IdxVal < LoElts)
2234  return SDValue(DAG.UpdateNodeOperands(N, Lo, Idx), 0);
2235  return SDValue(DAG.UpdateNodeOperands(N, Hi,
2236  DAG.getConstant(IdxVal - LoElts, SDLoc(N),
2237  Idx.getValueType())), 0);
2238  }
2239 
2240  // See if the target wants to custom expand this node.
2241  if (CustomLowerNode(N, N->getValueType(0), true))
2242  return SDValue();
2243 
2244  // Make the vector elements byte-addressable if they aren't already.
2245  SDLoc dl(N);
2246  EVT EltVT = VecVT.getVectorElementType();
2247  if (VecVT.getScalarSizeInBits() < 8) {
2248  EltVT = MVT::i8;
2249  VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT,
2250  VecVT.getVectorNumElements());
2251  Vec = DAG.getNode(ISD::ANY_EXTEND, dl, VecVT, Vec);
2252  }
2253 
2254  // Store the vector to the stack.
2255  SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
2256  auto &MF = DAG.getMachineFunction();
2257  auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
2258  auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
2259  SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo);
2260 
2261  // Load back the required element.
2262  StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
2263 
2264  // FIXME: This is to handle i1 vectors with elements promoted to i8.
2265  // i1 vector handling needs general improvement.
2266  if (N->getValueType(0).bitsLT(EltVT)) {
2267  SDValue Load = DAG.getLoad(EltVT, dl, Store, StackPtr,
2269  return DAG.getZExtOrTrunc(Load, dl, N->getValueType(0));
2270  }
2271 
2272  return DAG.getExtLoad(
2273  ISD::EXTLOAD, dl, N->getValueType(0), Store, StackPtr,
2275 }
2276 
2277 SDValue DAGTypeLegalizer::SplitVecOp_ExtVecInRegOp(SDNode *N) {
2278  SDValue Lo, Hi;
2279 
2280  // *_EXTEND_VECTOR_INREG only reference the lower half of the input, so
2281  // splitting the result has the same effect as splitting the input operand.
2282  SplitVecRes_ExtVecInRegOp(N, Lo, Hi);
2283 
2284  return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), N->getValueType(0), Lo, Hi);
2285 }
2286 
2287 SDValue DAGTypeLegalizer::SplitVecOp_MGATHER(MaskedGatherSDNode *MGT,
2288  unsigned OpNo) {
2289  EVT LoVT, HiVT;
2290  SDLoc dl(MGT);
2291  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MGT->getValueType(0));
2292 
2293  SDValue Ch = MGT->getChain();
2294  SDValue Ptr = MGT->getBasePtr();
2295  SDValue Index = MGT->getIndex();
2296  SDValue Scale = MGT->getScale();
2297  SDValue Mask = MGT->getMask();
2298  SDValue PassThru = MGT->getPassThru();
2299  unsigned Alignment = MGT->getOriginalAlignment();
2300 
2301  SDValue MaskLo, MaskHi;
2302  if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
2303  // Split Mask operand
2304  GetSplitVector(Mask, MaskLo, MaskHi);
2305  else
2306  std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
2307 
2308  EVT MemoryVT = MGT->getMemoryVT();
2309  EVT LoMemVT, HiMemVT;
2310  std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
2311 
2312  SDValue PassThruLo, PassThruHi;
2313  if (getTypeAction(PassThru.getValueType()) == TargetLowering::TypeSplitVector)
2314  GetSplitVector(PassThru, PassThruLo, PassThruHi);
2315  else
2316  std::tie(PassThruLo, PassThruHi) = DAG.SplitVector(PassThru, dl);
2317 
2318  SDValue IndexHi, IndexLo;
2319  if (getTypeAction(Index.getValueType()) == TargetLowering::TypeSplitVector)
2320  GetSplitVector(Index, IndexLo, IndexHi);
2321  else
2322  std::tie(IndexLo, IndexHi) = DAG.SplitVector(Index, dl);
2323 
2327  Alignment, MGT->getAAInfo(), MGT->getRanges());
2328 
2329  SDValue OpsLo[] = {Ch, PassThruLo, MaskLo, Ptr, IndexLo, Scale};
2330  SDValue Lo = DAG.getMaskedGather(DAG.getVTList(LoVT, MVT::Other), LoVT, dl,
2331  OpsLo, MMO, MGT->getIndexType());
2332 
2333  MMO = DAG.getMachineFunction().
2336  Alignment, MGT->getAAInfo(),
2337  MGT->getRanges());
2338 
2339  SDValue OpsHi[] = {Ch, PassThruHi, MaskHi, Ptr, IndexHi, Scale};
2340  SDValue Hi = DAG.getMaskedGather(DAG.getVTList(HiVT, MVT::Other), HiVT, dl,
2341  OpsHi, MMO, MGT->getIndexType());
2342 
2343  // Build a factor node to remember that this load is independent of the
2344  // other one.
2345  Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2346  Hi.getValue(1));
2347 
2348  // Legalize the chain result - switch anything that used the old chain to
2349  // use the new one.
2350  ReplaceValueWith(SDValue(MGT, 1), Ch);
2351 
2352  SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, MGT->getValueType(0), Lo,
2353  Hi);
2354  ReplaceValueWith(SDValue(MGT, 0), Res);
2355  return SDValue();
2356 }
2357 
2358 SDValue DAGTypeLegalizer::SplitVecOp_MSTORE(MaskedStoreSDNode *N,
2359  unsigned OpNo) {
2360  SDValue Ch = N->getChain();
2361  SDValue Ptr = N->getBasePtr();
2362  SDValue Mask = N->getMask();
2363  SDValue Data = N->getValue();
2364  EVT MemoryVT = N->getMemoryVT();
2365  unsigned Alignment = N->getOriginalAlignment();
2366  SDLoc DL(N);
2367 
2368  EVT LoMemVT, HiMemVT;
2369  std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
2370 
2371  SDValue DataLo, DataHi;
2372  if (getTypeAction(Data.getValueType()) == TargetLowering::TypeSplitVector)
2373  // Split Data operand
2374  GetSplitVector(Data, DataLo, DataHi);
2375  else
2376  std::tie(DataLo, DataHi) = DAG.SplitVector(Data, DL);
2377 
2378  // Split Mask operand
2379  SDValue MaskLo, MaskHi;
2380  if (OpNo == 1 && Mask.getOpcode() == ISD::SETCC) {
2381  SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
2382  } else {
2383  if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
2384  GetSplitVector(Mask, MaskLo, MaskHi);
2385  else
2386  std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, DL);
2387  }
2388 
2389  SDValue Lo, Hi;
2393  Alignment, N->getAAInfo(), N->getRanges());
2394 
2395  Lo = DAG.getMaskedStore(Ch, DL, DataLo, Ptr, MaskLo, LoMemVT, MMO,
2396  N->isTruncatingStore(),
2397  N->isCompressingStore());
2398 
2399  Ptr = TLI.IncrementMemoryAddress(Ptr, MaskLo, DL, LoMemVT, DAG,
2400  N->isCompressingStore());
2401  unsigned HiOffset = LoMemVT.getStoreSize();
2402 
2405  HiMemVT.getStoreSize(), Alignment, N->getAAInfo(),
2406  N->getRanges());
2407 
2408  Hi = DAG.getMaskedStore(Ch, DL, DataHi, Ptr, MaskHi, HiMemVT, MMO,
2410 
2411  // Build a factor node to remember that this store is independent of the
2412  // other one.
2413  return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
2414 }
2415 
2416 SDValue DAGTypeLegalizer::SplitVecOp_MSCATTER(MaskedScatterSDNode *N,
2417  unsigned OpNo) {
2418  SDValue Ch = N->getChain();
2419  SDValue Ptr = N->getBasePtr();
2420  SDValue Mask = N->getMask();
2421  SDValue Index = N->getIndex();
2422  SDValue Scale = N->getScale();
2423  SDValue Data = N->getValue();
2424  EVT MemoryVT = N->getMemoryVT();
2425  unsigned Alignment = N->getOriginalAlignment();
2426  SDLoc DL(N);
2427 
2428  // Split all operands
2429  EVT LoMemVT, HiMemVT;
2430  std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
2431 
2432  SDValue DataLo, DataHi;
2433  if (getTypeAction(Data.getValueType()) == TargetLowering::TypeSplitVector)
2434  // Split Data operand
2435  GetSplitVector(Data, DataLo, DataHi);
2436  else
2437  std::tie(DataLo, DataHi) = DAG.SplitVector(Data, DL);
2438 
2439  // Split Mask operand
2440  SDValue MaskLo, MaskHi;
2441  if (OpNo == 1 && Mask.getOpcode() == ISD::SETCC) {
2442  SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
2443  } else {
2444  if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
2445  GetSplitVector(Mask, MaskLo, MaskHi);
2446  else
2447  std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, DL);
2448  }
2449 
2450  SDValue IndexHi, IndexLo;
2451  if (getTypeAction(Index.getValueType()) == TargetLowering::TypeSplitVector)
2452  GetSplitVector(Index, IndexLo, IndexHi);
2453  else
2454  std::tie(IndexLo, IndexHi) = DAG.SplitVector(Index, DL);
2455 
2456  SDValue Lo;
2460  Alignment, N->getAAInfo(), N->getRanges());
2461 
2462  SDValue OpsLo[] = {Ch, DataLo, MaskLo, Ptr, IndexLo, Scale};
2463  Lo = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), DataLo.getValueType(),
2464  DL, OpsLo, MMO, N->getIndexType());
2465 
2466  MMO = DAG.getMachineFunction().
2469  Alignment, N->getAAInfo(), N->getRanges());
2470 
2471  // The order of the Scatter operation after split is well defined. The "Hi"
2472  // part comes after the "Lo". So these two operations should be chained one
2473  // after another.
2474  SDValue OpsHi[] = {Lo, DataHi, MaskHi, Ptr, IndexHi, Scale};
2475  return DAG.getMaskedScatter(DAG.getVTList(MVT::Other), DataHi.getValueType(),
2476  DL, OpsHi, MMO, N->getIndexType());
2477 }
2478 
2479 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
2480  assert(N->isUnindexed() && "Indexed store of vector?");
2481  assert(OpNo == 1 && "Can only split the stored value");
2482  SDLoc DL(N);
2483 
2484  bool isTruncating = N->isTruncatingStore();
2485  SDValue Ch = N->getChain();
2486  SDValue Ptr = N->getBasePtr();
2487  EVT MemoryVT = N->getMemoryVT();
2488  unsigned Alignment = N->getOriginalAlignment();
2489  MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
2490  AAMDNodes AAInfo = N->getAAInfo();
2491  SDValue Lo, Hi;
2492  GetSplitVector(N->getOperand(1), Lo, Hi);
2493 
2494  EVT LoMemVT, HiMemVT;
2495  std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
2496 
2497  // Scalarize if the split halves are not byte-sized.
2498  if (!LoMemVT.isByteSized() || !HiMemVT.isByteSized())
2499  return TLI.scalarizeVectorStore(N, DAG);
2500 
2501  unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
2502 
2503  if (isTruncating)
2504  Lo = DAG.getTruncStore(Ch, DL, Lo, Ptr, N->getPointerInfo(), LoMemVT,
2505  Alignment, MMOFlags, AAInfo);
2506  else
2507  Lo = DAG.getStore(Ch, DL, Lo, Ptr, N->getPointerInfo(), Alignment, MMOFlags,
2508  AAInfo);
2509 
2510  // Increment the pointer to the other half.
2511  Ptr = DAG.getObjectPtrOffset(DL, Ptr, IncrementSize);
2512 
2513  if (isTruncating)
2514  Hi = DAG.getTruncStore(Ch, DL, Hi, Ptr,
2515  N->getPointerInfo().getWithOffset(IncrementSize),
2516  HiMemVT, Alignment, MMOFlags, AAInfo);
2517  else
2518  Hi = DAG.getStore(Ch, DL, Hi, Ptr,
2519  N->getPointerInfo().getWithOffset(IncrementSize),
2520  Alignment, MMOFlags, AAInfo);
2521 
2522  return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
2523 }
2524 
2525 SDValue DAGTypeLegalizer::SplitVecOp_CONCAT_VECTORS(SDNode *N) {
2526  SDLoc DL(N);
2527 
2528  // The input operands all must have the same type, and we know the result
2529  // type is valid. Convert this to a buildvector which extracts all the
2530  // input elements.
2531  // TODO: If the input elements are power-two vectors, we could convert this to
2532  // a new CONCAT_VECTORS node with elements that are half-wide.
2534  EVT EltVT = N->getValueType(0).getVectorElementType();
2535  for (const SDValue &Op : N->op_values()) {
2536  for (unsigned i = 0, e = Op.getValueType().getVectorNumElements();
2537  i != e; ++i) {
2538  Elts.push_back(DAG.getNode(
2539  ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Op,
2540  DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
2541  }
2542  }
2543 
2544  return DAG.getBuildVector(N->getValueType(0), DL, Elts);
2545 }
2546 
2547 SDValue DAGTypeLegalizer::SplitVecOp_TruncateHelper(SDNode *N) {
2548  // The result type is legal, but the input type is illegal. If splitting
2549  // ends up with the result type of each half still being legal, just
2550  // do that. If, however, that would result in an illegal result type,
2551  // we can try to get more clever with power-two vectors. Specifically,
2552  // split the input type, but also widen the result element size, then
2553  // concatenate the halves and truncate again. For example, consider a target
2554  // where v8i8 is legal and v8i32 is not (ARM, which doesn't have 256-bit
2555  // vectors). To perform a "%res = v8i8 trunc v8i32 %in" we do:
2556  // %inlo = v4i32 extract_subvector %in, 0
2557  // %inhi = v4i32 extract_subvector %in, 4
2558  // %lo16 = v4i16 trunc v4i32 %inlo
2559  // %hi16 = v4i16 trunc v4i32 %inhi
2560  // %in16 = v8i16 concat_vectors v4i16 %lo16, v4i16 %hi16
2561  // %res = v8i8 trunc v8i16 %in16
2562  //
2563  // Without this transform, the original truncate would end up being
2564  // scalarized, which is pretty much always a last resort.
2565  SDValue InVec = N->getOperand(0);
2566  EVT InVT = InVec->getValueType(0);
2567  EVT OutVT = N->getValueType(0);
2568  unsigned NumElements = OutVT.getVectorNumElements();
2569  bool IsFloat = OutVT.isFloatingPoint();
2570 
2571  // Widening should have already made sure this is a power-two vector
2572  // if we're trying to split it at all. assert() that's true, just in case.
2573  assert(!(NumElements & 1) && "Splitting vector, but not in half!");
2574 
2575  unsigned InElementSize = InVT.getScalarSizeInBits();
2576  unsigned OutElementSize = OutVT.getScalarSizeInBits();
2577 
2578  // Determine the split output VT. If its legal we can just split dirctly.
2579  EVT LoOutVT, HiOutVT;
2580  std::tie(LoOutVT, HiOutVT) = DAG.GetSplitDestVTs(OutVT);
2581  assert(LoOutVT == HiOutVT && "Unequal split?");
2582 
2583  // If the input elements are only 1/2 the width of the result elements,
2584  // just use the normal splitting. Our trick only work if there's room
2585  // to split more than once.
2586  if (isTypeLegal(LoOutVT) ||
2587  InElementSize <= OutElementSize * 2)
2588  return SplitVecOp_UnaryOp(N);
2589  SDLoc DL(N);
2590 
2591  // Don't touch if this will be scalarized.
2592  EVT FinalVT = InVT;
2593  while (getTypeAction(FinalVT) == TargetLowering::TypeSplitVector)
2594  FinalVT = FinalVT.getHalfNumVectorElementsVT(*DAG.getContext());
2595 
2596  if (getTypeAction(FinalVT) == TargetLowering::TypeScalarizeVector)
2597  return SplitVecOp_UnaryOp(N);
2598 
2599  // Get the split input vector.
2600  SDValue InLoVec, InHiVec;
2601  GetSplitVector(InVec, InLoVec, InHiVec);
2602 
2603  // Truncate them to 1/2 the element size.
2604  EVT HalfElementVT = IsFloat ?
2605  EVT::getFloatingPointVT(InElementSize/2) :
2606  EVT::getIntegerVT(*DAG.getContext(), InElementSize/2);
2607  EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT,
2608  NumElements/2);
2609  SDValue HalfLo = DAG.getNode(N->getOpcode(), DL, HalfVT, InLoVec);
2610  SDValue HalfHi = DAG.getNode(N->getOpcode(), DL, HalfVT, InHiVec);
2611  // Concatenate them to get the full intermediate truncation result.
2612  EVT InterVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT, NumElements);
2613  SDValue InterVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InterVT, HalfLo,
2614  HalfHi);
2615  // Now finish up by truncating all the way down to the original result
2616  // type. This should normally be something that ends up being legal directly,
2617  // but in theory if a target has very wide vectors and an annoyingly
2618  // restricted set of legal types, this split can chain to build things up.
2619  return IsFloat
2620  ? DAG.getNode(ISD::FP_ROUND, DL, OutVT, InterVec,
2621  DAG.getTargetConstant(
2622  0, DL, TLI.getPointerTy(DAG.getDataLayout())))
2623  : DAG.getNode(ISD::TRUNCATE, DL, OutVT, InterVec);
2624 }
2625 
2626 SDValue DAGTypeLegalizer::SplitVecOp_VSETCC(SDNode *N) {
2627  assert(N->getValueType(0).isVector() &&
2628  N->getOperand(0).getValueType().isVector() &&
2629  "Operand types must be vectors");
2630  // The result has a legal vector type, but the input needs splitting.
2631  SDValue Lo0, Hi0, Lo1, Hi1, LoRes, HiRes;
2632  SDLoc DL(N);
2633  GetSplitVector(N->getOperand(0), Lo0, Hi0);
2634  GetSplitVector(N->getOperand(1), Lo1, Hi1);
2635  unsigned PartElements = Lo0.getValueType().getVectorNumElements();
2636  EVT PartResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, PartElements);
2637  EVT WideResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, 2*PartElements);
2638 
2639  LoRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Lo0, Lo1, N->getOperand(2));
2640  HiRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Hi0, Hi1, N->getOperand(2));
2641  SDValue Con = DAG.getNode(ISD::CONCAT_VECTORS, DL, WideResVT, LoRes, HiRes);
2642  return PromoteTargetBoolean(Con, N->getValueType(0));
2643 }
2644 
2645 
2646 SDValue DAGTypeLegalizer::SplitVecOp_FP_ROUND(SDNode *N) {
2647  // The result has a legal vector type, but the input needs splitting.
2648  EVT ResVT = N->getValueType(0);
2649  SDValue Lo, Hi;
2650  SDLoc DL(N);
2651  GetSplitVector(N->getOperand(N->isStrictFPOpcode() ? 1 : 0), Lo, Hi);
2652  EVT InVT = Lo.getValueType();
2653 
2654  EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
2655  InVT.getVectorNumElements());
2656 
2657  if (N->isStrictFPOpcode()) {
2658  Lo = DAG.getNode(N->getOpcode(), DL, { OutVT, MVT::Other },
2659  { N->getOperand(0), Lo, N->getOperand(2) });
2660  Hi = DAG.getNode(N->getOpcode(), DL, { OutVT, MVT::Other },
2661  { N->getOperand(0), Hi, N->getOperand(2) });
2662  // Legalize the chain result - switch anything that used the old chain to
2663  // use the new one.
2664  SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
2665  Lo.getValue(1), Hi.getValue(1));
2666  ReplaceValueWith(SDValue(N, 1), NewChain);
2667  } else {
2668  Lo = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Lo, N->getOperand(1));
2669  Hi = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Hi, N->getOperand(1));
2670  }
2671 
2672  return DAG.getNode(ISD::CONCAT_VECTORS, DL, ResVT, Lo, Hi);
2673 }
2674 
2675 SDValue DAGTypeLegalizer::SplitVecOp_FCOPYSIGN(SDNode *N) {
2676  // The result (and the first input) has a legal vector type, but the second
2677  // input needs splitting.
2678  return DAG.UnrollVectorOp(N, N->getValueType(0).getVectorNumElements());
2679 }
2680 
2681 
2682 //===----------------------------------------------------------------------===//
2683 // Result Vector Widening
2684 //===----------------------------------------------------------------------===//
2685 
2686 void DAGTypeLegalizer::WidenVectorResult(SDNode *N, unsigned ResNo) {
2687  LLVM_DEBUG(dbgs() << "Widen node result " << ResNo << ": "; N->dump(&DAG);
2688  dbgs() << "\n");
2689 
2690  // See if the target wants to custom widen this node.
2691  if (CustomWidenLowerNode(N, N->getValueType(ResNo)))
2692  return;
2693 
2694  SDValue Res = SDValue();
2695  switch (N->getOpcode()) {
2696  default:
2697 #ifndef NDEBUG
2698  dbgs() << "WidenVectorResult #" << ResNo << ": ";
2699  N->dump(&DAG);
2700  dbgs() << "\n";
2701 #endif
2702  llvm_unreachable("Do not know how to widen the result of this operator!");
2703 
2704  case ISD::MERGE_VALUES: Res = WidenVecRes_MERGE_VALUES(N, ResNo); break;
2705  case ISD::BITCAST: Res = WidenVecRes_BITCAST(N); break;
2706  case ISD::BUILD_VECTOR: Res = WidenVecRes_BUILD_VECTOR(N); break;
2707  case ISD::CONCAT_VECTORS: Res = WidenVecRes_CONCAT_VECTORS(N); break;
2708  case ISD::EXTRACT_SUBVECTOR: Res = WidenVecRes_EXTRACT_SUBVECTOR(N); break;
2709  case ISD::INSERT_VECTOR_ELT: Res = WidenVecRes_INSERT_VECTOR_ELT(N); break;
2710  case ISD::LOAD: Res = WidenVecRes_LOAD(N); break;
2711  case ISD::SCALAR_TO_VECTOR: Res = WidenVecRes_SCALAR_TO_VECTOR(N); break;
2712  case ISD::SIGN_EXTEND_INREG: Res = WidenVecRes_InregOp(N); break;
2713  case ISD::VSELECT:
2714  case ISD::SELECT: Res = WidenVecRes_SELECT(N); break;
2715  case ISD::SELECT_CC: Res = WidenVecRes_SELECT_CC(N); break;
2716  case ISD::SETCC: Res = WidenVecRes_SETCC(N); break;
2717  case ISD::UNDEF: Res = WidenVecRes_UNDEF(N); break;
2718  case ISD::VECTOR_SHUFFLE:
2719  Res = WidenVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N));
2720  break;
2721  case ISD::MLOAD:
2722  Res = WidenVecRes_MLOAD(cast<MaskedLoadSDNode>(N));
2723  break;
2724  case ISD::MGATHER:
2725  Res = WidenVecRes_MGATHER(cast<MaskedGatherSDNode>(N));
2726  break;
2727 
2728  case ISD::ADD:
2729  case ISD::AND:
2730  case ISD::MUL:
2731  case ISD::MULHS:
2732  case ISD::MULHU:
2733  case ISD::OR:
2734  case ISD::SUB:
2735  case ISD::XOR:
2736  case ISD::FMINNUM:
2737  case ISD::FMAXNUM:
2738  case ISD::FMINIMUM:
2739  case ISD::FMAXIMUM:
2740  case ISD::SMIN:
2741  case ISD::SMAX:
2742  case ISD::UMIN:
2743  case ISD::UMAX:
2744  case ISD::UADDSAT:
2745  case ISD::SADDSAT:
2746  case ISD::USUBSAT:
2747  case ISD::SSUBSAT:
2748  Res = WidenVecRes_Binary(N);
2749  break;
2750 
2751  case ISD::FADD:
2752  case ISD::FMUL:
2753  case ISD::FPOW:
2754  case ISD::FSUB:
2755  case ISD::FDIV:
2756  case ISD::FREM:
2757  case ISD::SDIV:
2758  case ISD::UDIV:
2759  case ISD::SREM:
2760  case ISD::UREM:
2761  Res = WidenVecRes_BinaryCanTrap(N);
2762  break;
2763 
2764  case ISD::SMULFIX:
2765  case ISD::SMULFIXSAT:
2766  case ISD::UMULFIX:
2767  case ISD::UMULFIXSAT:
2768  // These are binary operations, but with an extra operand that shouldn't
2769  // be widened (the scale).
2770  Res = WidenVecRes_BinaryWithExtraScalarOp(N);
2771  break;
2772 
2773  case ISD::STRICT_FADD:
2774  case ISD::STRICT_FSUB:
2775  case ISD::STRICT_FMUL:
2776  case ISD::STRICT_FDIV:
2777  case ISD::STRICT_FREM:
2778  case ISD::STRICT_FSQRT:
2779  case ISD::STRICT_FMA:
2780  case ISD::STRICT_FPOW:
2781  case ISD::STRICT_FPOWI:
2782  case ISD::STRICT_FSIN:
2783  case ISD::STRICT_FCOS:
2784  case ISD::STRICT_FEXP:
2785  case ISD::STRICT_FEXP2:
2786  case ISD::STRICT_FLOG:
2787  case ISD::STRICT_FLOG10:
2788  case ISD::STRICT_FLOG2:
2789  case ISD::STRICT_FRINT:
2791  case ISD::STRICT_FMAXNUM:
2792  case ISD::STRICT_FMINNUM:
2793  case ISD::STRICT_FCEIL:
2794  case ISD::STRICT_FFLOOR:
2795  case ISD::STRICT_FROUND:
2796  case ISD::STRICT_FTRUNC:
2797  Res = WidenVecRes_StrictFP(N);
2798  break;
2799 
2800  case ISD::UADDO:
2801  case ISD::SADDO:
2802  case ISD::USUBO:
2803  case ISD::SSUBO:
2804  case ISD::UMULO:
2805  case ISD::SMULO:
2806  Res = WidenVecRes_OverflowOp(N, ResNo);
2807  break;
2808 
2809  case ISD::FCOPYSIGN:
2810  Res = WidenVecRes_FCOPYSIGN(N);
2811  break;
2812 
2813  case ISD::FPOWI:
2814  Res = WidenVecRes_POWI(N);
2815  break;
2816 
2817  case ISD::SHL:
2818  case ISD::SRA:
2819  case ISD::SRL:
2820  Res = WidenVecRes_Shift(N);
2821  break;
2822 
2826  Res = WidenVecRes_EXTEND_VECTOR_INREG(N);
2827  break;
2828 
2829  case ISD::ANY_EXTEND:
2830  case ISD::FP_EXTEND:
2831  case ISD::FP_ROUND:
2832  case ISD::FP_TO_SINT:
2833  case ISD::FP_TO_UINT:
2834  case ISD::SIGN_EXTEND:
2835  case ISD::SINT_TO_FP:
2836  case ISD::TRUNCATE:
2837  case ISD::UINT_TO_FP:
2838  case ISD::ZERO_EXTEND:
2839  Res = WidenVecRes_Convert(N);
2840  break;
2841 
2842  case ISD::STRICT_FP_EXTEND:
2843  case ISD::STRICT_FP_ROUND:
2846  Res = WidenVecRes_Convert_StrictFP(N);
2847  break;
2848 
2849  case ISD::FABS:
2850  case ISD::FCEIL:
2851  case ISD::FCOS:
2852  case ISD::FEXP:
2853  case ISD::FEXP2:
2854  case ISD::FFLOOR:
2855  case ISD::FLOG:
2856  case ISD::FLOG10:
2857  case ISD::FLOG2:
2858  case ISD::FNEARBYINT:
2859  case ISD::FRINT:
2860  case ISD::FROUND:
2861  case ISD::FSIN:
2862  case ISD::FSQRT:
2863  case ISD::FTRUNC: {
2864  // We're going to widen this vector op to a legal type by padding with undef
2865  // elements. If the wide vector op is eventually going to be expanded to
2866  // scalar libcalls, then unroll into scalar ops now to avoid unnecessary
2867  // libcalls on the undef elements.
2868  EVT VT = N->getValueType(0);
2869  EVT WideVecVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2870  if (!TLI.isOperationLegalOrCustom(N->getOpcode(), WideVecVT) &&
2871  TLI.isOperationExpand(N->getOpcode(), VT.getScalarType())) {
2872  Res = DAG.UnrollVectorOp(N, WideVecVT.getVectorNumElements());
2873  break;
2874  }
2875  }
2876  // If the target has custom/legal support for the scalar FP intrinsic ops
2877  // (they are probably not destined to become libcalls), then widen those like
2878  // any other unary ops.
2880 
2881  case ISD::ABS:
2882  case ISD::BITREVERSE:
2883  case ISD::BSWAP:
2884  case ISD::CTLZ:
2885  case ISD::CTLZ_ZERO_UNDEF:
2886  case ISD::CTPOP:
2887  case ISD::CTTZ:
2888  case ISD::CTTZ_ZERO_UNDEF:
2889  case ISD::FNEG:
2890  case ISD::FCANONICALIZE:
2891  Res = WidenVecRes_Unary(N);
2892  break;
2893  case ISD::FMA:
2894  Res = WidenVecRes_Ternary(N);
2895  break;
2896  }
2897 
2898  // If Res is null, the sub-method took care of registering the result.
2899  if (Res.getNode())
2900  SetWidenedVector(SDValue(N, ResNo), Res);
2901 }
2902 
2903 SDValue DAGTypeLegalizer::WidenVecRes_Ternary(SDNode *N) {
2904  // Ternary op widening.
2905  SDLoc dl(N);
2906  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2907  SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2908  SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2909  SDValue InOp3 = GetWidenedVector(N->getOperand(2));
2910  return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, InOp3);
2911 }
2912 
2913 SDValue DAGTypeLegalizer::WidenVecRes_Binary(SDNode *N) {
2914  // Binary op widening.
2915  SDLoc dl(N);
2916  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2917  SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2918  SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2919  return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, N->getFlags());
2920 }
2921 
2922 SDValue DAGTypeLegalizer::WidenVecRes_BinaryWithExtraScalarOp(SDNode *N) {
2923  // Binary op widening, but with an extra operand that shouldn't be widened.
2924  SDLoc dl(N);
2925  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2926  SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2927  SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2928  SDValue InOp3 = N->getOperand(2);
2929  return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, InOp3,
2930  N->getFlags());
2931 }
2932 
2933 // Given a vector of operations that have been broken up to widen, see
2934 // if we can collect them together into the next widest legal VT. This
2935 // implementation is trap-safe.
2937  SmallVectorImpl<SDValue> &ConcatOps,
2938  unsigned ConcatEnd, EVT VT, EVT MaxVT,
2939  EVT WidenVT) {
2940  // Check to see if we have a single operation with the widen type.
2941  if (ConcatEnd == 1) {
2942  VT = ConcatOps[0].getValueType();
2943  if (VT == WidenVT)
2944  return ConcatOps[0];
2945  }
2946 
2947  SDLoc dl(ConcatOps[0]);
2948  EVT WidenEltVT = WidenVT.getVectorElementType();
2949 
2950  // while (Some element of ConcatOps is not of type MaxVT) {
2951  // From the end of ConcatOps, collect elements of the same type and put
2952  // them into an op of the next larger supported type
2953  // }
2954  while (ConcatOps[ConcatEnd-1].getValueType() != MaxVT) {
2955  int Idx = ConcatEnd - 1;
2956  VT = ConcatOps[Idx--].getValueType();
2957  while (Idx >= 0 && ConcatOps[Idx].getValueType() == VT)
2958  Idx--;
2959 
2960  int NextSize = VT.isVector() ? VT.getVectorNumElements() : 1;
2961  EVT NextVT;
2962  do {
2963  NextSize *= 2;
2964  NextVT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NextSize);
2965  } while (!TLI.isTypeLegal(NextVT));
2966 
2967  if (!VT.isVector()) {
2968  // Scalar type, create an INSERT_VECTOR_ELEMENT of type NextVT
2969  SDValue VecOp = DAG.getUNDEF(NextVT);
2970  unsigned NumToInsert = ConcatEnd - Idx - 1;
2971  for (unsigned i = 0, OpIdx = Idx+1; i < NumToInsert; i++, OpIdx++) {
2972  VecOp = DAG.getNode(
2973  ISD::INSERT_VECTOR_ELT, dl, NextVT, VecOp, ConcatOps[OpIdx],
2974  DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
2975  }
2976  ConcatOps[Idx+1] = VecOp;
2977  ConcatEnd = Idx + 2;
2978  } else {
2979  // Vector type, create a CONCAT_VECTORS of type NextVT
2980  SDValue undefVec = DAG.getUNDEF(VT);
2981  unsigned OpsToConcat = NextSize/VT.getVectorNumElements();
2982  SmallVector<SDValue, 16> SubConcatOps(OpsToConcat);
2983  unsigned RealVals = ConcatEnd - Idx - 1;
2984  unsigned SubConcatEnd = 0;
2985  unsigned SubConcatIdx = Idx + 1;
2986  while (SubConcatEnd < RealVals)
2987  SubConcatOps[SubConcatEnd++] = ConcatOps[++Idx];
2988  while (SubConcatEnd < OpsToConcat)
2989  SubConcatOps[SubConcatEnd++] = undefVec;
2990  ConcatOps[SubConcatIdx] = DAG.getNode(ISD::CONCAT_VECTORS, dl,
2991  NextVT, SubConcatOps);
2992  ConcatEnd = SubConcatIdx + 1;
2993  }
2994  }
2995 
2996  // Check to see if we have a single operation with the widen type.
2997  if (ConcatEnd == 1) {
2998  VT = ConcatOps[0].getValueType();
2999  if (VT == WidenVT)
3000  return ConcatOps[0];
3001  }
3002 
3003  // add undefs of size MaxVT until ConcatOps grows to length of WidenVT
3004  unsigned NumOps = WidenVT.getVectorNumElements()/MaxVT.getVectorNumElements();
3005  if (NumOps != ConcatEnd ) {
3006  SDValue UndefVal = DAG.getUNDEF(MaxVT);
3007  for (unsigned j = ConcatEnd; j < NumOps; ++j)
3008  ConcatOps[j] = UndefVal;
3009  }
3010  return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
3011  makeArrayRef(ConcatOps.data(), NumOps));
3012 }
3013 
3014 SDValue DAGTypeLegalizer::WidenVecRes_BinaryCanTrap(SDNode *N) {
3015  // Binary op widening for operations that can trap.
3016  unsigned Opcode = N->getOpcode();
3017  SDLoc dl(N);
3018  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3019  EVT WidenEltVT = WidenVT.getVectorElementType();
3020  EVT VT = WidenVT;
3021  unsigned NumElts = VT.getVectorNumElements();
3022  const SDNodeFlags Flags = N->getFlags();
3023  while (!TLI.isTypeLegal(VT) && NumElts != 1) {
3024  NumElts = NumElts / 2;
3025  VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
3026  }
3027 
3028  if (NumElts != 1 && !TLI.canOpTrap(N->getOpcode(), VT)) {
3029  // Operation doesn't trap so just widen as normal.
3030  SDValue InOp1 = GetWidenedVector(N->getOperand(0));
3031  SDValue InOp2 = GetWidenedVector(N->getOperand(1));
3032  return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, Flags);
3033  }
3034 
3035  // No legal vector version so unroll the vector operation and then widen.
3036  if (NumElts == 1)
3037  return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
3038 
3039  // Since the operation can trap, apply operation on the original vector.
3040  EVT MaxVT = VT;
3041  SDValue InOp1 = GetWidenedVector(N->getOperand(0));
3042  SDValue InOp2 = GetWidenedVector(N->getOperand(1));
3043  unsigned CurNumElts = N->getValueType(0).getVectorNumElements();
3044 
3045  SmallVector<SDValue, 16> ConcatOps(CurNumElts);
3046  unsigned ConcatEnd = 0; // Current ConcatOps index.
3047  int Idx = 0; // Current Idx into input vectors.
3048 
3049  // NumElts := greatest legal vector size (at most WidenVT)
3050  // while (orig. vector has unhandled elements) {
3051  // take munches of size NumElts from the beginning and add to ConcatOps
3052  // NumElts := next smaller supported vector size or 1
3053  // }
3054  while (CurNumElts != 0) {
3055  while (CurNumElts >= NumElts) {
3056  SDValue EOp1 = DAG.getNode(
3057  ISD::EXTRACT_SUBVECTOR, dl, VT, InOp1,
3058  DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3059  SDValue EOp2 = DAG.getNode(
3060  ISD::EXTRACT_SUBVECTOR, dl, VT, InOp2,
3061  DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3062  ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, VT, EOp1, EOp2, Flags);
3063  Idx += NumElts;
3064  CurNumElts -= NumElts;
3065  }
3066  do {
3067  NumElts = NumElts / 2;
3068  VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
3069  } while (!TLI.isTypeLegal(VT) && NumElts != 1);
3070 
3071  if (NumElts == 1) {
3072  for (unsigned i = 0; i != CurNumElts; ++i, ++Idx) {
3073  SDValue EOp1 = DAG.getNode(
3074  ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT, InOp1,
3075  DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3076  SDValue EOp2 = DAG.getNode(
3077  ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT, InOp2,
3078  DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3079  ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, WidenEltVT,
3080  EOp1, EOp2, Flags);
3081  }
3082  CurNumElts = 0;
3083  }
3084  }
3085 
3086  return CollectOpsToWiden(DAG, TLI, ConcatOps, ConcatEnd, VT, MaxVT, WidenVT);
3087 }
3088 
3089 SDValue DAGTypeLegalizer::WidenVecRes_StrictFP(SDNode *N) {
3090  // StrictFP op widening for operations that can trap.
3091  unsigned NumOpers = N->getNumOperands();
3092  unsigned Opcode = N->getOpcode();
3093  SDLoc dl(N);
3094  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3095  EVT WidenEltVT = WidenVT.getVectorElementType();
3096  EVT VT = WidenVT;
3097  unsigned NumElts = VT.getVectorNumElements();
3098  while (!TLI.isTypeLegal(VT) && NumElts != 1) {
3099  NumElts = NumElts / 2;
3100  VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
3101  }
3102 
3103  // No legal vector version so unroll the vector operation and then widen.
3104  if (NumElts == 1)
3105  return UnrollVectorOp_StrictFP(N, WidenVT.getVectorNumElements());
3106 
3107  // Since the operation can trap, apply operation on the original vector.
3108  EVT MaxVT = VT;
3110  unsigned CurNumElts = N->getValueType(0).getVectorNumElements();
3111 
3112  SmallVector<SDValue, 16> ConcatOps(CurNumElts);
3113  SmallVector<SDValue, 16> Chains;
3114  unsigned ConcatEnd = 0; // Current ConcatOps index.
3115  int Idx = 0; // Current Idx into input vectors.
3116 
3117  // The Chain is the first operand.
3118  InOps.push_back(N->getOperand(0));
3119 
3120  // Now process the remaining operands.
3121  for (unsigned i = 1; i < NumOpers; ++i) {
3122  SDValue Oper = N->getOperand(i);
3123 
3124  if (Oper.getValueType().isVector()) {
3125  assert(Oper.getValueType() == N->getValueType(0) &&
3126  "Invalid operand type to widen!");
3127  Oper = GetWidenedVector(Oper);
3128  }
3129 
3130  InOps.push_back(Oper);
3131  }
3132 
3133  // NumElts := greatest legal vector size (at most WidenVT)
3134  // while (orig. vector has unhandled elements) {
3135  // take munches of size NumElts from the beginning and add to ConcatOps
3136  // NumElts := next smaller supported vector size or 1
3137  // }
3138  while (CurNumElts != 0) {
3139  while (CurNumElts >= NumElts) {
3141 
3142  for (unsigned i = 0; i < NumOpers; ++i) {
3143  SDValue Op = InOps[i];
3144 
3145  if (Op.getValueType().isVector())
3146  Op = DAG.getNode(
3147  ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
3148  DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3149 
3150  EOps.push_back(Op);
3151  }
3152 
3153  EVT OperVT[] = {VT, MVT::Other};
3154  SDValue Oper = DAG.getNode(Opcode, dl, OperVT, EOps);
3155  ConcatOps[ConcatEnd++] = Oper;
3156  Chains.push_back(Oper.getValue(1));
3157  Idx += NumElts;
3158  CurNumElts -= NumElts;
3159  }
3160  do {
3161  NumElts = NumElts / 2;
3162  VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
3163  } while (!TLI.isTypeLegal(VT) && NumElts != 1);
3164 
3165  if (NumElts == 1) {
3166  for (unsigned i = 0; i != CurNumElts; ++i, ++Idx) {
3168 
3169  for (unsigned i = 0; i < NumOpers; ++i) {
3170  SDValue Op = InOps[i];
3171 
3172  if (Op.getValueType().isVector())
3173  Op = DAG.getNode(
3174  ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT, Op,
3175  DAG.getConstant(Idx, dl,
3176  TLI.getVectorIdxTy(DAG.getDataLayout())));
3177 
3178  EOps.push_back(Op);
3179  }
3180 
3181  EVT WidenVT[] = {WidenEltVT, MVT::Other};
3182  SDValue Oper = DAG.getNode(Opcode, dl, WidenVT, EOps);
3183  ConcatOps[ConcatEnd++] = Oper;
3184  Chains.push_back(Oper.getValue(1));
3185  }
3186  CurNumElts = 0;
3187  }
3188  }
3189 
3190  // Build a factor node to remember all the Ops that have been created.
3191  SDValue NewChain;
3192  if (Chains.size() == 1)
3193  NewChain = Chains[0];
3194  else
3195  NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
3196  ReplaceValueWith(SDValue(N, 1), NewChain);
3197 
3198  return CollectOpsToWiden(DAG, TLI, ConcatOps, ConcatEnd, VT, MaxVT, WidenVT);
3199 }
3200 
3201 SDValue DAGTypeLegalizer::WidenVecRes_OverflowOp(SDNode *N, unsigned ResNo) {
3202  SDLoc DL(N);
3203  EVT ResVT = N->getValueType(0);
3204  EVT OvVT = N->getValueType(1);
3205  EVT WideResVT, WideOvVT;
3206  SDValue WideLHS, WideRHS;
3207 
3208  // TODO: This might result in a widen/split loop.
3209  if (ResNo == 0) {
3210  WideResVT = TLI.getTypeToTransformTo(*DAG.getContext(), ResVT);
3211  WideOvVT = EVT::getVectorVT(
3212  *DAG.getContext(), OvVT.getVectorElementType(),
3213  WideResVT.getVectorNumElements());
3214 
3215  WideLHS = GetWidenedVector(N->getOperand(0));
3216  WideRHS = GetWidenedVector(N->getOperand(1));
3217  } else {
3218  WideOvVT = TLI.getTypeToTransformTo(*DAG.getContext(), OvVT);
3219  WideResVT = EVT::getVectorVT(
3220  *DAG.getContext(), ResVT.getVectorElementType(),
3221  WideOvVT.getVectorNumElements());
3222 
3223  SDValue Zero = DAG.getConstant(
3224  0, DL, TLI.getVectorIdxTy(DAG.getDataLayout()));
3225  WideLHS = DAG.getNode(
3226  ISD::INSERT_SUBVECTOR, DL, WideResVT, DAG.getUNDEF(WideResVT),
3227  N->getOperand(0), Zero);
3228  WideRHS = DAG.getNode(
3229  ISD::INSERT_SUBVECTOR, DL, WideResVT, DAG.getUNDEF(WideResVT),
3230  N->getOperand(1), Zero);
3231  }
3232 
3233  SDVTList WideVTs = DAG.getVTList(WideResVT, WideOvVT);
3234  SDNode *WideNode = DAG.getNode(
3235  N->getOpcode(), DL, WideVTs, WideLHS, WideRHS).getNode();
3236 
3237  // Replace the other vector result not being explicitly widened here.
3238  unsigned OtherNo = 1 - ResNo;
3239  EVT OtherVT = N->getValueType(OtherNo);
3240  if (getTypeAction(OtherVT) == TargetLowering::TypeWidenVector) {
3241  SetWidenedVector(SDValue(N, OtherNo), SDValue(WideNode, OtherNo));
3242  } else {
3243  SDValue Zero = DAG.getConstant(
3244  0, DL, TLI.getVectorIdxTy(DAG.getDataLayout()));
3245  SDValue OtherVal = DAG.getNode(
3246  ISD::EXTRACT_SUBVECTOR, DL, OtherVT, SDValue(WideNode, OtherNo), Zero);
3247  ReplaceValueWith(SDValue(N, OtherNo), OtherVal);
3248  }
3249 
3250  return SDValue(WideNode, ResNo);
3251 }
3252 
3253 SDValue DAGTypeLegalizer::WidenVecRes_Convert(SDNode *N) {
3254  SDValue InOp = N->getOperand(0);
3255  SDLoc DL(N);
3256 
3257  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3258  unsigned WidenNumElts = WidenVT.getVectorNumElements();
3259 
3260  EVT InVT = InOp.getValueType();
3261  EVT InEltVT = InVT.getVectorElementType();
3262  EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
3263 
3264  unsigned Opcode = N->getOpcode();
3265  unsigned InVTNumElts = InVT.getVectorNumElements();
3266  const SDNodeFlags Flags = N->getFlags();
3267  if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
3268  InOp = GetWidenedVector(N->getOperand(0));
3269  InVT = InOp.getValueType();
3270  InVTNumElts = InVT.getVectorNumElements();
3271  if (InVTNumElts == WidenNumElts) {
3272  if (N->getNumOperands() == 1)
3273  return DAG.getNode(Opcode, DL, WidenVT, InOp);
3274  return DAG.getNode(Opcode, DL, WidenVT, InOp, N->getOperand(1), Flags);
3275  }
3276  if (WidenVT.getSizeInBits() == InVT.getSizeInBits()) {
3277  // If both input and result vector types are of same width, extend
3278  // operations should be done with SIGN/ZERO_EXTEND_VECTOR_INREG, which
3279  // accepts fewer elements in the result than in the input.
3280  if (Opcode == ISD::ANY_EXTEND)
3281  return DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, WidenVT, InOp);
3282  if (Opcode == ISD::SIGN_EXTEND)
3283  return DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, WidenVT, InOp);
3284  if (Opcode == ISD::ZERO_EXTEND)
3285  return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, WidenVT, InOp);
3286  }
3287  }
3288 
3289  if (TLI.isTypeLegal(InWidenVT)) {
3290  // Because the result and the input are different vector types, widening
3291  // the result could create a legal type but widening the input might make
3292  // it an illegal type that might lead to repeatedly splitting the input
3293  // and then widening it. To avoid this, we widen the input only if
3294  // it results in a legal type.
3295  if (WidenNumElts % InVTNumElts == 0) {
3296  // Widen the input and call convert on the widened input vector.
3297  unsigned NumConcat = WidenNumElts/InVTNumElts;
3298  SmallVector<SDValue, 16> Ops(NumConcat, DAG.getUNDEF(InVT));
3299  Ops[0] = InOp;
3300  SDValue InVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InWidenVT, Ops);
3301  if (N->getNumOperands() == 1)
3302  return DAG.getNode(Opcode, DL, WidenVT, InVec);
3303  return DAG.getNode(Opcode, DL, WidenVT, InVec, N->getOperand(1), Flags);
3304  }
3305 
3306  if (InVTNumElts % WidenNumElts == 0) {
3307  SDValue InVal = DAG.getNode(
3308  ISD::EXTRACT_SUBVECTOR, DL, InWidenVT, InOp,
3309  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
3310  // Extract the input and convert the shorten input vector.
3311  if (N->getNumOperands() == 1)
3312  return DAG.getNode(Opcode, DL, WidenVT, InVal);
3313  return DAG.getNode(Opcode, DL, WidenVT, InVal, N->getOperand(1), Flags);
3314  }
3315  }
3316 
3317  // Otherwise unroll into some nasty scalar code and rebuild the vector.
3318  EVT EltVT = WidenVT.getVectorElementType();
3319  SmallVector<SDValue, 16> Ops(WidenNumElts, DAG.getUNDEF(EltVT));
3320  // Use the original element count so we don't do more scalar opts than
3321  // necessary.
3322  unsigned MinElts = N->getValueType(0).getVectorNumElements();
3323  for (unsigned i=0; i < MinElts; ++i) {
3324  SDValue Val = DAG.getNode(
3325  ISD::EXTRACT_VECTOR_ELT, DL, InEltVT, InOp,
3326  DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
3327  if (N->getNumOperands() == 1)
3328  Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val);
3329  else
3330  Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val, N->getOperand(1), Flags);
3331  }
3332 
3333  return DAG.getBuildVector(WidenVT, DL, Ops);
3334 }
3335 
3336 SDValue DAGTypeLegalizer::WidenVecRes_Convert_StrictFP(SDNode *N) {
3337  SDValue InOp = N->getOperand(1);
3338  SDLoc DL(N);
3339  SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
3340 
3341  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3342  unsigned WidenNumElts = WidenVT.getVectorNumElements();
3343  SmallVector<EVT, 2> WidenVTs = { WidenVT, MVT::Other };
3344 
3345  EVT InVT = InOp.getValueType();
3346  EVT InEltVT = InVT.getVectorElementType();
3347 
3348  unsigned Opcode = N->getOpcode();
3349 
3350  // FIXME: Optimizations need to be implemented here.
3351 
3352  // Otherwise unroll into some nasty scalar code and rebuild the vector.
3353  EVT EltVT = WidenVT.getVectorElementType();
3354  SmallVector<EVT, 2> EltVTs = { EltVT, MVT::Other };
3355  SmallVector<SDValue, 16> Ops(WidenNumElts, DAG.getUNDEF(EltVT));
3356  SmallVector<SDValue, 32> OpChains;
3357  // Use the original element count so we don't do more scalar opts than
3358  // necessary.
3359  unsigned MinElts = N->getValueType(0).getVectorNumElements();
3360  for (unsigned i=0; i < MinElts; ++i) {
3361  NewOps[1] = DAG.getNode(
3362  ISD::EXTRACT_VECTOR_ELT, DL, InEltVT, InOp,
3363  DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
3364  Ops[i] = DAG.getNode(Opcode, DL, EltVTs, NewOps);
3365  OpChains.push_back(Ops[i].getValue(1));
3366  }
3367  SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OpChains);
3368  ReplaceValueWith(SDValue(N, 1), NewChain);
3369 
3370  return DAG.getBuildVector(WidenVT, DL, Ops);
3371 }
3372 
3373 SDValue DAGTypeLegalizer::WidenVecRes_EXTEND_VECTOR_INREG(SDNode *N) {
3374  unsigned Opcode = N->getOpcode();
3375  SDValue InOp = N->getOperand(0);
3376  SDLoc DL(N);
3377 
3378  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3379  EVT WidenSVT = WidenVT.getVectorElementType();
3380  unsigned WidenNumElts = WidenVT.getVectorNumElements();
3381 
3382  EVT InVT = InOp.getValueType();
3383  EVT InSVT = InVT.getVectorElementType();
3384  unsigned InVTNumElts = InVT.getVectorNumElements();
3385 
3386  if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
3387  InOp = GetWidenedVector(InOp);
3388  InVT = InOp.getValueType();
3389  if (InVT.getSizeInBits() == WidenVT.getSizeInBits()) {
3390  switch (Opcode) {
3394  return DAG.getNode(Opcode, DL, WidenVT, InOp);
3395  }
3396  }
3397  }
3398 
3399  // Unroll, extend the scalars and rebuild the vector.
3401  for (unsigned i = 0, e = std::min(InVTNumElts, WidenNumElts); i != e; ++i) {
3402  SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, InSVT, InOp,
3403  DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
3404  switch (Opcode) {
3406  Val = DAG.getNode(ISD::ANY_EXTEND, DL, WidenSVT, Val);
3407  break;
3409  Val = DAG.getNode(ISD::SIGN_EXTEND, DL, WidenSVT, Val);
3410  break;
3412  Val = DAG.getNode(ISD::ZERO_EXTEND, DL, WidenSVT, Val);
3413  break;
3414  default:
3415  llvm_unreachable("A *_EXTEND_VECTOR_INREG node was expected");
3416  }
3417  Ops.push_back(Val);
3418  }
3419 
3420  while (Ops.size() != WidenNumElts)
3421  Ops.push_back(DAG.getUNDEF(WidenSVT));
3422 
3423  return DAG.getBuildVector(WidenVT, DL, Ops);
3424 }
3425 
3426 SDValue DAGTypeLegalizer::WidenVecRes_FCOPYSIGN(SDNode *N) {
3427  // If this is an FCOPYSIGN with same input types, we can treat it as a
3428  // normal (can trap) binary op.
3429  if (N->getOperand(0).getValueType() == N->getOperand(1).getValueType())
3430  return WidenVecRes_BinaryCanTrap(N);
3431 
3432  // If the types are different, fall back to unrolling.
3433  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3434  return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
3435 }
3436 
3437 SDValue DAGTypeLegalizer::WidenVecRes_POWI(SDNode *N) {
3438  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3439  SDValue InOp = GetWidenedVector(N->getOperand(0));
3440  SDValue ShOp = N->getOperand(1);
3441  return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ShOp);
3442 }
3443 
3444 SDValue DAGTypeLegalizer::WidenVecRes_Shift(SDNode *N) {
3445  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3446  SDValue InOp = GetWidenedVector(N->getOperand(0));
3447  SDValue ShOp = N->getOperand(1);
3448 
3449  EVT ShVT = ShOp.getValueType();
3450  if (getTypeAction(ShVT) == TargetLowering::TypeWidenVector) {
3451  ShOp = GetWidenedVector(ShOp);
3452  ShVT = ShOp.getValueType();
3453  }
3454  EVT ShWidenVT = EVT::getVectorVT(*DAG.getContext(),
3455  ShVT.getVectorElementType(),
3456  WidenVT.getVectorNumElements());
3457  if (ShVT != ShWidenVT)
3458  ShOp = ModifyToType(ShOp, ShWidenVT);
3459 
3460  return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ShOp);
3461 }
3462 
3463 SDValue DAGTypeLegalizer::WidenVecRes_Unary(SDNode *N) {
3464  // Unary op widening.
3465  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3466  SDValue InOp = GetWidenedVector(N->getOperand(0));
3467  return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp);
3468 }
3469 
3470 SDValue DAGTypeLegalizer::WidenVecRes_InregOp(SDNode *N) {
3471  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3472  EVT ExtVT = EVT::getVectorVT(*DAG.getContext(),
3473  cast<VTSDNode>(N->getOperand(1))->getVT()
3474  .getVectorElementType(),
3475  WidenVT.getVectorNumElements());
3476  SDValue WidenLHS = GetWidenedVector(N->getOperand(0));
3477  return DAG.getNode(N->getOpcode(), SDLoc(N),
3478  WidenVT, WidenLHS, DAG.getValueType(ExtVT));
3479 }
3480 
3481 SDValue DAGTypeLegalizer::WidenVecRes_MERGE_VALUES(SDNode *N, unsigned ResNo) {
3482  SDValue WidenVec = DisintegrateMERGE_VALUES(N, ResNo);
3483  return GetWidenedVector(WidenVec);
3484 }
3485 
3486 SDValue DAGTypeLegalizer::WidenVecRes_BITCAST(SDNode *N) {
3487  SDValue InOp = N->getOperand(0);
3488  EVT InVT = InOp.getValueType();
3489  EVT VT = N->getValueType(0);
3490  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3491  SDLoc dl(N);
3492 
3493  switch (getTypeAction(InVT)) {
3495  break;
3497  // If the incoming type is a vector that is being promoted, then
3498  // we know that the elements are arranged differently and that we
3499  // must perform the conversion using a stack slot.
3500  if (InVT.isVector())
3501  break;
3502 
3503  // If the InOp is promoted to the same size, convert it. Otherwise,
3504  // fall out of the switch and widen the promoted input.
3505  InOp = GetPromotedInteger(InOp);
3506  InVT = InOp.getValueType();
3507  if (WidenVT.bitsEq(InVT))
3508  return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
3509  break;
3516  break;
3518  // If the InOp is widened to the same size, convert it. Otherwise, fall
3519  // out of the switch and widen the widened input.
3520  InOp = GetWidenedVector(InOp);
3521  InVT = InOp.getValueType();
3522  if (WidenVT.bitsEq(InVT))
3523  // The input widens to the same size. Convert to the widen value.
3524  return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
3525  break;
3526  }
3527 
3528  unsigned WidenSize = WidenVT.getSizeInBits();
3529  unsigned InSize = InVT.getSizeInBits();
3530  // x86mmx is not an acceptable vector element type, so don't try.
3531  if (WidenSize % InSize == 0 && InVT != MVT::x86mmx) {
3532  // Determine new input vector type. The new input vector type will use
3533  // the same element type (if its a vector) or use the input type as a
3534  // vector. It is the same size as the type to widen to.
3535  EVT NewInVT;
3536  unsigned NewNumElts = WidenSize / InSize;
3537  if (InVT.isVector()) {
3538  EVT InEltVT = InVT.getVectorElementType();
3539  NewInVT = EVT::getVectorVT(*DAG.getContext(), InEltVT,
3540  WidenSize / InEltVT.getSizeInBits());
3541  } else {
3542  NewInVT = EVT::getVectorVT(*DAG.getContext(), InVT, NewNumElts);
3543  }
3544 
3545  if (TLI.isTypeLegal(NewInVT)) {
3546  SDValue NewVec;
3547  if (InVT.isVector()) {
3548  // Because the result and the input are different vector types, widening
3549  // the result could create a legal type but widening the input might make
3550  // it an illegal type that might lead to repeatedly splitting the input
3551  // and then widening it. To avoid this, we widen the input only if
3552  // it results in a legal type.
3553  SmallVector<SDValue, 16> Ops(NewNumElts, DAG.getUNDEF(InVT));
3554  Ops[0] = InOp;
3555 
3556  NewVec = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewInVT, Ops);
3557  } else {
3558  NewVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewInVT, InOp);
3559  }
3560  return DAG.getNode(ISD::BITCAST, dl, WidenVT, NewVec);
3561  }
3562  }
3563 
3564  return CreateStackStoreLoad(InOp, WidenVT);
3565 }
3566 
3567 SDValue DAGTypeLegalizer::WidenVecRes_BUILD_VECTOR(SDNode *N) {
3568  SDLoc dl(N);
3569  // Build a vector with undefined for the new nodes.
3570  EVT VT = N->getValueType(0);
3571 
3572  // Integer BUILD_VECTOR operands may be larger than the node's vector element
3573  // type. The UNDEFs need to have the same type as the existing operands.
3574  EVT EltVT = N->getOperand(0).getValueType();
3575  unsigned NumElts = VT.getVectorNumElements();
3576 
3577  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3578  unsigned WidenNumElts = WidenVT.getVectorNumElements();
3579 
3580  SmallVector<SDValue, 16> NewOps(N->op_begin(), N->op_end());
3581  assert(WidenNumElts >= NumElts && "Shrinking vector instead of widening!");
3582  NewOps.append(WidenNumElts - NumElts, DAG.getUNDEF(EltVT));
3583 
3584  return DAG.getBuildVector(WidenVT, dl, NewOps);
3585 }
3586 
3587 SDValue DAGTypeLegalizer::WidenVecRes_CONCAT_VECTORS(SDNode *N) {
3588  EVT InVT = N->getOperand(0).getValueType();
3589  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3590  SDLoc dl(N);
3591  unsigned WidenNumElts = WidenVT.getVectorNumElements();
3592  unsigned NumInElts = InVT.getVectorNumElements();
3593  unsigned NumOperands = N->getNumOperands();
3594 
3595  bool InputWidened = false; // Indicates we need to widen the input.
3596  if (getTypeAction(InVT) != TargetLowering::TypeWidenVector) {
3597  if (WidenVT.getVectorNumElements() % InVT.getVectorNumElements() == 0) {
3598  // Add undef vectors to widen to correct length.
3599  unsigned NumConcat = WidenVT.getVectorNumElements() /
3600  InVT.getVectorNumElements();
3601  SDValue UndefVal = DAG.getUNDEF(InVT);
3602  SmallVector<SDValue, 16> Ops(NumConcat);
3603  for (unsigned i=0; i < NumOperands; ++i)
3604  Ops[i] = N->getOperand(i);
3605  for (unsigned i = NumOperands; i != NumConcat; ++i)
3606  Ops[i] = UndefVal;
3607  return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, Ops);
3608  }
3609  } else {
3610  InputWidened = true;
3611  if (WidenVT == TLI.getTypeToTransformTo(*DAG.getContext(), InVT)) {
3612  // The inputs and the result are widen to the same value.
3613  unsigned i;
3614  for (i=1; i < NumOperands; ++i)
3615  if (!N->getOperand(i).isUndef())
3616  break;
3617 
3618  if (i == NumOperands)
3619  // Everything but the first operand is an UNDEF so just return the
3620  // widened first operand.
3621  return GetWidenedVector(N->getOperand(0));
3622 
3623  if (NumOperands == 2) {
3624  // Replace concat of two operands with a shuffle.
3625  SmallVector<int, 16> MaskOps(WidenNumElts, -1);
3626  for (unsigned i = 0; i < NumInElts; ++i) {
3627  MaskOps[i] = i;
3628  MaskOps[i + NumInElts] = i + WidenNumElts;
3629  }
3630  return DAG.getVectorShuffle(WidenVT, dl,
3631  GetWidenedVector(N->getOperand(0)),
3632  GetWidenedVector(N->getOperand(1)),
3633  MaskOps);
3634  }
3635  }
3636  }
3637 
3638  // Fall back to use extracts and build vector.
3639  EVT EltVT = WidenVT.getVectorElementType();
3640  SmallVector<SDValue, 16> Ops(WidenNumElts);
3641  unsigned Idx = 0;
3642  for (unsigned i=0; i < NumOperands; ++i) {
3643  SDValue InOp = N->getOperand(i);
3644  if (InputWidened)
3645  InOp = GetWidenedVector(InOp);
3646  for (unsigned j=0; j < NumInElts; ++j)
3647  Ops[Idx++] = DAG.getNode(
3648  ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
3649  DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3650  }
3651  SDValue UndefVal = DAG.getUNDEF(EltVT);
3652  for (; Idx < WidenNumElts; ++Idx)
3653  Ops[Idx] = UndefVal;
3654  return DAG.getBuildVector(WidenVT, dl, Ops);
3655 }
3656 
3657 SDValue DAGTypeLegalizer::WidenVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
3658  EVT VT = N->getValueType(0);
3659  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3660  unsigned WidenNumElts = WidenVT.getVectorNumElements();
3661  SDValue InOp = N->getOperand(0);
3662  SDValue Idx = N->getOperand(1);
3663  SDLoc dl(N);
3664 
3665  if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
3666  InOp = GetWidenedVector(InOp);
3667 
3668  EVT InVT = InOp.getValueType();
3669 
3670  // Check if we can just return the input vector after widening.
3671  uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
3672  if (IdxVal == 0 && InVT == WidenVT)
3673  return InOp;
3674 
3675  // Check if we can extract from the vector.
3676  unsigned InNumElts = InVT.getVectorNumElements();
3677  if (IdxVal % WidenNumElts == 0 && IdxVal + WidenNumElts < InNumElts)
3678  return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, InOp, Idx);
3679 
3680  // We could try widening the input to the right length but for now, extract
3681  // the original elements, fill the rest with undefs and build a vector.
3682  SmallVector<SDValue, 16> Ops(WidenNumElts);
3683  EVT EltVT = VT.getVectorElementType();
3684  unsigned NumElts = VT.getVectorNumElements();
3685  unsigned i;
3686  for (i=0; i < NumElts; ++i)
3687  Ops[i] =
3688  DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
3689  DAG.getConstant(IdxVal + i, dl,
3690  TLI.getVectorIdxTy(DAG.getDataLayout())));
3691 
3692  SDValue UndefVal = DAG.getUNDEF(EltVT);
3693  for (; i < WidenNumElts; ++i)
3694  Ops[i] = UndefVal;
3695  return DAG.getBuildVector(WidenVT, dl, Ops);
3696 }
3697 
3698 SDValue DAGTypeLegalizer::WidenVecRes_INSERT_VECTOR_ELT(SDNode *N) {
3699  SDValue InOp = GetWidenedVector(N->getOperand(0));
3700  return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N),
3701  InOp.getValueType(), InOp,
3702  N->getOperand(1), N->getOperand(2));
3703 }
3704 
3705 SDValue DAGTypeLegalizer::WidenVecRes_LOAD(SDNode *N) {
3706  LoadSDNode *LD = cast<LoadSDNode>(N);
3708 
3709  SDValue Result;
3710  SmallVector<SDValue, 16> LdChain; // Chain for the series of load
3711  if (ExtType != ISD::NON_EXTLOAD)
3712  Result = GenWidenVectorExtLoads(LdChain, LD, ExtType);
3713  else
3714  Result = GenWidenVectorLoads(LdChain, LD);
3715 
3716  // If we generate a single load, we can use that for the chain. Otherwise,
3717  // build a factor node to remember the multiple loads are independent and
3718  // chain to that.
3719  SDValue NewChain;
3720  if (LdChain.size() == 1)
3721  NewChain = LdChain[0];
3722  else
3723  NewChain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other, LdChain);
3724 
3725  // Modified the chain - switch anything that used the old chain to use
3726  // the new one.
3727  ReplaceValueWith(SDValue(N, 1), NewChain);
3728 
3729  return Result;
3730 }
3731 
3732 SDValue DAGTypeLegalizer::WidenVecRes_MLOAD(MaskedLoadSDNode *N) {
3733 
3734  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),N->getValueType(0));
3735  SDValue Mask = N->getMask();
3736  EVT MaskVT = Mask.getValueType();
3737  SDValue PassThru = GetWidenedVector(N->getPassThru());
3739  SDLoc dl(N);
3740 
3741  // The mask should be widened as well
3742  EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(),
3743  MaskVT.getVectorElementType(),
3744  WidenVT.getVectorNumElements());
3745  Mask = ModifyToType(Mask, WideMaskVT, true);
3746 
3747  SDValue Res = DAG.getMaskedLoad(WidenVT, dl, N->getChain(), N->getBasePtr(),
3748  Mask, PassThru, N->getMemoryVT(),
3749  N->getMemOperand(), ExtType,
3750  N->isExpandingLoad());
3751  // Legalize the chain result - switch anything that used the old chain to
3752  // use the new one.
3753  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
3754  return Res;
3755 }
3756 
3757 SDValue DAGTypeLegalizer::WidenVecRes_MGATHER(MaskedGatherSDNode *N) {
3758 
3759  EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3760  SDValue Mask = N->getMask();
3761  EVT MaskVT = Mask.getValueType();
3762  SDValue PassThru = GetWidenedVector(N->getPassThru());
3763  SDValue Scale = N->getScale();
3764  unsigned NumElts = WideVT.getVectorNumElements();
3765  SDLoc dl(N);
3766 
3767  // The mask should be widened as well
3768  EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(),
3769  MaskVT.getVectorElementType(),
3770  WideVT.getVectorNumElements());
3771  Mask = ModifyToType(Mask, WideMaskVT, true);
3772 
3773  // Widen the Index operand
3774  SDValue Index = N->getIndex();
3775  EVT WideIndexVT = EVT::getVectorVT(*DAG.getContext(),
3776  Index.getValueType().getScalarType(),
3777  NumElts);
3778  Index = ModifyToType(Index, WideIndexVT);
3779  SDValue Ops[] = { N->getChain(), PassThru, Mask, N->getBasePtr(), Index,
3780  Scale };
3781  SDValue Res = DAG.getMaskedGather(DAG.getVTList(WideVT, MVT::Other),
3782  N->getMemoryVT(), dl, Ops,
3783  N->getMemOperand(), N->getIndexType());
3784 
3785  // Legalize the chain result - switch anything that used the old chain to
3786  // use the new one.
3787  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
3788  return Res;
3789 }
3790 
3791 SDValue DAGTypeLegalizer::WidenVecRes_SCALAR_TO_VECTOR(SDNode *N) {
3792  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3793  return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N),
3794  WidenVT, N->getOperand(0));
3795 }
3796 
3797 // Return true if this is a node that could have two SETCCs as operands.
3798 static inline bool isLogicalMaskOp(unsigned Opcode) {
3799  switch (Opcode) {
3800  case ISD::AND:
3801  case ISD::OR:
3802  case ISD::XOR:
3803  return true;
3804  }
3805  return false;
3806 }
3807 
3808 // This is used just for the assert in convertMask(). Check that this either
3809 // a SETCC or a previously handled SETCC by convertMask().
3810 #ifndef NDEBUG
3811 static inline bool isSETCCorConvertedSETCC(SDValue N) {
3812  if (N.getOpcode() == ISD::EXTRACT_SUBVECTOR)
3813  N = N.getOperand(0);
3814  else if (N.getOpcode() == ISD::CONCAT_VECTORS) {
3815  for (unsigned i = 1; i < N->getNumOperands(); ++i)
3816  if (!N->getOperand(i)->isUndef())
3817  return false;
3818  N = N.getOperand(0);
3819  }
3820 
3821  if (N.getOpcode() == ISD::TRUNCATE)
3822  N = N.getOperand(0);
3823  else if (N.getOpcode() == ISD::SIGN_EXTEND)
3824  N = N.getOperand(0);
3825 
3826  if (isLogicalMaskOp(N.getOpcode()))
3827  return isSETCCorConvertedSETCC(N.getOperand(0)) &&
3829 
3830  return (N.getOpcode() == ISD::SETCC ||
3832 }
3833 #endif
3834 
3835 // Return a mask of vector type MaskVT to replace InMask. Also adjust MaskVT
3836 // to ToMaskVT if needed with vector extension or truncation.
3837 SDValue DAGTypeLegalizer::convertMask(SDValue InMask, EVT MaskVT,
3838  EVT ToMaskVT) {
3839  // Currently a SETCC or a AND/OR/XOR with two SETCCs are handled.
3840  // FIXME: This code seems to be too restrictive, we might consider
3841  // generalizing it or dropping it.
3842  assert(isSETCCorConvertedSETCC(InMask) && "Unexpected mask argument.");
3843 
3844  // Make a new Mask node, with a legal result VT.
3846  for (unsigned i = 0, e = InMask->getNumOperands(); i < e; ++i)
3847  Ops.push_back(InMask->getOperand(i));
3848  SDValue Mask = DAG.getNode(InMask->getOpcode(), SDLoc(InMask), MaskVT, Ops);
3849 
3850  // If MaskVT has smaller or bigger elements than ToMaskVT, a vector sign
3851  // extend or truncate is needed.
3852  LLVMContext &Ctx = *DAG.getContext();
3853  unsigned MaskScalarBits = MaskVT.getScalarSizeInBits();
3854  unsigned ToMaskScalBits = ToMaskVT.getScalarSizeInBits();
3855  if (MaskScalarBits < ToMaskScalBits) {
3856  EVT ExtVT = EVT::getVectorVT(Ctx, ToMaskVT.getVectorElementType(),
3857  MaskVT.getVectorNumElements());
3858  Mask = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(Mask), ExtVT, Mask);
3859  } else if (MaskScalarBits > ToMaskScalBits) {
3860  EVT TruncVT = EVT::getVectorVT(Ctx, ToMaskVT.getVectorElementType(),
3861  MaskVT.getVectorNumElements());
3862  Mask = DAG.getNode(ISD::TRUNCATE, SDLoc(Mask), TruncVT, Mask);
3863  }
3864 
3866  ToMaskVT.getScalarSizeInBits() &&
3867  "Mask should have the right element size by now.");
3868 
3869  // Adjust Mask to the right number of elements.
3870  unsigned CurrMaskNumEls = Mask->getValueType(0).getVectorNumElements();
3871  if (CurrMaskNumEls > ToMaskVT.getVectorNumElements()) {
3872  MVT IdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
3873  SDValue ZeroIdx = DAG.getConstant(0, SDLoc(Mask), IdxTy);
3874  Mask = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Mask), ToMaskVT, Mask,
3875  ZeroIdx);
3876  } else if (CurrMaskNumEls < ToMaskVT.getVectorNumElements()) {
3877  unsigned NumSubVecs = (ToMaskVT.getVectorNumElements() / CurrMaskNumEls);
3878  EVT SubVT = Mask->getValueType(0);
3879  SmallVector<SDValue, 16> SubOps(NumSubVecs, DAG.getUNDEF(SubVT));
3880  SubOps[0] = Mask;
3881  Mask = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Mask), ToMaskVT, SubOps);
3882  }
3883 
3884  assert((Mask->getValueType(0) == ToMaskVT) &&
3885  "A mask of ToMaskVT should have been produced by now.");
3886 
3887  return Mask;
3888 }
3889 
3890 // This method tries to handle VSELECT and its mask by legalizing operands
3891 // (which may require widening) and if needed adjusting the mask vector type
3892 // to match that of the VSELECT. Without it, many cases end up with
3893 // scalarization of the SETCC, with many unnecessary instructions.
3894 SDValue DAGTypeLegalizer::WidenVSELECTAndMask(SDNode *N) {
3895  LLVMContext &Ctx = *DAG.getContext();
3896  SDValue Cond = N->getOperand(0);
3897 
3898  if (N->getOpcode() != ISD::VSELECT)
3899  return SDValue();
3900 
3901  if (Cond->getOpcode() != ISD::SETCC && !isLogicalMaskOp(Cond->getOpcode()))
3902  return SDValue();
3903 
3904  // If this is a splitted VSELECT that was previously already handled, do
3905  // nothing.
3906  EVT CondVT = Cond->getValueType(0);
3907  if (CondVT.getScalarSizeInBits() != 1)
3908  return SDValue();
3909 
3910  EVT VSelVT = N->getValueType(0);
3911  // Only handle vector types which are a power of 2.
3912  if (!isPowerOf2_64(VSelVT.getSizeInBits()))
3913  return SDValue();
3914 
3915  // Don't touch if this will be scalarized.
3916  EVT FinalVT = VSelVT;
3917  while (getTypeAction(FinalVT) == TargetLowering::TypeSplitVector)
3918  FinalVT = FinalVT.getHalfNumVectorElementsVT(Ctx);
3919 
3920  if (FinalVT.getVectorNumElements() == 1)
3921  return SDValue();
3922 
3923  // If there is support for an i1 vector mask, don't touch.
3924  if (Cond.getOpcode() == ISD::SETCC) {
3925  EVT SetCCOpVT = Cond->getOperand(0).getValueType();
3926  while (TLI.getTypeAction(Ctx, SetCCOpVT) != TargetLowering::TypeLegal)
3927  SetCCOpVT = TLI.getTypeToTransformTo(Ctx, SetCCOpVT);
3928  EVT SetCCResVT = getSetCCResultType(SetCCOpVT);
3929  if (SetCCResVT.getScalarSizeInBits() == 1)
3930  return SDValue();
3931  } else if (CondVT.getScalarType() == MVT::i1) {
3932  // If there is support for an i1 vector mask (or only scalar i1 conditions),
3933  // don't touch.
3934  while (TLI.getTypeAction(Ctx, CondVT) != TargetLowering::TypeLegal)
3935  CondVT = TLI.getTypeToTransformTo(Ctx, CondVT);
3936 
3937  if (CondVT.getScalarType() == MVT::i1)
3938  return SDValue();
3939  }
3940 
3941  // Get the VT and operands for VSELECT, and widen if needed.
3942  SDValue VSelOp1 = N->getOperand(1);
3943  SDValue VSelOp2 = N->getOperand(2);
3944  if (getTypeAction(VSelVT) == TargetLowering::TypeWidenVector) {
3945  VSelVT = TLI.getTypeToTransformTo(Ctx, VSelVT);
3946  VSelOp1 = GetWidenedVector(VSelOp1);
3947  VSelOp2 = GetWidenedVector(VSelOp2);
3948  }
3949 
3950  // The mask of the VSELECT should have integer elements.
3951  EVT ToMaskVT = VSelVT;
3952  if (!ToMaskVT.getScalarType().isInteger())
3953  ToMaskVT = ToMaskVT.changeVectorElementTypeToInteger();
3954 
3955  SDValue Mask;
3956  if (Cond->getOpcode() == ISD::SETCC) {
3957  EVT MaskVT = getSetCCResultType(Cond.getOperand(0).getValueType());
3958  Mask = convertMask(Cond, MaskVT, ToMaskVT);
3959  } else if (isLogicalMaskOp(Cond->getOpcode()) &&
3960  Cond->getOperand(0).getOpcode() == ISD::SETCC &&
3961  Cond->getOperand(1).getOpcode() == ISD::SETCC) {
3962  // Cond is (AND/OR/XOR (SETCC, SETCC))
3963  SDValue SETCC0 = Cond->getOperand(0);
3964  SDValue SETCC1 = Cond->getOperand(1);
3965  EVT VT0 = getSetCCResultType(SETCC0.getOperand(0).getValueType());
3966  EVT VT1 = getSetCCResultType(SETCC1.getOperand(0).getValueType());
3967  unsigned ScalarBits0 = VT0.getScalarSizeInBits();
3968  unsigned ScalarBits1 = VT1.getScalarSizeInBits();
3969  unsigned ScalarBits_ToMask = ToMaskVT.getScalarSizeInBits();
3970  EVT MaskVT;
3971  // If the two SETCCs have different VTs, either extend/truncate one of
3972  // them to the other "towards" ToMaskVT, or truncate one and extend the
3973  // other to ToMaskVT.
3974  if (ScalarBits0 != ScalarBits1) {
3975  EVT NarrowVT = ((ScalarBits0 < ScalarBits1) ? VT0 : VT1);
3976  EVT WideVT = ((NarrowVT == VT0) ? VT1 : VT0);
3977  if (ScalarBits_ToMask >= WideVT.getScalarSizeInBits())
3978  MaskVT = WideVT;
3979  else if (ScalarBits_ToMask <= NarrowVT.getScalarSizeInBits())
3980  MaskVT = NarrowVT;
3981  else
3982  MaskVT = ToMaskVT;
3983  } else
3984  // If the two SETCCs have the same VT, don't change it.
3985  MaskVT = VT0;
3986 
3987  // Make new SETCCs and logical nodes.
3988  SETCC0 = convertMask(SETCC0, VT0, MaskVT);
3989  SETCC1 = convertMask(SETCC1, VT1, MaskVT);
3990  Cond = DAG.getNode(Cond->getOpcode(), SDLoc(Cond), MaskVT, SETCC0, SETCC1);
3991 
3992  // Convert the logical op for VSELECT if needed.
3993  Mask = convertMask(Cond, MaskVT, ToMaskVT);
3994  } else
3995  return SDValue();
3996 
3997  return DAG.getNode(ISD::VSELECT, SDLoc(N), VSelVT, Mask, VSelOp1, VSelOp2);
3998 }
3999 
4000 SDValue DAGTypeLegalizer::WidenVecRes_SELECT(SDNode *N) {
4001  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4002  unsigned WidenNumElts = WidenVT.getVectorNumElements();
4003 
4004  SDValue Cond1 = N->getOperand(0);
4005  EVT CondVT = Cond1.getValueType();
4006  if (CondVT.isVector()) {
4007  if (SDValue Res = WidenVSELECTAndMask(N))
4008  return Res;
4009 
4010  EVT CondEltVT = CondVT.getVectorElementType();
4011  EVT CondWidenVT = EVT::getVectorVT(*DAG.getContext(),
4012  CondEltVT, WidenNumElts);
4013  if (getTypeAction(CondVT) == TargetLowering::TypeWidenVector)
4014  Cond1 = GetWidenedVector(Cond1);
4015 
4016  // If we have to split the condition there is no point in widening the
4017  // select. This would result in an cycle of widening the select ->
4018  // widening the condition operand -> splitting the condition operand ->
4019  // splitting the select -> widening the select. Instead split this select
4020  // further and widen the resulting type.
4021  if (getTypeAction(CondVT) == TargetLowering::TypeSplitVector) {
4022  SDValue SplitSelect = SplitVecOp_VSELECT(N, 0);
4023  SDValue Res = ModifyToType(SplitSelect, WidenVT);
4024  return Res;
4025  }
4026 
4027  if (Cond1.getValueType() != CondWidenVT)
4028  Cond1 = ModifyToType(Cond1, CondWidenVT);
4029  }
4030 
4031  SDValue InOp1 = GetWidenedVector(N->getOperand(1));
4032  SDValue InOp2 = GetWidenedVector(N->getOperand(2));
4033  assert(InOp1.getValueType() == WidenVT && InOp2.getValueType() == WidenVT);
4034  return DAG.getNode(N->getOpcode(), SDLoc(N),
4035  WidenVT, Cond1, InOp1, InOp2);
4036 }
4037 
4038 SDValue DAGTypeLegalizer::WidenVecRes_SELECT_CC(SDNode *N) {
4039  SDValue InOp1 = GetWidenedVector(N->getOperand(2));
4040  SDValue InOp2 = GetWidenedVector(N->getOperand(3));
4041  return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
4042  InOp1.getValueType(), N->getOperand(0),
4043  N->getOperand(1), InOp1, InOp2, N->getOperand(4));
4044 }
4045 
4046 SDValue DAGTypeLegalizer::WidenVecRes_UNDEF(SDNode *N) {
4047  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4048  return DAG.getUNDEF(WidenVT);
4049 }
4050 
4051 SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
4052  EVT VT = N->getValueType(0);
4053  SDLoc dl(N);
4054 
4055  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4056  unsigned NumElts = VT.getVectorNumElements();
4057  unsigned WidenNumElts = WidenVT.getVectorNumElements();
4058 
4059  SDValue InOp1 = GetWidenedVector(N->getOperand(0));
4060  SDValue InOp2 = GetWidenedVector(N->getOperand(1));
4061 
4062  // Adjust mask based on new input vector length.
4063  SmallVector<int, 16> NewMask;
4064  for (unsigned i = 0; i != NumElts; ++i) {
4065  int Idx = N->getMaskElt(i);
4066  if (Idx < (int)NumElts)
4067  NewMask.push_back(Idx);
4068  else
4069  NewMask.push_back(Idx - NumElts + WidenNumElts);
4070  }
4071  for (unsigned i = NumElts; i != WidenNumElts; ++i)
4072  NewMask.push_back(-1);
4073  return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, NewMask);
4074 }
4075 
4076 SDValue DAGTypeLegalizer::WidenVecRes_SETCC(SDNode *N) {
4077  assert(N->getValueType(0).isVector() &&
4078  N->getOperand(0).getValueType().isVector() &&
4079  "Operands must be vectors");
4080  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4081  unsigned WidenNumElts = WidenVT.getVectorNumElements();
4082 
4083  SDValue InOp1 = N->getOperand(0);
4084  EVT InVT = InOp1.getValueType();
4085  assert(InVT.isVector() && "can not widen non-vector type");
4086  EVT WidenInVT = EVT::getVectorVT(*DAG.getContext(),
4087  InVT.getVectorElementType(), WidenNumElts);
4088 
4089  // The input and output types often differ here, and it could be that while
4090  // we'd prefer to widen the result type, the input operands have been split.
4091  // In this case, we also need to split the result of this node as well.
4092  if (getTypeAction(InVT) == TargetLowering::TypeSplitVector) {
4093  SDValue SplitVSetCC = SplitVecOp_VSETCC(N);
4094  SDValue Res = ModifyToType(SplitVSetCC, WidenVT);
4095  return Res;
4096  }
4097 
4098  // If the inputs also widen, handle them directly. Otherwise widen by hand.
4099  SDValue InOp2 = N->getOperand(1);
4100  if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
4101  InOp1 = GetWidenedVector(InOp1);
4102  InOp2 = GetWidenedVector(InOp2);
4103  } else {
4104  InOp1 = DAG.WidenVector(InOp1, SDLoc(N));
4105  InOp2 = DAG.WidenVector(InOp2, SDLoc(N));
4106  }
4107 
4108  // Assume that the input and output will be widen appropriately. If not,
4109  // we will have to unroll it at some point.
4110  assert(InOp1.getValueType() == WidenInVT &&
4111  InOp2.getValueType() == WidenInVT &&
4112  "Input not widened to expected type!");
4113  (void)WidenInVT;
4114  return DAG.getNode(ISD::SETCC, SDLoc(N),
4115  WidenVT, InOp1, InOp2, N->getOperand(2));
4116 }
4117 
4118 
4119 //===----------------------------------------------------------------------===//
4120 // Widen Vector Operand
4121 //===----------------------------------------------------------------------===//
4122 bool DAGTypeLegalizer::WidenVectorOperand(SDNode *N, unsigned OpNo) {
4123  LLVM_DEBUG(dbgs() << "Widen node operand " << OpNo << ": "; N->dump(&DAG);
4124  dbgs() << "\n");
4125  SDValue Res = SDValue();
4126 
4127  // See if the target wants to custom widen this node.
4128  if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
4129  return false;
4130 
4131  switch (N->getOpcode()) {
4132  default:
4133 #ifndef NDEBUG
4134  dbgs() << "WidenVectorOperand op #" << OpNo << ": ";
4135  N->dump(&DAG);
4136  dbgs() << "\n";
4137 #endif
4138  llvm_unreachable("Do not know how to widen this operator's operand!");
4139 
4140  case ISD::BITCAST: Res = WidenVecOp_BITCAST(N); break;
4141  case ISD::CONCAT_VECTORS: Res = WidenVecOp_CONCAT_VECTORS(N); break;
4142  case ISD::EXTRACT_SUBVECTOR: Res = WidenVecOp_EXTRACT_SUBVECTOR(N); break;
4143  case ISD::EXTRACT_VECTOR_ELT: Res = WidenVecOp_EXTRACT_VECTOR_ELT(N); break;
4144  case ISD::STORE: Res = WidenVecOp_STORE(N); break;
4145  case ISD::MSTORE: Res = WidenVecOp_MSTORE(N, OpNo); break;
4146  case ISD::MGATHER: Res = WidenVecOp_MGATHER(N, OpNo); break;
4147  case ISD::MSCATTER: Res = WidenVecOp_MSCATTER(N, OpNo); break;
4148  case ISD::SETCC: Res = WidenVecOp_SETCC(N); break;
4149  case ISD::VSELECT: Res = WidenVecOp_VSELECT(N); break;
4150  case ISD::FCOPYSIGN: Res = WidenVecOp_FCOPYSIGN(N); break;
4151 
4152  case ISD::ANY_EXTEND:
4153  case ISD::SIGN_EXTEND:
4154  case ISD::ZERO_EXTEND:
4155  Res = WidenVecOp_EXTEND(N);
4156  break;
4157 
4158  case ISD::FP_EXTEND:
4159  case ISD::STRICT_FP_EXTEND:
4160  case ISD::FP_TO_SINT:
4162  case ISD::FP_TO_UINT:
4164  case ISD::SINT_TO_FP:
4165  case ISD::UINT_TO_FP:
4166  case ISD::TRUNCATE:
4167  Res = WidenVecOp_Convert(N);
4168  break;
4169 
4170  case ISD::VECREDUCE_FADD:
4171  case ISD::VECREDUCE_FMUL:
4172  case ISD::VECREDUCE_ADD:
4173  case ISD::VECREDUCE_MUL:
4174  case ISD::VECREDUCE_AND:
4175  case ISD::VECREDUCE_OR:
4176  case ISD::VECREDUCE_XOR:
4177  case ISD::VECREDUCE_SMAX:
4178  case ISD::VECREDUCE_SMIN:
4179  case ISD::VECREDUCE_UMAX:
4180  case ISD::VECREDUCE_UMIN:
4181  case ISD::VECREDUCE_FMAX:
4182  case ISD::VECREDUCE_FMIN:
4183  Res = WidenVecOp_VECREDUCE(N);
4184  break;
4185  }
4186 
4187  // If Res is null, the sub-method took care of registering the result.
4188  if (!Res.getNode()) return false;
4189 
4190  // If the result is N, the sub-method updated N in place. Tell the legalizer
4191  // core about this.
4192  if (Res.getNode() == N)
4193  return true;
4194 
4195 
4196  if (N->isStrictFPOpcode())
4197  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 2 &&
4198  "Invalid operand expansion");
4199  else
4200  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
4201  "Invalid operand expansion");
4202 
4203  ReplaceValueWith(SDValue(N, 0), Res);
4204  return false;
4205 }
4206 
4207 SDValue DAGTypeLegalizer::WidenVecOp_EXTEND(SDNode *N) {
4208  SDLoc DL(N);
4209  EVT VT = N->getValueType(0);
4210 
4211  SDValue InOp = N->getOperand(0);
4212  assert(getTypeAction(InOp.getValueType()) ==
4214  "Unexpected type action");
4215  InOp = GetWidenedVector(InOp);
4218  "Input wasn't widened!");
4219 
4220  // We may need to further widen the operand until it has the same total
4221  // vector size as the result.
4222  EVT InVT = InOp.getValueType();
4223  if (InVT.getSizeInBits() != VT.getSizeInBits()) {
4224  EVT InEltVT = InVT.getVectorElementType();
4225  for (int i = MVT::FIRST_VECTOR_VALUETYPE, e = MVT::LAST_VECTOR_VALUETYPE; i < e; ++i) {
4226  EVT FixedVT = (MVT::SimpleValueType)i;
4227  EVT FixedEltVT = FixedVT.getVectorElementType();
4228  if (TLI.isTypeLegal(FixedVT) &&
4229  FixedVT.getSizeInBits() == VT.getSizeInBits() &&
4230  FixedEltVT == InEltVT) {
4231  assert(FixedVT.getVectorNumElements() >= VT.getVectorNumElements() &&
4232  "Not enough elements in the fixed type for the operand!");
4233  assert(FixedVT.getVectorNumElements() != InVT.getVectorNumElements() &&
4234  "We can't have the same type as we started with!");
4235  if (FixedVT.getVectorNumElements() > InVT.getVectorNumElements())
4236  InOp = DAG.getNode(
4237  ISD::INSERT_SUBVECTOR, DL, FixedVT, DAG.getUNDEF(FixedVT), InOp,
4238  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
4239  else
4240  InOp = DAG.getNode(
4241  ISD::EXTRACT_SUBVECTOR, DL, FixedVT, InOp,
4242  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
4243  break;
4244  }
4245  }
4246  InVT = InOp.getValueType();
4247  if (InVT.getSizeInBits() != VT.getSizeInBits())
4248  // We couldn't find a legal vector type that was a widening of the input
4249  // and could be extended in-register to the result type, so we have to
4250  // scalarize.
4251  return WidenVecOp_Convert(N);
4252  }
4253 
4254  // Use special DAG nodes to represent the operation of extending the
4255  // low lanes.
4256  switch (N->getOpcode()) {
4257  default:
4258  llvm_unreachable("Extend legalization on extend operation!");
4259  case ISD::ANY_EXTEND:
4260  return DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, InOp);
4261  case ISD::SIGN_EXTEND:
4262  return DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, InOp);
4263  case ISD::ZERO_EXTEND:
4264  return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, InOp);
4265  }
4266 }
4267 
4268 SDValue DAGTypeLegalizer::WidenVecOp_FCOPYSIGN(SDNode *N) {
4269  // The result (and first input) is legal, but the second input is illegal.
4270  // We can't do much to fix that, so just unroll and let the extracts off of
4271  // the second input be widened as needed later.
4272  return DAG.UnrollVectorOp(N);
4273 }
4274 
4275 SDValue DAGTypeLegalizer::WidenVecOp_Convert(SDNode *N) {
4276  // Since the result is legal and the input is illegal.
4277  EVT VT = N->getValueType(0);
4278  EVT EltVT = VT.getVectorElementType();
4279  SDLoc dl(N);
4280  unsigned NumElts = VT.getVectorNumElements();
4281  SDValue InOp = N->getOperand(N->isStrictFPOpcode() ? 1 : 0);
4282  assert(getTypeAction(InOp.getValueType()) ==
4284  "Unexpected type action");
4285  InOp = GetWidenedVector(InOp);
4286  EVT InVT = InOp.getValueType();
4287  unsigned Opcode = N->getOpcode();
4288 
4289  // See if a widened result type would be legal, if so widen the node.
4290  // FIXME: This isn't safe for StrictFP. Other optimization here is needed.
4291  EVT WideVT = EVT::getVectorVT(*DAG.getContext(), EltVT,
4292  InVT.getVectorNumElements());
4293  if (TLI.isTypeLegal(WideVT) && !N->isStrictFPOpcode()) {
4294  SDValue Res;
4295  if (N->isStrictFPOpcode()) {
4296  Res = DAG.getNode(Opcode, dl, { WideVT, MVT::Other },
4297  { N->getOperand(0), InOp });
4298  // Legalize the chain result - switch anything that used the old chain to
4299  // use the new one.
4300  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
4301  } else
4302  Res = DAG.getNode(Opcode, dl, WideVT, InOp);
4303  return DAG.getNode(
4304  ISD::EXTRACT_SUBVECTOR, dl, VT, Res,
4305  DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4306  }
4307 
4308  EVT InEltVT = InVT.getVectorElementType();
4309 
4310  // Unroll the convert into some scalar code and create a nasty build vector.
4311  SmallVector<SDValue, 16> Ops(NumElts);
4312  if (N->isStrictFPOpcode()) {
4313  SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
4314  SmallVector<SDValue, 32> OpChains;
4315  for (unsigned i=0; i < NumElts; ++i) {
4316  NewOps[1] = DAG.getNode(
4317  ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
4318  DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4319  Ops[i] = DAG.getNode(Opcode, dl, { EltVT, MVT::Other }, NewOps);
4320  OpChains.push_back(Ops[i].getValue(1));
4321  }
4322  SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OpChains);
4323  ReplaceValueWith(SDValue(N, 1), NewChain);
4324  } else {
4325  for (unsigned i = 0; i < NumElts; ++i)
4326  Ops[i] = DAG.getNode(
4327  Opcode, dl, EltVT,
4328  DAG.getNode(
4329  ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
4330  DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
4331  }
4332 
4333  return DAG.getBuildVector(VT, dl, Ops);
4334 }
4335 
4336 SDValue DAGTypeLegalizer::WidenVecOp_BITCAST(SDNode *N) {
4337  EVT VT = N->getValueType(0);
4338  SDValue InOp = GetWidenedVector(N->getOperand(0));
4339  EVT InWidenVT = InOp.getValueType();
4340  SDLoc dl(N);
4341 
4342  // Check if we can convert between two legal vector types and extract.
4343  unsigned InWidenSize = InWidenVT.getSizeInBits();
4344  unsigned Size = VT.getSizeInBits();
4345  // x86mmx is not an acceptable vector element type, so don't try.
4346  if (InWidenSize % Size == 0 && !VT.isVector() && VT != MVT::x86mmx) {
4347  unsigned NewNumElts = InWidenSize / Size;
4348  EVT NewVT = EVT::getVectorVT(*DAG.getContext(), VT, NewNumElts);
4349  if (TLI.isTypeLegal(NewVT)) {
4350  SDValue BitOp = DAG.getNode(ISD::BITCAST, dl, NewVT, InOp);
4351  return DAG.getNode(
4352  ISD::EXTRACT_VECTOR_ELT, dl, VT, BitOp,
4353  DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4354  }
4355  }
4356 
4357  // Handle a case like bitcast v12i8 -> v3i32. Normally that would get widened
4358  // to v16i8 -> v4i32, but for a target where v3i32 is legal but v12i8 is not,
4359  // we end up here. Handling the case here with EXTRACT_SUBVECTOR avoids
4360  // having to copy via memory.
4361  if (VT.isVector()) {
4362  EVT EltVT = VT.getVectorElementType();
4363  unsigned EltSize = EltVT.getSizeInBits();
4364  if (InWidenSize % EltSize == 0) {
4365  unsigned NewNumElts = InWidenSize / EltSize;
4366  EVT NewVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NewNumElts);
4367  if (TLI.isTypeLegal(NewVT)) {
4368  SDValue BitOp = DAG.getNode(ISD::BITCAST, dl, NewVT, InOp);
4369  return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, BitOp,
4370  DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4371  }
4372  }
4373  }
4374 
4375  return CreateStackStoreLoad(InOp, VT);
4376 }
4377 
4378 SDValue DAGTypeLegalizer::WidenVecOp_CONCAT_VECTORS(SDNode *N) {
4379  EVT VT = N->getValueType(0);
4380  EVT EltVT = VT.getVectorElementType();
4381  EVT InVT = N->getOperand(0).getValueType();
4382  SDLoc dl(N);
4383 
4384  // If the widen width for this operand is the same as the width of the concat
4385  // and all but the first operand is undef, just use the widened operand.
4386  unsigned NumOperands = N->getNumOperands();
4387  if (VT == TLI.getTypeToTransformTo(*DAG.getContext(), InVT)) {
4388  unsigned i;
4389  for (i = 1; i < NumOperands; ++i)
4390  if (!N->getOperand(i).isUndef())
4391  break;
4392 
4393  if (i == NumOperands)
4394  return GetWidenedVector(N->getOperand(0));
4395  }
4396 
4397  // Otherwise, fall back to a nasty build vector.
4398  unsigned NumElts = VT.getVectorNumElements();
4399  SmallVector<SDValue, 16> Ops(NumElts);
4400 
4401  unsigned NumInElts = InVT.getVectorNumElements();
4402 
4403  unsigned Idx = 0;
4404  for (unsigned i=0; i < NumOperands; ++i) {
4405  SDValue InOp = N->getOperand(i);
4406  assert(getTypeAction(InOp.getValueType()) ==
4408  "Unexpected type action");
4409  InOp = GetWidenedVector(InOp);
4410  for (unsigned j=0; j < NumInElts; ++j)
4411  Ops[Idx++] = DAG.getNode(
4412  ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
4413  DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4414  }
4415  return DAG.getBuildVector(VT, dl, Ops);
4416 }
4417 
4418 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
4419  SDValue InOp = GetWidenedVector(N->getOperand(0));
4420  return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N),
4421  N->getValueType(0), InOp, N->getOperand(1));
4422 }
4423 
4424 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
4425  SDValue InOp = GetWidenedVector(N->getOperand(0));
4426  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
4427  N->getValueType(0), InOp, N->getOperand(1));
4428 }
4429 
4430 SDValue DAGTypeLegalizer::WidenVecOp_STORE(SDNode *N) {
4431  // We have to widen the value, but we want only to store the original
4432  // vector type.
4433  StoreSDNode *ST = cast<StoreSDNode>(N);
4434 
4435  if (!ST->getMemoryVT().getScalarType().isByteSized())
4436  return TLI.scalarizeVectorStore(ST, DAG);
4437 
4438  SmallVector<SDValue, 16> StChain;
4439  if (ST->isTruncatingStore())
4440  GenWidenVectorTruncStores(StChain, ST);
4441  else
4442  GenWidenVectorStores(StChain, ST);
4443 
4444  if (StChain.size() == 1)
4445  return StChain[0];
4446  else
4447  return DAG.getNode(ISD::TokenFactor, SDLoc(ST), MVT::Other, StChain);
4448 }
4449 
4450 SDValue DAGTypeLegalizer::WidenVecOp_MSTORE(SDNode *N, unsigned OpNo) {
4451  assert((OpNo == 1 || OpNo == 3) &&
4452  "Can widen only data or mask operand of mstore");
4453  MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
4454  SDValue Mask = MST->getMask();
4455  EVT MaskVT = Mask.getValueType();
4456  SDValue StVal = MST->getValue();
4457  SDLoc dl(N);
4458 
4459  if (OpNo == 1) {
4460  // Widen the value.
4461  StVal = GetWidenedVector(StVal);
4462 
4463  // The mask should be widened as well.
4464  EVT WideVT = StVal.getValueType();
4465  EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(),
4466  MaskVT.getVectorElementType(),
4467  WideVT.getVectorNumElements());
4468  Mask = ModifyToType(Mask, WideMaskVT, true);
4469  } else {
4470  // Widen the mask.
4471  EVT WideMaskVT = TLI.getTypeToTransformTo(*DAG.getContext(), MaskVT);
4472  Mask = ModifyToType(Mask, WideMaskVT, true);
4473 
4474  EVT ValueVT = StVal.getValueType();
4475  EVT WideVT = EVT::getVectorVT(*DAG.getContext(),
4476  ValueVT.getVectorElementType(),
4477  WideMaskVT.getVectorNumElements());
4478  StVal = ModifyToType(StVal, WideVT);
4479  }
4480 
4481  assert(Mask.getValueType().getVectorNumElements() ==
4482  StVal.getValueType().getVectorNumElements() &&
4483  "Mask and data vectors should have the same number of elements");
4484  return DAG.getMaskedStore(MST->getChain(), dl, StVal, MST->getBasePtr(),
4485  Mask, MST->getMemoryVT(), MST->getMemOperand(),
4486  false, MST->isCompressingStore());
4487 }
4488 
4489 SDValue DAGTypeLegalizer::WidenVecOp_MGATHER(SDNode *N, unsigned OpNo) {
4490  assert(OpNo == 4 && "Can widen only the index of mgather");
4491  auto *MG = cast<MaskedGatherSDNode>(N);
4492  SDValue DataOp = MG->getPassThru();
4493  SDValue Mask = MG->getMask();
4494  SDValue Scale = MG->getScale();
4495 
4496  // Just widen the index. It's allowed to have extra elements.
4497  SDValue Index = GetWidenedVector(MG->getIndex());
4498 
4499  SDLoc dl(N);
4500  SDValue Ops[] = {MG->getChain(), DataOp, Mask, MG->getBasePtr(), Index,
4501  Scale};
4502  SDValue Res = DAG.getMaskedGather(MG->getVTList(), MG->getMemoryVT(), dl, Ops,
4503  MG->getMemOperand(), MG->getIndexType());
4504  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
4505  ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
4506  return SDValue();
4507 }
4508 
4509 SDValue DAGTypeLegalizer::WidenVecOp_MSCATTER(SDNode *N, unsigned OpNo) {
4510  MaskedScatterSDNode *MSC = cast<MaskedScatterSDNode>(N);
4511  SDValue DataOp = MSC->getValue();
4512  SDValue Mask = MSC->getMask();
4513  SDValue Index = MSC->getIndex();
4514  SDValue Scale = MSC->getScale();
4515 
4516  if (OpNo == 1) {
4517  DataOp = GetWidenedVector(DataOp);
4518  unsigned NumElts = DataOp.getValueType().getVectorNumElements();
4519 
4520  // Widen index.
4521  EVT IndexVT = Index.getValueType();
4522  EVT WideIndexVT = EVT::getVectorVT(*DAG.getContext(),
4523  IndexVT.getVectorElementType(), NumElts);
4524  Index = ModifyToType(Index, WideIndexVT);
4525 
4526  // The mask should be widened as well.
4527  EVT MaskVT = Mask.getValueType();
4528  EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(),
4529  MaskVT.getVectorElementType(), NumElts);
4530  Mask = ModifyToType(Mask, WideMaskVT, true);
4531  } else if (OpNo == 4) {
4532  // Just widen the index. It's allowed to have extra elements.
4533  Index = GetWidenedVector(Index);
4534  } else
4535  llvm_unreachable("Can't widen this operand of mscatter");
4536 
4537  SDValue Ops[] = {MSC->getChain(), DataOp, Mask, MSC->getBasePtr(), Index,
4538  Scale};
4539  return DAG.getMaskedScatter(DAG.getVTList(MVT::Other),
4540  MSC->getMemoryVT(), SDLoc(N), Ops,
4541  MSC->getMemOperand(), MSC->getIndexType());
4542 }
4543 
4544 SDValue DAGTypeLegalizer::WidenVecOp_SETCC(SDNode *N) {
4545  SDValue InOp0 = GetWidenedVector(N->getOperand(0));
4546  SDValue InOp1 = GetWidenedVector(N->getOperand(1));
4547  SDLoc dl(N);
4548  EVT VT = N->getValueType(0);
4549 
4550  // WARNING: In this code we widen the compare instruction with garbage.
4551  // This garbage may contain denormal floats which may be slow. Is this a real
4552  // concern ? Should we zero the unused lanes if this is a float compare ?
4553 
4554  // Get a new SETCC node to compare the newly widened operands.
4555  // Only some of the compared elements are legal.
4556  EVT SVT = getSetCCResultType(InOp0.getValueType());
4557  // The result type is legal, if its vXi1, keep vXi1 for the new SETCC.
4558  if (VT.getScalarType() == MVT::i1)
4559  SVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
4560  SVT.getVectorNumElements());
4561 
4562  SDValue WideSETCC = DAG.getNode(ISD::SETCC, SDLoc(N),
4563  SVT, InOp0, InOp1, N->getOperand(2));
4564 
4565  // Extract the needed results from the result vector.
4566  EVT ResVT = EVT::getVectorVT(*DAG.getContext(),
4567  SVT.getVectorElementType(),
4568  VT.getVectorNumElements());
4569  SDValue CC = DAG.getNode(
4570  ISD::EXTRACT_SUBVECTOR, dl, ResVT, WideSETCC,
4571  DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4572 
4573  return PromoteTargetBoolean(CC, VT);
4574 }
4575 
4576 SDValue DAGTypeLegalizer::WidenVecOp_VECREDUCE(SDNode *N) {
4577  SDLoc dl(N);
4578  SDValue Op = GetWidenedVector(N->getOperand(0));
4579  EVT OrigVT = N->getOperand(0).getValueType();
4580  EVT WideVT = Op.getValueType();
4581  EVT ElemVT = OrigVT.getVectorElementType();
4582 
4583  SDValue NeutralElem;
4584  switch (N->getOpcode()) {
4585  case ISD::VECREDUCE_ADD:
4586  case ISD::VECREDUCE_OR:
4587  case ISD::VECREDUCE_XOR:
4588  case ISD::VECREDUCE_UMAX:
4589  NeutralElem = DAG.getConstant(0, dl, ElemVT);
4590  break;
4591  case ISD::VECREDUCE_MUL:
4592  NeutralElem = DAG.getConstant(1, dl, ElemVT);
4593  break;
4594  case ISD::VECREDUCE_AND:
4595  case ISD::VECREDUCE_UMIN:
4596  NeutralElem = DAG.getAllOnesConstant(dl, ElemVT);
4597  break;
4598  case ISD::VECREDUCE_SMAX:
4599  NeutralElem = DAG.getConstant(
4600  APInt::getSignedMinValue(ElemVT.getSizeInBits()), dl, ElemVT);
4601  break;
4602  case ISD::VECREDUCE_SMIN:
4603  NeutralElem = DAG.getConstant(
4604  APInt::getSignedMaxValue(ElemVT.getSizeInBits()), dl, ElemVT);
4605  break;
4606  case ISD::VECREDUCE_FADD:
4607  NeutralElem = DAG.getConstantFP(0.0, dl, ElemVT);
4608  break;
4609  case ISD::VECREDUCE_FMUL:
4610  NeutralElem = DAG.getConstantFP(1.0, dl, ElemVT);
4611  break;
4612  case ISD::VECREDUCE_FMAX:
4613  NeutralElem = DAG.getConstantFP(
4614  std::numeric_limits<double>::infinity(), dl, ElemVT);
4615  break;
4616  case ISD::VECREDUCE_FMIN:
4617  NeutralElem = DAG.getConstantFP(
4618  -std::numeric_limits<double>::infinity(), dl, ElemVT);
4619  break;
4620  }
4621 
4622  // Pad the vector with the neutral element.
4623  unsigned OrigElts = OrigVT.getVectorNumElements();
4624  unsigned WideElts = WideVT.getVectorNumElements();
4625  for (unsigned Idx = OrigElts; Idx < WideElts; Idx++)
4626  Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, WideVT, Op, NeutralElem,
4627  DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4628 
4629  return DAG.getNode(N->getOpcode(), dl, N->getValueType(0), Op, N->getFlags());
4630 }
4631 
4632 SDValue DAGTypeLegalizer::WidenVecOp_VSELECT(SDNode *N) {
4633  // This only gets called in the case that the left and right inputs and
4634  // result are of a legal odd vector type, and the condition is illegal i1 of
4635  // the same odd width that needs widening.
4636  EVT VT = N->getValueType(0);
4637  assert(VT.isVector() && !VT.isPow2VectorType() && isTypeLegal(VT));
4638 
4639  SDValue Cond = GetWidenedVector(N->getOperand(0));
4640  SDValue LeftIn = DAG.WidenVector(N->getOperand(1), SDLoc(N));
4641  SDValue RightIn = DAG.WidenVector(N->getOperand(2), SDLoc(N));
4642  SDLoc DL(N);
4643 
4644  SDValue Select = DAG.getNode(N->getOpcode(), DL, LeftIn.getValueType(), Cond,
4645  LeftIn, RightIn);
4646  return DAG.getNode(
4647  ISD::EXTRACT_SUBVECTOR, DL, VT, Select,
4648  DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
4649 }
4650 
4651 //===----------------------------------------------------------------------===//
4652 // Vector Widening Utilities
4653 //===----------------------------------------------------------------------===//
4654 
4655 // Utility function to find the type to chop up a widen vector for load/store
4656 // TLI: Target lowering used to determine legal types.
4657 // Width: Width left need to load/store.
4658 // WidenVT: The widen vector type to load to/store from
4659 // Align: If 0, don't allow use of a wider type
4660 // WidenEx: If Align is not 0, the amount additional we can load/store from.
4661 
4662 static EVT FindMemType(SelectionDAG& DAG, const TargetLowering &TLI,
4663  unsigned Width, EVT WidenVT,
4664  unsigned Align = 0, unsigned WidenEx = 0) {
4665  EVT WidenEltVT = WidenVT.getVectorElementType();
4666  unsigned WidenWidth = WidenVT.getSizeInBits();
4667  unsigned WidenEltWidth = WidenEltVT.getSizeInBits();
4668  unsigned AlignInBits = Align*8;
4669 
4670  // If we have one element to load/store, return it.
4671  EVT RetVT = WidenEltVT;
4672  if (Width == WidenEltWidth)
4673  return RetVT;
4674 
4675  // See if there is larger legal integer than the element type to load/store.
4676  unsigned VT;
4677  for (VT = (unsigned)MVT::LAST_INTEGER_VALUETYPE;
4678  VT >= (unsigned)MVT::FIRST_INTEGER_VALUETYPE; --VT) {
4679  EVT MemVT((MVT::SimpleValueType) VT);
4680  unsigned MemVTWidth = MemVT.getSizeInBits();
4681  if (MemVT.getSizeInBits() <= WidenEltWidth)
4682  break;
4683  auto Action = TLI.getTypeAction(*DAG.getContext(), MemVT);
4684  if ((Action == TargetLowering::TypeLegal ||
4686  (WidenWidth % MemVTWidth) == 0 &&
4687  isPowerOf2_32(WidenWidth / MemVTWidth) &&
4688  (MemVTWidth <= Width ||
4689  (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
4690  if (MemVTWidth == WidenWidth)
4691  return MemVT;
4692  RetVT = MemVT;
4693  break;
4694  }
4695  }
4696 
4697  // See if there is a larger vector type to load/store that has the same vector
4698  // element type and is evenly divisible with the WidenVT.
4699  for (VT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
4700  VT >= (unsigned)MVT::FIRST_VECTOR_VALUETYPE; --VT) {
4701  EVT MemVT = (MVT::SimpleValueType) VT;
4702  unsigned MemVTWidth = MemVT.getSizeInBits();
4703  auto Action = TLI.getTypeAction(*DAG.getContext(), MemVT);
4704  if ((Action == TargetLowering::TypeLegal ||
4706  WidenEltVT == MemVT.getVectorElementType() &&
4707  (WidenWidth % MemVTWidth) == 0 &&
4708  isPowerOf2_32(WidenWidth / MemVTWidth) &&
4709  (MemVTWidth <= Width ||
4710  (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
4711  if (RetVT.getSizeInBits() < MemVTWidth || MemVT == WidenVT)
4712  return MemVT;
4713  }
4714  }
4715 
4716  return RetVT;
4717 }
4718 
4719 // Builds a vector type from scalar loads
4720 // VecTy: Resulting Vector type
4721 // LDOps: Load operators to build a vector type
4722 // [Start,End) the list of loads to use.
4724  SmallVectorImpl<SDValue> &LdOps,
4725  unsigned Start, unsigned End) {
4726  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4727  SDLoc dl(LdOps[Start]);
4728  EVT LdTy = LdOps[Start].getValueType();
4729  unsigned Width = VecTy.getSizeInBits();
4730  unsigned NumElts = Width / LdTy.getSizeInBits();
4731  EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), LdTy, NumElts);
4732 
4733  unsigned Idx = 1;
4734  SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT,LdOps[Start]);
4735 
4736  for (unsigned i = Start + 1; i != End; ++i) {
4737  EVT NewLdTy = LdOps[i].getValueType();
4738  if (NewLdTy != LdTy) {
4739  NumElts = Width / NewLdTy.getSizeInBits();
4740  NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewLdTy, NumElts);
4741  VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, VecOp);
4742  // Readjust position and vector position based on new load type.
4743  Idx = Idx * LdTy.getSizeInBits() / NewLdTy.getSizeInBits();
4744  LdTy = NewLdTy;
4745  }
4746  VecOp = DAG.getNode(
4747  ISD::INSERT_VECTOR_ELT, dl, NewVecVT, VecOp, LdOps[i],
4748  DAG.getConstant(Idx++, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4749  }
4750  return DAG.getNode(ISD::BITCAST, dl, VecTy, VecOp);
4751 }
4752 
4753 SDValue DAGTypeLegalizer::GenWidenVectorLoads(SmallVectorImpl<SDValue> &LdChain,
4754  LoadSDNode *LD) {
4755  // The strategy assumes that we can efficiently load power-of-two widths.
4756  // The routine chops the vector into the largest vector loads with the same
4757  // element type or scalar loads and then recombines it to the widen vector
4758  // type.
4759  EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
4760  unsigned WidenWidth = WidenVT.getSizeInBits();
4761  EVT LdVT = LD->getMemoryVT();
4762  SDLoc dl(LD);
4763  assert(LdVT.isVector() && WidenVT.isVector());
4764  assert(LdVT.getVectorElementType() == WidenVT.getVectorElementType());
4765 
4766  // Load information
4767  SDValue Chain = LD->getChain();
4768  SDValue BasePtr = LD->getBasePtr();
4769  unsigned Align = LD->getAlignment();
4770  MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
4771  AAMDNodes AAInfo = LD->getAAInfo();
4772 
4773  int LdWidth = LdVT.getSizeInBits();
4774  int WidthDiff = WidenWidth - LdWidth;
4775  unsigned LdAlign = (!LD->isSimple()) ? 0 : Align; // Allow wider loads.
4776 
4777  // Find the vector type that can load from.
4778  EVT NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
4779  int NewVTWidth = NewVT.getSizeInBits();
4780  SDValue LdOp = DAG.getLoad(NewVT, dl, Chain, BasePtr, LD->getPointerInfo(),
4781  Align, MMOFlags, AAInfo);
4782  LdChain.push_back(LdOp.getValue(1));
4783 
4784  // Check if we can load the element with one instruction.
4785  if (LdWidth <= NewVTWidth) {
4786  if (!NewVT.isVector()) {
4787  unsigned NumElts = WidenWidth / NewVTWidth;
4788  EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
4789  SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT, LdOp);
4790  return DAG.getNode(ISD::BITCAST, dl, WidenVT, VecOp);
4791  }
4792  if (NewVT == WidenVT)
4793  return LdOp;
4794 
4795  assert(WidenWidth % NewVTWidth == 0);
4796  unsigned NumConcat = WidenWidth / NewVTWidth;
4797  SmallVector<SDValue, 16> ConcatOps(NumConcat);
4798  SDValue UndefVal = DAG.getUNDEF(NewVT);
4799  ConcatOps[0] = LdOp;
4800  for (unsigned i = 1; i != NumConcat; ++i)
4801  ConcatOps[i] = UndefVal;
4802  return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, ConcatOps);
4803  }
4804 
4805  // Load vector by using multiple loads from largest vector to scalar.
4807  LdOps.push_back(LdOp);
4808 
4809  LdWidth -= NewVTWidth;
4810  unsigned Offset = 0;
4811 
4812  while (LdWidth > 0) {
4813  unsigned Increment = NewVTWidth / 8;
4814  Offset += Increment;
4815  BasePtr = DAG.getObjectPtrOffset(dl, BasePtr, Increment);
4816 
4817  SDValue L;
4818  if (LdWidth < NewVTWidth) {
4819  // The current type we are using is too large. Find a better size.
4820  NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
4821  NewVTWidth = NewVT.getSizeInBits();
4822  L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
4823  LD->getPointerInfo</