LLVM  10.0.0
HexagonISelLoweringHVX.cpp
Go to the documentation of this file.
1 //===-- HexagonISelLoweringHVX.cpp --- Lowering HVX operations ------------===//
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 #include "HexagonISelLowering.h"
10 #include "HexagonRegisterInfo.h"
11 #include "HexagonSubtarget.h"
13 
14 using namespace llvm;
15 
20 
21 
22 void
23 HexagonTargetLowering::initializeHVXLowering() {
24  if (Subtarget.useHVX64BOps()) {
25  addRegisterClass(MVT::v64i8, &Hexagon::HvxVRRegClass);
26  addRegisterClass(MVT::v32i16, &Hexagon::HvxVRRegClass);
27  addRegisterClass(MVT::v16i32, &Hexagon::HvxVRRegClass);
28  addRegisterClass(MVT::v128i8, &Hexagon::HvxWRRegClass);
29  addRegisterClass(MVT::v64i16, &Hexagon::HvxWRRegClass);
30  addRegisterClass(MVT::v32i32, &Hexagon::HvxWRRegClass);
31  // These "short" boolean vector types should be legal because
32  // they will appear as results of vector compares. If they were
33  // not legal, type legalization would try to make them legal
34  // and that would require using operations that do not use or
35  // produce such types. That, in turn, would imply using custom
36  // nodes, which would be unoptimizable by the DAG combiner.
37  // The idea is to rely on target-independent operations as much
38  // as possible.
39  addRegisterClass(MVT::v16i1, &Hexagon::HvxQRRegClass);
40  addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
41  addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
42  addRegisterClass(MVT::v512i1, &Hexagon::HvxQRRegClass);
43  } else if (Subtarget.useHVX128BOps()) {
44  addRegisterClass(MVT::v128i8, &Hexagon::HvxVRRegClass);
45  addRegisterClass(MVT::v64i16, &Hexagon::HvxVRRegClass);
46  addRegisterClass(MVT::v32i32, &Hexagon::HvxVRRegClass);
47  addRegisterClass(MVT::v256i8, &Hexagon::HvxWRRegClass);
48  addRegisterClass(MVT::v128i16, &Hexagon::HvxWRRegClass);
49  addRegisterClass(MVT::v64i32, &Hexagon::HvxWRRegClass);
50  addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
51  addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
52  addRegisterClass(MVT::v128i1, &Hexagon::HvxQRRegClass);
53  addRegisterClass(MVT::v1024i1, &Hexagon::HvxQRRegClass);
54  }
55 
56  // Set up operation actions.
57 
58  bool Use64b = Subtarget.useHVX64BOps();
59  ArrayRef<MVT> LegalV = Use64b ? LegalV64 : LegalV128;
60  ArrayRef<MVT> LegalW = Use64b ? LegalW64 : LegalW128;
61  MVT ByteV = Use64b ? MVT::v64i8 : MVT::v128i8;
62  MVT ByteW = Use64b ? MVT::v128i8 : MVT::v256i8;
63 
64  auto setPromoteTo = [this] (unsigned Opc, MVT FromTy, MVT ToTy) {
65  setOperationAction(Opc, FromTy, Promote);
66  AddPromotedToType(Opc, FromTy, ToTy);
67  };
68 
71 
72  for (MVT T : LegalV) {
75 
83  if (T != ByteV) {
87  }
88 
95  // Make concat-vectors custom to handle concats of more than 2 vectors.
104  if (T != ByteV) {
106  // HVX only has shifts of words and halfwords.
110 
111  // Promote all shuffles to operate on vectors of bytes.
112  setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteV);
113  }
114 
122  }
123 
124  for (MVT T : LegalW) {
125  // Custom-lower BUILD_VECTOR for vector pairs. The standard (target-
126  // independent) handling of it would convert it to a load, which is
127  // not always the optimal choice.
129  // Make concat-vectors custom to handle concats of more than 2 vectors.
131 
132  // Custom-lower these operations for pairs. Expand them into a concat
133  // of the corresponding operations on individual vectors.
141 
147 
158  if (T != ByteW) {
162 
163  // Promote all shuffles to operate on vectors of bytes.
164  setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteW);
165  }
166  }
167 
168  // Boolean vectors.
169 
170  for (MVT T : LegalW) {
171  // Boolean types for vector pairs will overlap with the boolean
172  // types for single vectors, e.g.
173  // v64i8 -> v64i1 (single)
174  // v64i16 -> v64i1 (pair)
175  // Set these actions first, and allow the single actions to overwrite
176  // any duplicates.
177  MVT BoolW = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
182  }
183 
184  for (MVT T : LegalV) {
185  MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
195  }
196 
197  if (Use64b)
200  else
203 
205 }
206 
207 SDValue
208 HexagonTargetLowering::getInt(unsigned IntId, MVT ResTy, ArrayRef<SDValue> Ops,
209  const SDLoc &dl, SelectionDAG &DAG) const {
210  SmallVector<SDValue,4> IntOps;
211  IntOps.push_back(DAG.getConstant(IntId, dl, MVT::i32));
212  for (const SDValue &Op : Ops)
213  IntOps.push_back(Op);
214  return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, ResTy, IntOps);
215 }
216 
217 MVT
218 HexagonTargetLowering::typeJoin(const TypePair &Tys) const {
219  assert(Tys.first.getVectorElementType() == Tys.second.getVectorElementType());
220 
221  MVT ElemTy = Tys.first.getVectorElementType();
222  return MVT::getVectorVT(ElemTy, Tys.first.getVectorNumElements() +
223  Tys.second.getVectorNumElements());
224 }
225 
226 HexagonTargetLowering::TypePair
227 HexagonTargetLowering::typeSplit(MVT VecTy) const {
228  assert(VecTy.isVector());
229  unsigned NumElem = VecTy.getVectorNumElements();
230  assert((NumElem % 2) == 0 && "Expecting even-sized vector type");
231  MVT HalfTy = MVT::getVectorVT(VecTy.getVectorElementType(), NumElem/2);
232  return { HalfTy, HalfTy };
233 }
234 
235 MVT
236 HexagonTargetLowering::typeExtElem(MVT VecTy, unsigned Factor) const {
237  MVT ElemTy = VecTy.getVectorElementType();
238  MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() * Factor);
239  return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
240 }
241 
242 MVT
243 HexagonTargetLowering::typeTruncElem(MVT VecTy, unsigned Factor) const {
244  MVT ElemTy = VecTy.getVectorElementType();
245  MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() / Factor);
246  return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
247 }
248 
249 SDValue
250 HexagonTargetLowering::opCastElem(SDValue Vec, MVT ElemTy,
251  SelectionDAG &DAG) const {
252  if (ty(Vec).getVectorElementType() == ElemTy)
253  return Vec;
254  MVT CastTy = tyVector(Vec.getValueType().getSimpleVT(), ElemTy);
255  return DAG.getBitcast(CastTy, Vec);
256 }
257 
258 SDValue
259 HexagonTargetLowering::opJoin(const VectorPair &Ops, const SDLoc &dl,
260  SelectionDAG &DAG) const {
261  return DAG.getNode(ISD::CONCAT_VECTORS, dl, typeJoin(ty(Ops)),
262  Ops.second, Ops.first);
263 }
264 
265 HexagonTargetLowering::VectorPair
266 HexagonTargetLowering::opSplit(SDValue Vec, const SDLoc &dl,
267  SelectionDAG &DAG) const {
268  TypePair Tys = typeSplit(ty(Vec));
269  if (Vec.getOpcode() == HexagonISD::QCAT)
270  return VectorPair(Vec.getOperand(0), Vec.getOperand(1));
271  return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
272 }
273 
274 bool
275 HexagonTargetLowering::isHvxSingleTy(MVT Ty) const {
276  return Subtarget.isHVXVectorType(Ty) &&
277  Ty.getSizeInBits() == 8 * Subtarget.getVectorLength();
278 }
279 
280 bool
281 HexagonTargetLowering::isHvxPairTy(MVT Ty) const {
282  return Subtarget.isHVXVectorType(Ty) &&
283  Ty.getSizeInBits() == 16 * Subtarget.getVectorLength();
284 }
285 
286 SDValue
287 HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
288  SelectionDAG &DAG) const {
289  if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
290  ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
291 
292  unsigned ElemWidth = ElemTy.getSizeInBits();
293  if (ElemWidth == 8)
294  return ElemIdx;
295 
296  unsigned L = Log2_32(ElemWidth/8);
297  const SDLoc &dl(ElemIdx);
298  return DAG.getNode(ISD::SHL, dl, MVT::i32,
299  {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
300 }
301 
302 SDValue
303 HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
304  SelectionDAG &DAG) const {
305  unsigned ElemWidth = ElemTy.getSizeInBits();
306  assert(ElemWidth >= 8 && ElemWidth <= 32);
307  if (ElemWidth == 32)
308  return Idx;
309 
310  if (ty(Idx) != MVT::i32)
311  Idx = DAG.getBitcast(MVT::i32, Idx);
312  const SDLoc &dl(Idx);
313  SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
314  SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
315  return SubIdx;
316 }
317 
318 SDValue
319 HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
321  SelectionDAG &DAG) const {
322  MVT OpTy = ty(Op0);
323  assert(OpTy == ty(Op1));
324 
325  MVT ElemTy = OpTy.getVectorElementType();
326  if (ElemTy == MVT::i8)
327  return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
328  assert(ElemTy.getSizeInBits() >= 8);
329 
330  MVT ResTy = tyVector(OpTy, MVT::i8);
331  unsigned ElemSize = ElemTy.getSizeInBits() / 8;
332 
333  SmallVector<int,128> ByteMask;
334  for (int M : Mask) {
335  if (M < 0) {
336  for (unsigned I = 0; I != ElemSize; ++I)
337  ByteMask.push_back(-1);
338  } else {
339  int NewM = M*ElemSize;
340  for (unsigned I = 0; I != ElemSize; ++I)
341  ByteMask.push_back(NewM+I);
342  }
343  }
344  assert(ResTy.getVectorNumElements() == ByteMask.size());
345  return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
346  opCastElem(Op1, MVT::i8, DAG), ByteMask);
347 }
348 
349 SDValue
350 HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
351  const SDLoc &dl, MVT VecTy,
352  SelectionDAG &DAG) const {
353  unsigned VecLen = Values.size();
355  MVT ElemTy = VecTy.getVectorElementType();
356  unsigned ElemWidth = ElemTy.getSizeInBits();
357  unsigned HwLen = Subtarget.getVectorLength();
358 
359  unsigned ElemSize = ElemWidth / 8;
360  assert(ElemSize*VecLen == HwLen);
362 
363  if (VecTy.getVectorElementType() != MVT::i32) {
364  assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
365  unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
366  MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
367  for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
368  SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
369  Words.push_back(DAG.getBitcast(MVT::i32, W));
370  }
371  } else {
372  Words.assign(Values.begin(), Values.end());
373  }
374 
375  unsigned NumWords = Words.size();
376  bool IsSplat = true, IsUndef = true;
377  SDValue SplatV;
378  for (unsigned i = 0; i != NumWords && IsSplat; ++i) {
379  if (isUndef(Words[i]))
380  continue;
381  IsUndef = false;
382  if (!SplatV.getNode())
383  SplatV = Words[i];
384  else if (SplatV != Words[i])
385  IsSplat = false;
386  }
387  if (IsUndef)
388  return DAG.getUNDEF(VecTy);
389  if (IsSplat) {
390  assert(SplatV.getNode());
391  auto *IdxN = dyn_cast<ConstantSDNode>(SplatV.getNode());
392  if (IdxN && IdxN->isNullValue())
393  return getZero(dl, VecTy, DAG);
394  return DAG.getNode(HexagonISD::VSPLATW, dl, VecTy, SplatV);
395  }
396 
397  // Delay recognizing constant vectors until here, so that we can generate
398  // a vsplat.
399  SmallVector<ConstantInt*, 128> Consts(VecLen);
400  bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
401  if (AllConst) {
402  ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
403  (Constant**)Consts.end());
404  Constant *CV = ConstantVector::get(Tmp);
405  unsigned Align = HwLen;
406  SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
407  return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
409  }
410 
411  // A special case is a situation where the vector is built entirely from
412  // elements extracted from another vector. This could be done via a shuffle
413  // more efficiently, but typically, the size of the source vector will not
414  // match the size of the vector being built (which precludes the use of a
415  // shuffle directly).
416  // This only handles a single source vector, and the vector being built
417  // should be of a sub-vector type of the source vector type.
418  auto IsBuildFromExtracts = [this,&Values] (SDValue &SrcVec,
419  SmallVectorImpl<int> &SrcIdx) {
420  SDValue Vec;
421  for (SDValue V : Values) {
422  if (isUndef(V)) {
423  SrcIdx.push_back(-1);
424  continue;
425  }
426  if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
427  return false;
428  // All extracts should come from the same vector.
429  SDValue T = V.getOperand(0);
430  if (Vec.getNode() != nullptr && T.getNode() != Vec.getNode())
431  return false;
432  Vec = T;
433  ConstantSDNode *C = dyn_cast<ConstantSDNode>(V.getOperand(1));
434  if (C == nullptr)
435  return false;
436  int I = C->getSExtValue();
437  assert(I >= 0 && "Negative element index");
438  SrcIdx.push_back(I);
439  }
440  SrcVec = Vec;
441  return true;
442  };
443 
444  SmallVector<int,128> ExtIdx;
445  SDValue ExtVec;
446  if (IsBuildFromExtracts(ExtVec, ExtIdx)) {
447  MVT ExtTy = ty(ExtVec);
448  unsigned ExtLen = ExtTy.getVectorNumElements();
449  if (ExtLen == VecLen || ExtLen == 2*VecLen) {
450  // Construct a new shuffle mask that will produce a vector with the same
451  // number of elements as the input vector, and such that the vector we
452  // want will be the initial subvector of it.
454  BitVector Used(ExtLen);
455 
456  for (int M : ExtIdx) {
457  Mask.push_back(M);
458  if (M >= 0)
459  Used.set(M);
460  }
461  // Fill the rest of the mask with the unused elements of ExtVec in hopes
462  // that it will result in a permutation of ExtVec's elements. It's still
463  // fine if it doesn't (e.g. if undefs are present, or elements are
464  // repeated), but permutations can always be done efficiently via vdelta
465  // and vrdelta.
466  for (unsigned I = 0; I != ExtLen; ++I) {
467  if (Mask.size() == ExtLen)
468  break;
469  if (!Used.test(I))
470  Mask.push_back(I);
471  }
472 
473  SDValue S = DAG.getVectorShuffle(ExtTy, dl, ExtVec,
474  DAG.getUNDEF(ExtTy), Mask);
475  if (ExtLen == VecLen)
476  return S;
477  return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, VecTy, S);
478  }
479  }
480 
481  // Construct two halves in parallel, then or them together.
482  assert(4*Words.size() == Subtarget.getVectorLength());
483  SDValue HalfV0 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
484  SDValue HalfV1 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
485  SDValue S = DAG.getConstant(4, dl, MVT::i32);
486  for (unsigned i = 0; i != NumWords/2; ++i) {
487  SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
488  {HalfV0, Words[i]});
489  SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
490  {HalfV1, Words[i+NumWords/2]});
491  HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
492  HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
493  }
494 
495  HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
496  {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
497  SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
498  return DstV;
499 }
500 
501 SDValue
502 HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
503  unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
504  MVT PredTy = ty(PredV);
505  unsigned HwLen = Subtarget.getVectorLength();
506  MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
507 
508  if (Subtarget.isHVXVectorType(PredTy, true)) {
509  // Move the vector predicate SubV to a vector register, and scale it
510  // down to match the representation (bytes per type element) that VecV
511  // uses. The scaling down will pick every 2nd or 4th (every Scale-th
512  // in general) element and put them at the front of the resulting
513  // vector. This subvector will then be inserted into the Q2V of VecV.
514  // To avoid having an operation that generates an illegal type (short
515  // vector), generate a full size vector.
516  //
517  SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
518  SmallVector<int,128> Mask(HwLen);
519  // Scale = BitBytes(PredV) / Given BitBytes.
520  unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
521  unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
522 
523  for (unsigned i = 0; i != HwLen; ++i) {
524  unsigned Num = i % Scale;
525  unsigned Off = i / Scale;
526  Mask[BlockLen*Num + Off] = i;
527  }
528  SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
529  if (!ZeroFill)
530  return S;
531  // Fill the bytes beyond BlockLen with 0s.
532  MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
533  SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
534  {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
535  SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
536  return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
537  }
538 
539  // Make sure that this is a valid scalar predicate.
540  assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
541 
542  unsigned Bytes = 8 / PredTy.getVectorNumElements();
543  SmallVector<SDValue,4> Words[2];
544  unsigned IdxW = 0;
545 
546  auto Lo32 = [&DAG, &dl] (SDValue P) {
547  return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, P);
548  };
549  auto Hi32 = [&DAG, &dl] (SDValue P) {
550  return DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, P);
551  };
552 
553  SDValue W0 = isUndef(PredV)
554  ? DAG.getUNDEF(MVT::i64)
555  : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
556  Words[IdxW].push_back(Hi32(W0));
557  Words[IdxW].push_back(Lo32(W0));
558 
559  while (Bytes < BitBytes) {
560  IdxW ^= 1;
561  Words[IdxW].clear();
562 
563  if (Bytes < 4) {
564  for (const SDValue &W : Words[IdxW ^ 1]) {
565  SDValue T = expandPredicate(W, dl, DAG);
566  Words[IdxW].push_back(Hi32(T));
567  Words[IdxW].push_back(Lo32(T));
568  }
569  } else {
570  for (const SDValue &W : Words[IdxW ^ 1]) {
571  Words[IdxW].push_back(W);
572  Words[IdxW].push_back(W);
573  }
574  }
575  Bytes *= 2;
576  }
577 
578  assert(Bytes == BitBytes);
579 
580  SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
581  SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
582  for (const SDValue &W : Words[IdxW]) {
583  Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
584  Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
585  }
586 
587  return Vec;
588 }
589 
590 SDValue
591 HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
592  const SDLoc &dl, MVT VecTy,
593  SelectionDAG &DAG) const {
594  // Construct a vector V of bytes, such that a comparison V >u 0 would
595  // produce the required vector predicate.
596  unsigned VecLen = Values.size();
597  unsigned HwLen = Subtarget.getVectorLength();
598  assert(VecLen <= HwLen || VecLen == 8*HwLen);
600  bool AllT = true, AllF = true;
601 
602  auto IsTrue = [] (SDValue V) {
603  if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
604  return !N->isNullValue();
605  return false;
606  };
607  auto IsFalse = [] (SDValue V) {
608  if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
609  return N->isNullValue();
610  return false;
611  };
612 
613  if (VecLen <= HwLen) {
614  // In the hardware, each bit of a vector predicate corresponds to a byte
615  // of a vector register. Calculate how many bytes does a bit of VecTy
616  // correspond to.
617  assert(HwLen % VecLen == 0);
618  unsigned BitBytes = HwLen / VecLen;
619  for (SDValue V : Values) {
620  AllT &= IsTrue(V);
621  AllF &= IsFalse(V);
622 
623  SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
624  : DAG.getUNDEF(MVT::i8);
625  for (unsigned B = 0; B != BitBytes; ++B)
626  Bytes.push_back(Ext);
627  }
628  } else {
629  // There are as many i1 values, as there are bits in a vector register.
630  // Divide the values into groups of 8 and check that each group consists
631  // of the same value (ignoring undefs).
632  for (unsigned I = 0; I != VecLen; I += 8) {
633  unsigned B = 0;
634  // Find the first non-undef value in this group.
635  for (; B != 8; ++B) {
636  if (!Values[I+B].isUndef())
637  break;
638  }
639  SDValue F = Values[I+B];
640  AllT &= IsTrue(F);
641  AllF &= IsFalse(F);
642 
643  SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
644  : DAG.getUNDEF(MVT::i8);
645  Bytes.push_back(Ext);
646  // Verify that the rest of values in the group are the same as the
647  // first.
648  for (; B != 8; ++B)
649  assert(Values[I+B].isUndef() || Values[I+B] == F);
650  }
651  }
652 
653  if (AllT)
654  return DAG.getNode(HexagonISD::QTRUE, dl, VecTy);
655  if (AllF)
656  return DAG.getNode(HexagonISD::QFALSE, dl, VecTy);
657 
658  MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
659  SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
660  return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
661 }
662 
663 SDValue
664 HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
665  const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
666  MVT ElemTy = ty(VecV).getVectorElementType();
667 
668  unsigned ElemWidth = ElemTy.getSizeInBits();
669  assert(ElemWidth >= 8 && ElemWidth <= 32);
670  (void)ElemWidth;
671 
672  SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
673  SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
674  {VecV, ByteIdx});
675  if (ElemTy == MVT::i32)
676  return ExWord;
677 
678  // Have an extracted word, need to extract the smaller element out of it.
679  // 1. Extract the bits of (the original) IdxV that correspond to the index
680  // of the desired element in the 32-bit word.
681  SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
682  // 2. Extract the element from the word.
683  SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
684  return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
685 }
686 
687 SDValue
688 HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
689  const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
690  // Implement other return types if necessary.
691  assert(ResTy == MVT::i1);
692 
693  unsigned HwLen = Subtarget.getVectorLength();
694  MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
695  SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
696 
697  unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
698  SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
699  IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
700 
701  SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
702  SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
703  return getInstr(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
704 }
705 
706 SDValue
707 HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
708  SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
709  MVT ElemTy = ty(VecV).getVectorElementType();
710 
711  unsigned ElemWidth = ElemTy.getSizeInBits();
712  assert(ElemWidth >= 8 && ElemWidth <= 32);
713  (void)ElemWidth;
714 
715  auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
716  SDValue ByteIdxV) {
717  MVT VecTy = ty(VecV);
718  unsigned HwLen = Subtarget.getVectorLength();
719  SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
720  {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
721  SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
722  SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
723  SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
724  {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
725  SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
726  return TorV;
727  };
728 
729  SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
730  if (ElemTy == MVT::i32)
731  return InsertWord(VecV, ValV, ByteIdx);
732 
733  // If this is not inserting a 32-bit word, convert it into such a thing.
734  // 1. Extract the existing word from the target vector.
735  SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
736  {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
737  SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
738  dl, MVT::i32, DAG);
739 
740  // 2. Treating the extracted word as a 32-bit vector, insert the given
741  // value into it.
742  SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
743  MVT SubVecTy = tyVector(ty(Ext), ElemTy);
744  SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
745  ValV, SubIdx, dl, ElemTy, DAG);
746 
747  // 3. Insert the 32-bit word back into the original vector.
748  return InsertWord(VecV, Ins, ByteIdx);
749 }
750 
751 SDValue
752 HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
753  SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
754  unsigned HwLen = Subtarget.getVectorLength();
755  MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
756  SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
757 
758  unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
759  SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
760  IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
761  ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
762 
763  SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
764  return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
765 }
766 
767 SDValue
768 HexagonTargetLowering::extractHvxSubvectorReg(SDValue VecV, SDValue IdxV,
769  const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
770  MVT VecTy = ty(VecV);
771  unsigned HwLen = Subtarget.getVectorLength();
772  unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
773  MVT ElemTy = VecTy.getVectorElementType();
774  unsigned ElemWidth = ElemTy.getSizeInBits();
775 
776  // If the source vector is a vector pair, get the single vector containing
777  // the subvector of interest. The subvector will never overlap two single
778  // vectors.
779  if (isHvxPairTy(VecTy)) {
780  unsigned SubIdx;
781  if (Idx * ElemWidth >= 8*HwLen) {
782  SubIdx = Hexagon::vsub_hi;
783  Idx -= VecTy.getVectorNumElements() / 2;
784  } else {
785  SubIdx = Hexagon::vsub_lo;
786  }
787  VecTy = typeSplit(VecTy).first;
788  VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
789  if (VecTy == ResTy)
790  return VecV;
791  }
792 
793  // The only meaningful subvectors of a single HVX vector are those that
794  // fit in a scalar register.
795  assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
796 
797  MVT WordTy = tyVector(VecTy, MVT::i32);
798  SDValue WordVec = DAG.getBitcast(WordTy, VecV);
799  unsigned WordIdx = (Idx*ElemWidth) / 32;
800 
801  SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
802  SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
803  if (ResTy.getSizeInBits() == 32)
804  return DAG.getBitcast(ResTy, W0);
805 
806  SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
807  SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
808  SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64, {W1, W0});
809  return DAG.getBitcast(ResTy, WW);
810 }
811 
812 SDValue
813 HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
814  const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
815  MVT VecTy = ty(VecV);
816  unsigned HwLen = Subtarget.getVectorLength();
817  MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
818  SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
819  // IdxV is required to be a constant.
820  unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
821 
822  unsigned ResLen = ResTy.getVectorNumElements();
823  unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
824  unsigned Offset = Idx * BitBytes;
825  SDValue Undef = DAG.getUNDEF(ByteTy);
827 
828  if (Subtarget.isHVXVectorType(ResTy, true)) {
829  // Converting between two vector predicates. Since the result is shorter
830  // than the source, it will correspond to a vector predicate with the
831  // relevant bits replicated. The replication count is the ratio of the
832  // source and target vector lengths.
833  unsigned Rep = VecTy.getVectorNumElements() / ResLen;
834  assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
835  for (unsigned i = 0; i != HwLen/Rep; ++i) {
836  for (unsigned j = 0; j != Rep; ++j)
837  Mask.push_back(i + Offset);
838  }
839  SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
840  return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
841  }
842 
843  // Converting between a vector predicate and a scalar predicate. In the
844  // vector predicate, a group of BitBytes bits will correspond to a single
845  // i1 element of the source vector type. Those bits will all have the same
846  // value. The same will be true for ByteVec, where each byte corresponds
847  // to a bit in the vector predicate.
848  // The algorithm is to traverse the ByteVec, going over the i1 values from
849  // the source vector, and generate the corresponding representation in an
850  // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
851  // elements so that the interesting 8 bytes will be in the low end of the
852  // vector.
853  unsigned Rep = 8 / ResLen;
854  // Make sure the output fill the entire vector register, so repeat the
855  // 8-byte groups as many times as necessary.
856  for (unsigned r = 0; r != HwLen/ResLen; ++r) {
857  // This will generate the indexes of the 8 interesting bytes.
858  for (unsigned i = 0; i != ResLen; ++i) {
859  for (unsigned j = 0; j != Rep; ++j)
860  Mask.push_back(Offset + i*BitBytes);
861  }
862  }
863 
864  SDValue Zero = getZero(dl, MVT::i32, DAG);
865  SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
866  // Combine the two low words from ShuffV into a v8i8, and byte-compare
867  // them against 0.
868  SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
870  {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
871  SDValue Vec64 = DAG.getNode(HexagonISD::COMBINE, dl, MVT::v8i8, {W1, W0});
872  return getInstr(Hexagon::A4_vcmpbgtui, dl, ResTy,
873  {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
874 }
875 
876 SDValue
877 HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
878  SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
879  MVT VecTy = ty(VecV);
880  MVT SubTy = ty(SubV);
881  unsigned HwLen = Subtarget.getVectorLength();
882  MVT ElemTy = VecTy.getVectorElementType();
883  unsigned ElemWidth = ElemTy.getSizeInBits();
884 
885  bool IsPair = isHvxPairTy(VecTy);
886  MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
887  // The two single vectors that VecV consists of, if it's a pair.
888  SDValue V0, V1;
889  SDValue SingleV = VecV;
890  SDValue PickHi;
891 
892  if (IsPair) {
893  V0 = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, VecV);
894  V1 = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, VecV);
895 
896  SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
897  dl, MVT::i32);
898  PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
899  if (isHvxSingleTy(SubTy)) {
900  if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
901  unsigned Idx = CN->getZExtValue();
902  assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
903  unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
904  return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
905  }
906  // If IdxV is not a constant, generate the two variants: with the
907  // SubV as the high and as the low subregister, and select the right
908  // pair based on the IdxV.
909  SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
910  SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
911  return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
912  }
913  // The subvector being inserted must be entirely contained in one of
914  // the vectors V0 or V1. Set SingleV to the correct one, and update
915  // IdxV to be the index relative to the beginning of that vector.
916  SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
917  IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
918  SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
919  }
920 
921  // The only meaningful subvectors of a single HVX vector are those that
922  // fit in a scalar register.
923  assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
924  // Convert IdxV to be index in bytes.
925  auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
926  if (!IdxN || !IdxN->isNullValue()) {
927  IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
928  DAG.getConstant(ElemWidth/8, dl, MVT::i32));
929  SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
930  }
931  // When inserting a single word, the rotation back to the original position
932  // would be by HwLen-Idx, but if two words are inserted, it will need to be
933  // by (HwLen-4)-Idx.
934  unsigned RolBase = HwLen;
935  if (VecTy.getSizeInBits() == 32) {
936  SDValue V = DAG.getBitcast(MVT::i32, SubV);
937  SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, V);
938  } else {
939  SDValue V = DAG.getBitcast(MVT::i64, SubV);
940  SDValue R0 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, V);
941  SDValue R1 = DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, V);
942  SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
943  SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
944  DAG.getConstant(4, dl, MVT::i32));
945  SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
946  RolBase = HwLen-4;
947  }
948  // If the vector wasn't ror'ed, don't ror it back.
949  if (RolBase != 4 || !IdxN || !IdxN->isNullValue()) {
950  SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
951  DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
952  SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
953  }
954 
955  if (IsPair) {
956  SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
957  SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
958  return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
959  }
960  return SingleV;
961 }
962 
963 SDValue
964 HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
965  SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
966  MVT VecTy = ty(VecV);
967  MVT SubTy = ty(SubV);
968  assert(Subtarget.isHVXVectorType(VecTy, true));
969  // VecV is an HVX vector predicate. SubV may be either an HVX vector
970  // predicate as well, or it can be a scalar predicate.
971 
972  unsigned VecLen = VecTy.getVectorNumElements();
973  unsigned HwLen = Subtarget.getVectorLength();
974  assert(HwLen % VecLen == 0 && "Unexpected vector type");
975 
976  unsigned Scale = VecLen / SubTy.getVectorNumElements();
977  unsigned BitBytes = HwLen / VecLen;
978  unsigned BlockLen = HwLen / Scale;
979 
980  MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
981  SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
982  SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
983  SDValue ByteIdx;
984 
985  auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
986  if (!IdxN || !IdxN->isNullValue()) {
987  ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
988  DAG.getConstant(BitBytes, dl, MVT::i32));
989  ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
990  }
991 
992  // ByteVec is the target vector VecV rotated in such a way that the
993  // subvector should be inserted at index 0. Generate a predicate mask
994  // and use vmux to do the insertion.
995  MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
996  SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
997  {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
998  ByteVec = getInstr(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
999  // Rotate ByteVec back, and convert to a vector predicate.
1000  if (!IdxN || !IdxN->isNullValue()) {
1001  SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
1002  SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
1003  ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
1004  }
1005  return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
1006 }
1007 
1008 SDValue
1009 HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
1010  MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
1011  // Sign- and any-extending of a vector predicate to a vector register is
1012  // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
1013  // a vector of 1s (where the 1s are of type matching the vector type).
1014  assert(Subtarget.isHVXVectorType(ResTy));
1015  if (!ZeroExt)
1016  return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
1017 
1018  assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
1019  SDValue True = DAG.getNode(HexagonISD::VSPLAT, dl, ResTy,
1020  DAG.getConstant(1, dl, MVT::i32));
1021  SDValue False = getZero(dl, ResTy, DAG);
1022  return DAG.getSelect(dl, ResTy, VecV, True, False);
1023 }
1024 
1025 SDValue
1026 HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
1027  const {
1028  const SDLoc &dl(Op);
1029  MVT VecTy = ty(Op);
1030 
1031  unsigned Size = Op.getNumOperands();
1033  for (unsigned i = 0; i != Size; ++i)
1034  Ops.push_back(Op.getOperand(i));
1035 
1036  if (VecTy.getVectorElementType() == MVT::i1)
1037  return buildHvxVectorPred(Ops, dl, VecTy, DAG);
1038 
1039  if (VecTy.getSizeInBits() == 16*Subtarget.getVectorLength()) {
1040  ArrayRef<SDValue> A(Ops);
1041  MVT SingleTy = typeSplit(VecTy).first;
1042  SDValue V0 = buildHvxVectorReg(A.take_front(Size/2), dl, SingleTy, DAG);
1043  SDValue V1 = buildHvxVectorReg(A.drop_front(Size/2), dl, SingleTy, DAG);
1044  return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
1045  }
1046 
1047  return buildHvxVectorReg(Ops, dl, VecTy, DAG);
1048 }
1049 
1050 SDValue
1051 HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
1052  const {
1053  // Vector concatenation of two integer (non-bool) vectors does not need
1054  // special lowering. Custom-lower concats of bool vectors and expand
1055  // concats of more than 2 vectors.
1056  MVT VecTy = ty(Op);
1057  const SDLoc &dl(Op);
1058  unsigned NumOp = Op.getNumOperands();
1059  if (VecTy.getVectorElementType() != MVT::i1) {
1060  if (NumOp == 2)
1061  return Op;
1062  // Expand the other cases into a build-vector.
1063  SmallVector<SDValue,8> Elems;
1064  for (SDValue V : Op.getNode()->ops())
1065  DAG.ExtractVectorElements(V, Elems);
1066  // A vector of i16 will be broken up into a build_vector of i16's.
1067  // This is a problem, since at the time of operation legalization,
1068  // all operations are expected to be type-legalized, and i16 is not
1069  // a legal type. If any of the extracted elements is not of a valid
1070  // type, sign-extend it to a valid one.
1071  for (unsigned i = 0, e = Elems.size(); i != e; ++i) {
1072  SDValue V = Elems[i];
1073  MVT Ty = ty(V);
1074  if (!isTypeLegal(Ty)) {
1075  EVT NTy = getTypeToTransformTo(*DAG.getContext(), Ty);
1076  if (V.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1077  Elems[i] = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NTy,
1078  DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NTy,
1079  V.getOperand(0), V.getOperand(1)),
1080  DAG.getValueType(Ty));
1081  continue;
1082  }
1083  // A few less complicated cases.
1084  if (V.getOpcode() == ISD::Constant)
1085  Elems[i] = DAG.getSExtOrTrunc(V, dl, NTy);
1086  else if (V.isUndef())
1087  Elems[i] = DAG.getUNDEF(NTy);
1088  else
1089  llvm_unreachable("Unexpected vector element");
1090  }
1091  }
1092  return DAG.getBuildVector(VecTy, dl, Elems);
1093  }
1094 
1095  assert(VecTy.getVectorElementType() == MVT::i1);
1096  unsigned HwLen = Subtarget.getVectorLength();
1097  assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
1098 
1099  SDValue Op0 = Op.getOperand(0);
1100 
1101  // If the operands are HVX types (i.e. not scalar predicates), then
1102  // defer the concatenation, and create QCAT instead.
1103  if (Subtarget.isHVXVectorType(ty(Op0), true)) {
1104  if (NumOp == 2)
1105  return DAG.getNode(HexagonISD::QCAT, dl, VecTy, Op0, Op.getOperand(1));
1106 
1107  ArrayRef<SDUse> U(Op.getNode()->ops());
1108  SmallVector<SDValue,4> SV(U.begin(), U.end());
1109  ArrayRef<SDValue> Ops(SV);
1110 
1111  MVT HalfTy = typeSplit(VecTy).first;
1112  SDValue V0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1113  Ops.take_front(NumOp/2));
1114  SDValue V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1115  Ops.take_back(NumOp/2));
1116  return DAG.getNode(HexagonISD::QCAT, dl, VecTy, V0, V1);
1117  }
1118 
1119  // Count how many bytes (in a vector register) each bit in VecTy
1120  // corresponds to.
1121  unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
1122 
1123  SmallVector<SDValue,8> Prefixes;
1124  for (SDValue V : Op.getNode()->op_values()) {
1125  SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
1126  Prefixes.push_back(P);
1127  }
1128 
1129  unsigned InpLen = ty(Op.getOperand(0)).getVectorNumElements();
1130  MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1131  SDValue S = DAG.getConstant(InpLen*BitBytes, dl, MVT::i32);
1132  SDValue Res = getZero(dl, ByteTy, DAG);
1133  for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
1134  Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
1135  Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
1136  }
1137  return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
1138 }
1139 
1140 SDValue
1141 HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
1142  const {
1143  // Change the type of the extracted element to i32.
1144  SDValue VecV = Op.getOperand(0);
1145  MVT ElemTy = ty(VecV).getVectorElementType();
1146  const SDLoc &dl(Op);
1147  SDValue IdxV = Op.getOperand(1);
1148  if (ElemTy == MVT::i1)
1149  return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
1150 
1151  return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
1152 }
1153 
1154 SDValue
1155 HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
1156  const {
1157  const SDLoc &dl(Op);
1158  SDValue VecV = Op.getOperand(0);
1159  SDValue ValV = Op.getOperand(1);
1160  SDValue IdxV = Op.getOperand(2);
1161  MVT ElemTy = ty(VecV).getVectorElementType();
1162  if (ElemTy == MVT::i1)
1163  return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
1164 
1165  return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
1166 }
1167 
1168 SDValue
1169 HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
1170  const {
1171  SDValue SrcV = Op.getOperand(0);
1172  MVT SrcTy = ty(SrcV);
1173  MVT DstTy = ty(Op);
1174  SDValue IdxV = Op.getOperand(1);
1175  unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
1176  assert(Idx % DstTy.getVectorNumElements() == 0);
1177  (void)Idx;
1178  const SDLoc &dl(Op);
1179 
1180  MVT ElemTy = SrcTy.getVectorElementType();
1181  if (ElemTy == MVT::i1)
1182  return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
1183 
1184  return extractHvxSubvectorReg(SrcV, IdxV, dl, DstTy, DAG);
1185 }
1186 
1187 SDValue
1188 HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
1189  const {
1190  // Idx does not need to be a constant.
1191  SDValue VecV = Op.getOperand(0);
1192  SDValue ValV = Op.getOperand(1);
1193  SDValue IdxV = Op.getOperand(2);
1194 
1195  const SDLoc &dl(Op);
1196  MVT VecTy = ty(VecV);
1197  MVT ElemTy = VecTy.getVectorElementType();
1198  if (ElemTy == MVT::i1)
1199  return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
1200 
1201  return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
1202 }
1203 
1204 SDValue
1205 HexagonTargetLowering::LowerHvxAnyExt(SDValue Op, SelectionDAG &DAG) const {
1206  // Lower any-extends of boolean vectors to sign-extends, since they
1207  // translate directly to Q2V. Zero-extending could also be done equally
1208  // fast, but Q2V is used/recognized in more places.
1209  // For all other vectors, use zero-extend.
1210  MVT ResTy = ty(Op);
1211  SDValue InpV = Op.getOperand(0);
1212  MVT ElemTy = ty(InpV).getVectorElementType();
1213  if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1214  return LowerHvxSignExt(Op, DAG);
1215  return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(Op), ResTy, InpV);
1216 }
1217 
1218 SDValue
1219 HexagonTargetLowering::LowerHvxSignExt(SDValue Op, SelectionDAG &DAG) const {
1220  MVT ResTy = ty(Op);
1221  SDValue InpV = Op.getOperand(0);
1222  MVT ElemTy = ty(InpV).getVectorElementType();
1223  if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1224  return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), false, DAG);
1225  return Op;
1226 }
1227 
1228 SDValue
1229 HexagonTargetLowering::LowerHvxZeroExt(SDValue Op, SelectionDAG &DAG) const {
1230  MVT ResTy = ty(Op);
1231  SDValue InpV = Op.getOperand(0);
1232  MVT ElemTy = ty(InpV).getVectorElementType();
1233  if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1234  return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), true, DAG);
1235  return Op;
1236 }
1237 
1238 SDValue
1239 HexagonTargetLowering::LowerHvxCttz(SDValue Op, SelectionDAG &DAG) const {
1240  // Lower vector CTTZ into a computation using CTLZ (Hacker's Delight):
1241  // cttz(x) = bitwidth(x) - ctlz(~x & (x-1))
1242  const SDLoc &dl(Op);
1243  MVT ResTy = ty(Op);
1244  SDValue InpV = Op.getOperand(0);
1245  assert(ResTy == ty(InpV));
1246 
1247  // Calculate the vectors of 1 and bitwidth(x).
1248  MVT ElemTy = ty(InpV).getVectorElementType();
1249  unsigned ElemWidth = ElemTy.getSizeInBits();
1250  // Using uint64_t because a shift by 32 can happen.
1251  uint64_t Splat1 = 0, SplatW = 0;
1252  assert(isPowerOf2_32(ElemWidth) && ElemWidth <= 32);
1253  for (unsigned i = 0; i != 32/ElemWidth; ++i) {
1254  Splat1 = (Splat1 << ElemWidth) | 1;
1255  SplatW = (SplatW << ElemWidth) | ElemWidth;
1256  }
1257  SDValue Vec1 = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
1258  DAG.getConstant(uint32_t(Splat1), dl, MVT::i32));
1259  SDValue VecW = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
1260  DAG.getConstant(uint32_t(SplatW), dl, MVT::i32));
1261  SDValue VecN1 = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
1262  DAG.getConstant(-1, dl, MVT::i32));
1263  // Do not use DAG.getNOT, because that would create BUILD_VECTOR with
1264  // a BITCAST. Here we can skip the BITCAST (so we don't have to handle
1265  // it separately in custom combine or selection).
1266  SDValue A = DAG.getNode(ISD::AND, dl, ResTy,
1267  {DAG.getNode(ISD::XOR, dl, ResTy, {InpV, VecN1}),
1268  DAG.getNode(ISD::SUB, dl, ResTy, {InpV, Vec1})});
1269  return DAG.getNode(ISD::SUB, dl, ResTy,
1270  {VecW, DAG.getNode(ISD::CTLZ, dl, ResTy, A)});
1271 }
1272 
1273 SDValue
1274 HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
1275  MVT ResTy = ty(Op);
1276  assert(ResTy.isVector() && isHvxSingleTy(ResTy));
1277  const SDLoc &dl(Op);
1278  SmallVector<int,256> ShuffMask;
1279 
1280  MVT ElemTy = ResTy.getVectorElementType();
1281  unsigned VecLen = ResTy.getVectorNumElements();
1282  SDValue Vs = Op.getOperand(0);
1283  SDValue Vt = Op.getOperand(1);
1284 
1285  switch (ElemTy.SimpleTy) {
1286  case MVT::i8: {
1287  // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1288  // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1289  // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
1290  MVT ExtTy = typeExtElem(ResTy, 2);
1291  unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
1292  : Hexagon::V6_vmpyhv;
1293  SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
1294 
1295  // Discard high halves of the resulting values, collect the low halves.
1296  for (unsigned I = 0; I < VecLen; I += 2) {
1297  ShuffMask.push_back(I); // Pick even element.
1298  ShuffMask.push_back(I+VecLen); // Pick odd element.
1299  }
1300  VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
1301  SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1302  return DAG.getBitcast(ResTy, BS);
1303  }
1304  case MVT::i16:
1305  // For i16 there is V6_vmpyih, which acts exactly like the MUL opcode.
1306  // (There is also V6_vmpyhv, which behaves in an analogous way to
1307  // V6_vmpybv.)
1308  return getInstr(Hexagon::V6_vmpyih, dl, ResTy, {Vs, Vt}, DAG);
1309  case MVT::i32: {
1310  // Use the following sequence for signed word multiply:
1311  // T0 = V6_vmpyiowh Vs, Vt
1312  // T1 = V6_vaslw T0, 16
1313  // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
1314  SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
1315  SDValue T0 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
1316  SDValue T1 = getInstr(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
1317  SDValue T2 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1318  {T1, Vs, Vt}, DAG);
1319  return T2;
1320  }
1321  default:
1322  break;
1323  }
1324  return SDValue();
1325 }
1326 
1327 SDValue
1328 HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
1329  MVT ResTy = ty(Op);
1330  assert(ResTy.isVector());
1331  const SDLoc &dl(Op);
1332  SmallVector<int,256> ShuffMask;
1333 
1334  MVT ElemTy = ResTy.getVectorElementType();
1335  unsigned VecLen = ResTy.getVectorNumElements();
1336  SDValue Vs = Op.getOperand(0);
1337  SDValue Vt = Op.getOperand(1);
1338  bool IsSigned = Op.getOpcode() == ISD::MULHS;
1339 
1340  if (ElemTy == MVT::i8 || ElemTy == MVT::i16) {
1341  // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1342  // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1343  // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
1344  // For i16, use V6_vmpyhv, which behaves in an analogous way to
1345  // V6_vmpybv: results Lo and Hi are products of even/odd elements
1346  // respectively.
1347  MVT ExtTy = typeExtElem(ResTy, 2);
1348  unsigned MpyOpc = ElemTy == MVT::i8
1349  ? (IsSigned ? Hexagon::V6_vmpybv : Hexagon::V6_vmpyubv)
1350  : (IsSigned ? Hexagon::V6_vmpyhv : Hexagon::V6_vmpyuhv);
1351  SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
1352 
1353  // Discard low halves of the resulting values, collect the high halves.
1354  for (unsigned I = 0; I < VecLen; I += 2) {
1355  ShuffMask.push_back(I+1); // Pick even element.
1356  ShuffMask.push_back(I+VecLen+1); // Pick odd element.
1357  }
1358  VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
1359  SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1360  return DAG.getBitcast(ResTy, BS);
1361  }
1362 
1363  assert(ElemTy == MVT::i32);
1364  SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
1365 
1366  if (IsSigned) {
1367  // mulhs(Vs,Vt) =
1368  // = [(Hi(Vs)*2^16 + Lo(Vs)) *s (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1369  // = [Hi(Vs)*2^16 *s Hi(Vt)*2^16 + Hi(Vs) *su Lo(Vt)*2^16
1370  // + Lo(Vs) *us (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1371  // = [Hi(Vs) *s Hi(Vt)*2^32 + Hi(Vs) *su Lo(Vt)*2^16
1372  // + Lo(Vs) *us Vt] >> 32
1373  // The low half of Lo(Vs)*Lo(Vt) will be discarded (it's not added to
1374  // anything, so it cannot produce any carry over to higher bits),
1375  // so everything in [] can be shifted by 16 without loss of precision.
1376  // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + Lo(Vs)*Vt >> 16] >> 16
1377  // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + V6_vmpyewuh(Vs,Vt)] >> 16
1378  // Denote Hi(Vs) = Vs':
1379  // = [Vs'*s Hi(Vt)*2^16 + Vs' *su Lo(Vt) + V6_vmpyewuh(Vt,Vs)] >> 16
1380  // = Vs'*s Hi(Vt) + (V6_vmpyiewuh(Vs',Vt) + V6_vmpyewuh(Vt,Vs)) >> 16
1381  SDValue T0 = getInstr(Hexagon::V6_vmpyewuh, dl, ResTy, {Vt, Vs}, DAG);
1382  // Get Vs':
1383  SDValue S0 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {Vs, S16}, DAG);
1384  SDValue T1 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1385  {T0, S0, Vt}, DAG);
1386  // Shift by 16:
1387  SDValue S2 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {T1, S16}, DAG);
1388  // Get Vs'*Hi(Vt):
1389  SDValue T2 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {S0, Vt}, DAG);
1390  // Add:
1391  SDValue T3 = DAG.getNode(ISD::ADD, dl, ResTy, {S2, T2});
1392  return T3;
1393  }
1394 
1395  // Unsigned mulhw. (Would expansion using signed mulhw be better?)
1396 
1397  auto LoVec = [&DAG,ResTy,dl] (SDValue Pair) {
1398  return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResTy, Pair);
1399  };
1400  auto HiVec = [&DAG,ResTy,dl] (SDValue Pair) {
1401  return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResTy, Pair);
1402  };
1403 
1404  MVT PairTy = typeJoin({ResTy, ResTy});
1405  SDValue P = getInstr(Hexagon::V6_lvsplatw, dl, ResTy,
1406  {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
1407  // Multiply-unsigned halfwords:
1408  // LoVec = Vs.uh[2i] * Vt.uh[2i],
1409  // HiVec = Vs.uh[2i+1] * Vt.uh[2i+1]
1410  SDValue T0 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, Vt}, DAG);
1411  // The low halves in the LoVec of the pair can be discarded. They are
1412  // not added to anything (in the full-precision product), so they cannot
1413  // produce a carry into the higher bits.
1414  SDValue T1 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {LoVec(T0), S16}, DAG);
1415  // Swap low and high halves in Vt, and do the halfword multiplication
1416  // to get products Vs.uh[2i] * Vt.uh[2i+1] and Vs.uh[2i+1] * Vt.uh[2i].
1417  SDValue D0 = getInstr(Hexagon::V6_vdelta, dl, ResTy, {Vt, P}, DAG);
1418  SDValue T2 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, D0}, DAG);
1419  // T2 has mixed products of halfwords: Lo(Vt)*Hi(Vs) and Hi(Vt)*Lo(Vs).
1420  // These products are words, but cannot be added directly because the
1421  // sums could overflow. Add these products, by halfwords, where each sum
1422  // of a pair of halfwords gives a word.
1423  SDValue T3 = getInstr(Hexagon::V6_vadduhw, dl, PairTy,
1424  {LoVec(T2), HiVec(T2)}, DAG);
1425  // Add the high halfwords from the products of the low halfwords.
1426  SDValue T4 = DAG.getNode(ISD::ADD, dl, ResTy, {T1, LoVec(T3)});
1427  SDValue T5 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {T4, S16}, DAG);
1428  SDValue T6 = DAG.getNode(ISD::ADD, dl, ResTy, {HiVec(T0), HiVec(T3)});
1429  SDValue T7 = DAG.getNode(ISD::ADD, dl, ResTy, {T5, T6});
1430  return T7;
1431 }
1432 
1433 SDValue
1434 HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
1435  // Sign- and zero-extends are legal.
1437  return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, SDLoc(Op), ty(Op),
1438  Op.getOperand(0));
1439 }
1440 
1441 SDValue
1442 HexagonTargetLowering::LowerHvxShift(SDValue Op, SelectionDAG &DAG) const {
1443  if (SDValue S = getVectorShiftByInt(Op, DAG))
1444  return S;
1445  return Op;
1446 }
1447 
1448 SDValue
1449 HexagonTargetLowering::SplitHvxPairOp(SDValue Op, SelectionDAG &DAG) const {
1450  assert(!Op.isMachineOpcode());
1451  SmallVector<SDValue,2> OpsL, OpsH;
1452  const SDLoc &dl(Op);
1453 
1454  auto SplitVTNode = [&DAG,this] (const VTSDNode *N) {
1455  MVT Ty = typeSplit(N->getVT().getSimpleVT()).first;
1456  SDValue TV = DAG.getValueType(Ty);
1457  return std::make_pair(TV, TV);
1458  };
1459 
1460  for (SDValue A : Op.getNode()->ops()) {
1461  VectorPair P = Subtarget.isHVXVectorType(ty(A), true)
1462  ? opSplit(A, dl, DAG)
1463  : std::make_pair(A, A);
1464  // Special case for type operand.
1465  if (Op.getOpcode() == ISD::SIGN_EXTEND_INREG) {
1466  if (const auto *N = dyn_cast<const VTSDNode>(A.getNode()))
1467  P = SplitVTNode(N);
1468  }
1469  OpsL.push_back(P.first);
1470  OpsH.push_back(P.second);
1471  }
1472 
1473  MVT ResTy = ty(Op);
1474  MVT HalfTy = typeSplit(ResTy).first;
1475  SDValue L = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsL);
1476  SDValue H = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsH);
1477  SDValue S = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, L, H);
1478  return S;
1479 }
1480 
1481 SDValue
1482 HexagonTargetLowering::SplitHvxMemOp(SDValue Op, SelectionDAG &DAG) const {
1483  LSBaseSDNode *BN = cast<LSBaseSDNode>(Op.getNode());
1484  assert(BN->isUnindexed());
1485  MVT MemTy = BN->getMemoryVT().getSimpleVT();
1486  if (!isHvxPairTy(MemTy))
1487  return Op;
1488 
1489  const SDLoc &dl(Op);
1490  unsigned HwLen = Subtarget.getVectorLength();
1491  MVT SingleTy = typeSplit(MemTy).first;
1492  SDValue Chain = BN->getChain();
1493  SDValue Base0 = BN->getBasePtr();
1494  SDValue Base1 = DAG.getMemBasePlusOffset(Base0, HwLen, dl);
1495 
1496  MachineMemOperand *MOp0 = nullptr, *MOp1 = nullptr;
1497  if (MachineMemOperand *MMO = BN->getMemOperand()) {
1498  MachineFunction &MF = DAG.getMachineFunction();
1499  MOp0 = MF.getMachineMemOperand(MMO, 0, HwLen);
1500  MOp1 = MF.getMachineMemOperand(MMO, HwLen, HwLen);
1501  }
1502 
1503  unsigned MemOpc = BN->getOpcode();
1504  SDValue NewOp;
1505 
1506  if (MemOpc == ISD::LOAD) {
1507  SDValue Load0 = DAG.getLoad(SingleTy, dl, Chain, Base0, MOp0);
1508  SDValue Load1 = DAG.getLoad(SingleTy, dl, Chain, Base1, MOp1);
1509  NewOp = DAG.getMergeValues(
1510  { DAG.getNode(ISD::CONCAT_VECTORS, dl, MemTy, Load0, Load1),
1512  Load0.getValue(1), Load1.getValue(1)) }, dl);
1513  } else {
1514  assert(MemOpc == ISD::STORE);
1515  VectorPair Vals = opSplit(cast<StoreSDNode>(Op)->getValue(), dl, DAG);
1516  SDValue Store0 = DAG.getStore(Chain, dl, Vals.first, Base0, MOp0);
1517  SDValue Store1 = DAG.getStore(Chain, dl, Vals.second, Base1, MOp1);
1518  NewOp = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store0, Store1);
1519  }
1520 
1521  return NewOp;
1522 }
1523 
1524 SDValue
1525 HexagonTargetLowering::LowerHvxOperation(SDValue Op, SelectionDAG &DAG) const {
1526  unsigned Opc = Op.getOpcode();
1527  bool IsPairOp = isHvxPairTy(ty(Op)) ||
1528  llvm::any_of(Op.getNode()->ops(), [this] (SDValue V) {
1529  return isHvxPairTy(ty(V));
1530  });
1531 
1532  if (IsPairOp) {
1533  switch (Opc) {
1534  default:
1535  break;
1536  case ISD::LOAD:
1537  case ISD::STORE:
1538  return SplitHvxMemOp(Op, DAG);
1539  case ISD::CTPOP:
1540  case ISD::CTLZ:
1541  case ISD::CTTZ:
1542  case ISD::MUL:
1543  case ISD::MULHS:
1544  case ISD::MULHU:
1545  case ISD::AND:
1546  case ISD::OR:
1547  case ISD::XOR:
1548  case ISD::SRA:
1549  case ISD::SHL:
1550  case ISD::SRL:
1551  case ISD::SETCC:
1552  case ISD::VSELECT:
1553  case ISD::SIGN_EXTEND:
1554  case ISD::ZERO_EXTEND:
1556  return SplitHvxPairOp(Op, DAG);
1557  }
1558  }
1559 
1560  switch (Opc) {
1561  default:
1562  break;
1563  case ISD::BUILD_VECTOR: return LowerHvxBuildVector(Op, DAG);
1564  case ISD::CONCAT_VECTORS: return LowerHvxConcatVectors(Op, DAG);
1565  case ISD::INSERT_SUBVECTOR: return LowerHvxInsertSubvector(Op, DAG);
1566  case ISD::INSERT_VECTOR_ELT: return LowerHvxInsertElement(Op, DAG);
1567  case ISD::EXTRACT_SUBVECTOR: return LowerHvxExtractSubvector(Op, DAG);
1568  case ISD::EXTRACT_VECTOR_ELT: return LowerHvxExtractElement(Op, DAG);
1569 
1570  case ISD::ANY_EXTEND: return LowerHvxAnyExt(Op, DAG);
1571  case ISD::SIGN_EXTEND: return LowerHvxSignExt(Op, DAG);
1572  case ISD::ZERO_EXTEND: return LowerHvxZeroExt(Op, DAG);
1573  case ISD::CTTZ: return LowerHvxCttz(Op, DAG);
1574  case ISD::SRA:
1575  case ISD::SHL:
1576  case ISD::SRL: return LowerHvxShift(Op, DAG);
1577  case ISD::MUL: return LowerHvxMul(Op, DAG);
1578  case ISD::MULHS:
1579  case ISD::MULHU: return LowerHvxMulh(Op, DAG);
1580  case ISD::ANY_EXTEND_VECTOR_INREG: return LowerHvxExtend(Op, DAG);
1581  case ISD::SETCC:
1582  case ISD::INTRINSIC_VOID: return Op;
1583  // Unaligned loads will be handled by the default lowering.
1584  case ISD::LOAD: return SDValue();
1585  }
1586 #ifndef NDEBUG
1587  Op.dumpr(&DAG);
1588 #endif
1589  llvm_unreachable("Unhandled HVX operation");
1590 }
1591 
1592 SDValue
1593 HexagonTargetLowering::PerformHvxDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
1594  const {
1595  const SDLoc &dl(N);
1596  SDValue Op(N, 0);
1597 
1598  unsigned Opc = Op.getOpcode();
1599  if (Opc == ISD::VSELECT) {
1600  // (vselect (xor x, qtrue), v0, v1) -> (vselect x, v1, v0)
1601  SDValue Cond = Op.getOperand(0);
1602  if (Cond->getOpcode() == ISD::XOR) {
1603  SDValue C0 = Cond.getOperand(0), C1 = Cond.getOperand(1);
1604  if (C1->getOpcode() == HexagonISD::QTRUE) {
1605  SDValue VSel = DCI.DAG.getNode(ISD::VSELECT, dl, ty(Op), C0,
1606  Op.getOperand(2), Op.getOperand(1));
1607  return VSel;
1608  }
1609  }
1610  }
1611  return SDValue();
1612 }
1613 
1614 bool
1615 HexagonTargetLowering::isHvxOperation(SDValue Op) const {
1616  // If the type of the result, or any operand type are HVX vector types,
1617  // this is an HVX operation.
1618  return Subtarget.isHVXVectorType(ty(Op), true) ||
1619  llvm::any_of(Op.getNode()->ops(),
1620  [this] (SDValue V) {
1621  return Subtarget.isHVXVectorType(ty(V), true);
1622  });
1623 }
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
uint64_t CallInst * C
static MVT getIntegerVT(unsigned BitWidth)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
BitVector & set()
Definition: BitVector.h:397
EVT getValueType() const
Return the ValueType of the referenced return value.
bool isUndef() const
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:221
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR (an vector value) starting with the ...
Definition: ISDOpcodes.h:412
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static MVT getVectorVT(MVT VT, unsigned NumElements)
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:421
iterator begin() const
Definition: ArrayRef.h:146
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:580
bool isVector() const
Return true if this is a vector value type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:255
static const MVT LegalW128[]
bool test(unsigned Idx) const
Definition: BitVector.h:501
unsigned getVectorNumElements() const
const SDValue & getChain() const
F(f)
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS)
Helper function to make it easier to build Select&#39;s if you just have operands and don&#39;t want to check...
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:569
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
static const MVT LegalV64[]
SDValue getConstantPool(const Constant *C, EVT VT, unsigned Align=0, int Offs=0, bool isT=false, unsigned TargetFlags=0)
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1 at the ...
Definition: ISDOpcodes.h:407
std::pair< MCSymbol *, MachineModuleInfoImpl::StubValueTy > PairTy
A description of a memory reference used in the backend.
Shift and rotation operations.
Definition: ISDOpcodes.h:471
Base class for LoadSDNode and StoreSDNode.
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
void setCondCodeAction(ISD::CondCode CC, MVT VT, LegalizeAction Action)
Indicate that the specified condition code is or isn&#39;t supported on the target and indicate what to d...
SimpleValueType SimpleTy
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:485
void setOperationAction(unsigned Op, MVT VT, LegalizeAction Action)
Indicate that the specified operation does not work with the specified type and indicate what to do a...
This class is used to represent EVT&#39;s, which are used to parameterize some operations.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, unsigned base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
void assign(size_type NumElts, const T &Elt)
Definition: SmallVector.h:412
int64_t getSExtValue() const
SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:420
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:490
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:202
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC&#39;s if you just have an ISD::CondCode instead of an SD...
Definition: SelectionDAG.h:996
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
Definition: SelectionDAG.h:911
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:604
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition: ISDOpcodes.h:558
ArrayRef< SDUse > ops() const
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:152
MVT getVectorElementType() const
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector with the specified, possibly variable...
Definition: ISDOpcodes.h:381
#define P(N)
SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:167
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:465
Machine Value Type.
void addRegisterClass(MVT VT, const TargetRegisterClass *RC)
Add the specified register class as an available regclass for the specified value type...
void setTargetDAGCombine(ISD::NodeType NT)
Targets should invoke this method for each target independent node that they want to provide a custom...
bool isMachineOpcode() const
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:158
This is an important base class in LLVM.
Definition: Constant.h:41
iterator_range< value_op_iterator > op_values() const
static const MVT LegalW64[]
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL...
Definition: ISDOpcodes.h:386
#define H(x, y, z)
Definition: MD5.cpp:57
static const MVT LegalV128[]
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1189
constexpr double e
Definition: MathExtras.h:57
void AddPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT)
If Opc/OrigVT is specified as being promoted, the promotion code defaults to trying a larger integer/...
Extended Value Type.
Definition: ValueTypes.h:34
size_t size() const
Definition: SmallVector.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands...
bool isUnindexed() const
Return true if this is NOT a pre/post inc/dec load/store.
unsigned first
SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type...
TokenFactor - This node takes multiple tokens as input and produces a single token result...
Definition: ISDOpcodes.h:51
bool isHVXVectorType(MVT VecTy, bool IncludeBool=false) const
SDValue getMemBasePlusOffset(SDValue Base, int64_t Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:393
std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provides VTs and return the low/high part...
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:223
SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Definition: SelectionDAG.h:758
iterator end() const
Definition: ArrayRef.h:147
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:474
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0)
Append the extracted elements from Start to Count out of the vector Op in Args.
Represents one node in the SelectionDAG.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:585
void setIndexedLoadAction(unsigned IdxMode, MVT VT, LegalizeAction Action)
Indicate that the specified indexed load does or does not work with the specified type and indicate w...
EVT getMemoryVT() const
Return the type of the in-memory value.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:481
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:529
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:532
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:188
void dumpr() const
ArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
Definition: ArrayRef.h:228
unsigned getVectorLength() const
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:448
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:547
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:197
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:672
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
unsigned getOpcode() const
SDValue getValue(unsigned R) const
static MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:399
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SDValue getValueType(EVT)
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:504
unsigned getNumOperands() const
Conversion operators.
Definition: ISDOpcodes.h:526
const SDValue & getOperand(unsigned i) const
SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
const SDValue & getBasePtr() const
LLVMContext * getContext() const
Definition: SelectionDAG.h:430
static Constant * get(ArrayRef< Constant *> V)
Definition: Constants.cpp:1161
#define T1
void setIndexedStoreAction(unsigned IdxMode, MVT VT, LegalizeAction Action)
Indicate that the specified indexed store does or does not work with the specified type and indicate ...
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:441