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"
12 #include "llvm/IR/IntrinsicsHexagon.h"
13 #include "llvm/Support/CommandLine.h"
14 
15 using namespace llvm;
16 
17 static const MVT LegalV64[] =  { MVT::v64i8,  MVT::v32i16,  MVT::v16i32 };
18 static const MVT LegalW64[] =  { MVT::v128i8, MVT::v64i16,  MVT::v32i32 };
19 static const MVT LegalV128[] = { MVT::v128i8, MVT::v64i16,  MVT::v32i32 };
20 static const MVT LegalW128[] = { MVT::v256i8, MVT::v128i16, MVT::v64i32 };
21 
22 
23 void
24 HexagonTargetLowering::initializeHVXLowering() {
25   if (Subtarget.useHVX64BOps()) {
26     addRegisterClass(MVT::v64i8,  &Hexagon::HvxVRRegClass);
27     addRegisterClass(MVT::v32i16, &Hexagon::HvxVRRegClass);
28     addRegisterClass(MVT::v16i32, &Hexagon::HvxVRRegClass);
29     addRegisterClass(MVT::v128i8, &Hexagon::HvxWRRegClass);
30     addRegisterClass(MVT::v64i16, &Hexagon::HvxWRRegClass);
31     addRegisterClass(MVT::v32i32, &Hexagon::HvxWRRegClass);
32     // These "short" boolean vector types should be legal because
33     // they will appear as results of vector compares. If they were
34     // not legal, type legalization would try to make them legal
35     // and that would require using operations that do not use or
36     // produce such types. That, in turn, would imply using custom
37     // nodes, which would be unoptimizable by the DAG combiner.
38     // The idea is to rely on target-independent operations as much
39     // as possible.
40     addRegisterClass(MVT::v16i1, &Hexagon::HvxQRRegClass);
41     addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
42     addRegisterClass(MVT::v64i1, &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   }
54 
55   // Set up operation actions.
56 
57   bool Use64b = Subtarget.useHVX64BOps();
58   ArrayRef<MVT> LegalV = Use64b ? LegalV64 : LegalV128;
59   ArrayRef<MVT> LegalW = Use64b ? LegalW64 : LegalW128;
60   MVT ByteV = Use64b ?  MVT::v64i8 : MVT::v128i8;
61   MVT ByteW = Use64b ? MVT::v128i8 : MVT::v256i8;
62 
63   auto setPromoteTo = [this] (unsigned Opc, MVT FromTy, MVT ToTy) {
64     setOperationAction(Opc, FromTy, Promote);
65     AddPromotedToType(Opc, FromTy, ToTy);
66   };
67 
68   // Handle bitcasts of vector predicates to scalars (e.g. v32i1 to i32).
69   // Note: v16i1 -> i16 is handled in type legalization instead of op
70   // legalization.
71   setOperationAction(ISD::BITCAST,            MVT::i16,   Custom);
72   setOperationAction(ISD::BITCAST,            MVT::i32,   Custom);
73   setOperationAction(ISD::BITCAST,            MVT::i64,   Custom);
74   setOperationAction(ISD::BITCAST,            MVT::v16i1, Custom);
75   setOperationAction(ISD::BITCAST,            MVT::v128i1, Custom);
76   setOperationAction(ISD::BITCAST,            MVT::i128, Custom);
77   setOperationAction(ISD::VECTOR_SHUFFLE,     ByteV,      Legal);
78   setOperationAction(ISD::VECTOR_SHUFFLE,     ByteW,      Legal);
79   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
80 
81   for (MVT T : LegalV) {
82     setIndexedLoadAction(ISD::POST_INC,  T, Legal);
83     setIndexedStoreAction(ISD::POST_INC, T, Legal);
84 
85     setOperationAction(ISD::AND,            T, Legal);
86     setOperationAction(ISD::OR,             T, Legal);
87     setOperationAction(ISD::XOR,            T, Legal);
88     setOperationAction(ISD::ADD,            T, Legal);
89     setOperationAction(ISD::SUB,            T, Legal);
90     setOperationAction(ISD::CTPOP,          T, Legal);
91     setOperationAction(ISD::CTLZ,           T, Legal);
92     if (T != ByteV) {
93       setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, T, Legal);
94       setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, T, Legal);
95       setOperationAction(ISD::BSWAP,                    T, Legal);
96     }
97 
98     setOperationAction(ISD::CTTZ,               T, Custom);
99     setOperationAction(ISD::LOAD,               T, Custom);
100     setOperationAction(ISD::MUL,                T, Custom);
101     setOperationAction(ISD::MULHS,              T, Custom);
102     setOperationAction(ISD::MULHU,              T, Custom);
103     setOperationAction(ISD::BUILD_VECTOR,       T, Custom);
104     // Make concat-vectors custom to handle concats of more than 2 vectors.
105     setOperationAction(ISD::CONCAT_VECTORS,     T, Custom);
106     setOperationAction(ISD::INSERT_SUBVECTOR,   T, Custom);
107     setOperationAction(ISD::INSERT_VECTOR_ELT,  T, Custom);
108     setOperationAction(ISD::EXTRACT_SUBVECTOR,  T, Custom);
109     setOperationAction(ISD::EXTRACT_VECTOR_ELT, T, Custom);
110     setOperationAction(ISD::ANY_EXTEND,         T, Custom);
111     setOperationAction(ISD::SIGN_EXTEND,        T, Custom);
112     setOperationAction(ISD::ZERO_EXTEND,        T, Custom);
113     if (T != ByteV) {
114       setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, T, Custom);
115       // HVX only has shifts of words and halfwords.
116       setOperationAction(ISD::SRA,                     T, Custom);
117       setOperationAction(ISD::SHL,                     T, Custom);
118       setOperationAction(ISD::SRL,                     T, Custom);
119 
120       // Promote all shuffles to operate on vectors of bytes.
121       setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteV);
122     }
123 
124     setCondCodeAction(ISD::SETNE,  T, Expand);
125     setCondCodeAction(ISD::SETLE,  T, Expand);
126     setCondCodeAction(ISD::SETGE,  T, Expand);
127     setCondCodeAction(ISD::SETLT,  T, Expand);
128     setCondCodeAction(ISD::SETULE, T, Expand);
129     setCondCodeAction(ISD::SETUGE, T, Expand);
130     setCondCodeAction(ISD::SETULT, T, Expand);
131   }
132 
133   for (MVT T : LegalW) {
134     // Custom-lower BUILD_VECTOR for vector pairs. The standard (target-
135     // independent) handling of it would convert it to a load, which is
136     // not always the optimal choice.
137     setOperationAction(ISD::BUILD_VECTOR,   T, Custom);
138     // Make concat-vectors custom to handle concats of more than 2 vectors.
139     setOperationAction(ISD::CONCAT_VECTORS, T, Custom);
140 
141     // Custom-lower these operations for pairs. Expand them into a concat
142     // of the corresponding operations on individual vectors.
143     setOperationAction(ISD::ANY_EXTEND,               T, Custom);
144     setOperationAction(ISD::SIGN_EXTEND,              T, Custom);
145     setOperationAction(ISD::ZERO_EXTEND,              T, Custom);
146     setOperationAction(ISD::SIGN_EXTEND_INREG,        T, Custom);
147     setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG,  T, Custom);
148     setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, T, Legal);
149     setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, T, Legal);
150 
151     setOperationAction(ISD::LOAD,     T, Custom);
152     setOperationAction(ISD::STORE,    T, Custom);
153     setOperationAction(ISD::CTLZ,     T, Custom);
154     setOperationAction(ISD::CTTZ,     T, Custom);
155     setOperationAction(ISD::CTPOP,    T, Custom);
156 
157     setOperationAction(ISD::ADD,      T, Legal);
158     setOperationAction(ISD::SUB,      T, Legal);
159     setOperationAction(ISD::MUL,      T, Custom);
160     setOperationAction(ISD::MULHS,    T, Custom);
161     setOperationAction(ISD::MULHU,    T, Custom);
162     setOperationAction(ISD::AND,      T, Custom);
163     setOperationAction(ISD::OR,       T, Custom);
164     setOperationAction(ISD::XOR,      T, Custom);
165     setOperationAction(ISD::SETCC,    T, Custom);
166     setOperationAction(ISD::VSELECT,  T, Custom);
167     if (T != ByteW) {
168       setOperationAction(ISD::SRA,      T, Custom);
169       setOperationAction(ISD::SHL,      T, Custom);
170       setOperationAction(ISD::SRL,      T, Custom);
171 
172       // Promote all shuffles to operate on vectors of bytes.
173       setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteW);
174     }
175   }
176 
177   // Boolean vectors.
178 
179   for (MVT T : LegalW) {
180     // Boolean types for vector pairs will overlap with the boolean
181     // types for single vectors, e.g.
182     //   v64i8  -> v64i1 (single)
183     //   v64i16 -> v64i1 (pair)
184     // Set these actions first, and allow the single actions to overwrite
185     // any duplicates.
186     MVT BoolW = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
187     setOperationAction(ISD::SETCC,              BoolW, Custom);
188     setOperationAction(ISD::AND,                BoolW, Custom);
189     setOperationAction(ISD::OR,                 BoolW, Custom);
190     setOperationAction(ISD::XOR,                BoolW, Custom);
191   }
192 
193   for (MVT T : LegalV) {
194     MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
195     setOperationAction(ISD::BUILD_VECTOR,       BoolV, Custom);
196     setOperationAction(ISD::CONCAT_VECTORS,     BoolV, Custom);
197     setOperationAction(ISD::INSERT_SUBVECTOR,   BoolV, Custom);
198     setOperationAction(ISD::INSERT_VECTOR_ELT,  BoolV, Custom);
199     setOperationAction(ISD::EXTRACT_SUBVECTOR,  BoolV, Custom);
200     setOperationAction(ISD::EXTRACT_VECTOR_ELT, BoolV, Custom);
201     setOperationAction(ISD::AND,                BoolV, Legal);
202     setOperationAction(ISD::OR,                 BoolV, Legal);
203     setOperationAction(ISD::XOR,                BoolV, Legal);
204   }
205 
206   if (Use64b) {
207     for (MVT T: {MVT::v32i8, MVT::v32i16, MVT::v16i8, MVT::v16i16, MVT::v16i32})
208       setOperationAction(ISD::SIGN_EXTEND_INREG, T, Legal);
209   } else {
210     for (MVT T: {MVT::v64i8, MVT::v64i16, MVT::v32i8, MVT::v32i16, MVT::v32i32})
211       setOperationAction(ISD::SIGN_EXTEND_INREG, T, Legal);
212   }
213 
214   setTargetDAGCombine(ISD::VSELECT);
215 }
216 
217 SDValue
218 HexagonTargetLowering::getInt(unsigned IntId, MVT ResTy, ArrayRef<SDValue> Ops,
219                               const SDLoc &dl, SelectionDAG &DAG) const {
220   SmallVector<SDValue,4> IntOps;
221   IntOps.push_back(DAG.getConstant(IntId, dl, MVT::i32));
222   for (const SDValue &Op : Ops)
223     IntOps.push_back(Op);
224   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, ResTy, IntOps);
225 }
226 
227 MVT
228 HexagonTargetLowering::typeJoin(const TypePair &Tys) const {
229   assert(Tys.first.getVectorElementType() == Tys.second.getVectorElementType());
230 
231   MVT ElemTy = Tys.first.getVectorElementType();
232   return MVT::getVectorVT(ElemTy, Tys.first.getVectorNumElements() +
233                                   Tys.second.getVectorNumElements());
234 }
235 
236 HexagonTargetLowering::TypePair
237 HexagonTargetLowering::typeSplit(MVT VecTy) const {
238   assert(VecTy.isVector());
239   unsigned NumElem = VecTy.getVectorNumElements();
240   assert((NumElem % 2) == 0 && "Expecting even-sized vector type");
241   MVT HalfTy = MVT::getVectorVT(VecTy.getVectorElementType(), NumElem/2);
242   return { HalfTy, HalfTy };
243 }
244 
245 MVT
246 HexagonTargetLowering::typeExtElem(MVT VecTy, unsigned Factor) const {
247   MVT ElemTy = VecTy.getVectorElementType();
248   MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() * Factor);
249   return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
250 }
251 
252 MVT
253 HexagonTargetLowering::typeTruncElem(MVT VecTy, unsigned Factor) const {
254   MVT ElemTy = VecTy.getVectorElementType();
255   MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() / Factor);
256   return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
257 }
258 
259 SDValue
260 HexagonTargetLowering::opCastElem(SDValue Vec, MVT ElemTy,
261                                   SelectionDAG &DAG) const {
262   if (ty(Vec).getVectorElementType() == ElemTy)
263     return Vec;
264   MVT CastTy = tyVector(Vec.getValueType().getSimpleVT(), ElemTy);
265   return DAG.getBitcast(CastTy, Vec);
266 }
267 
268 SDValue
269 HexagonTargetLowering::opJoin(const VectorPair &Ops, const SDLoc &dl,
270                               SelectionDAG &DAG) const {
271   return DAG.getNode(ISD::CONCAT_VECTORS, dl, typeJoin(ty(Ops)),
272                      Ops.second, Ops.first);
273 }
274 
275 HexagonTargetLowering::VectorPair
276 HexagonTargetLowering::opSplit(SDValue Vec, const SDLoc &dl,
277                                SelectionDAG &DAG) const {
278   TypePair Tys = typeSplit(ty(Vec));
279   if (Vec.getOpcode() == HexagonISD::QCAT)
280     return VectorPair(Vec.getOperand(0), Vec.getOperand(1));
281   return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
282 }
283 
284 bool
285 HexagonTargetLowering::isHvxSingleTy(MVT Ty) const {
286   return Subtarget.isHVXVectorType(Ty) &&
287          Ty.getSizeInBits() == 8 * Subtarget.getVectorLength();
288 }
289 
290 bool
291 HexagonTargetLowering::isHvxPairTy(MVT Ty) const {
292   return Subtarget.isHVXVectorType(Ty) &&
293          Ty.getSizeInBits() == 16 * Subtarget.getVectorLength();
294 }
295 
296 bool
297 HexagonTargetLowering::isHvxBoolTy(MVT Ty) const {
298   return Subtarget.isHVXVectorType(Ty, true) &&
299          Ty.getVectorElementType() == MVT::i1;
300 }
301 
302 bool
303 HexagonTargetLowering::allowsHvxMemoryAccess(MVT VecTy, unsigned Alignment,
304         MachineMemOperand::Flags Flags, bool *Fast) const {
305   // Bool vectors are excluded by default, but make it explicit to
306   // emphasize that bool vectors cannot be loaded or stored.
307   // Also, disallow double vector stores (to prevent unnecessary
308   // store widening in DAG combiner).
309   if (VecTy.getSizeInBits() > 8*Subtarget.getVectorLength())
310     return false;
311   if (!Subtarget.isHVXVectorType(VecTy, /*IncludeBool=*/false))
312     return false;
313   if (Fast)
314     *Fast = true;
315   return true;
316 }
317 
318 bool
319 HexagonTargetLowering::allowsHvxMisalignedMemoryAccesses(MVT VecTy,
320         unsigned Align, MachineMemOperand::Flags Flags, bool *Fast) const {
321   if (!Subtarget.isHVXVectorType(VecTy))
322     return false;
323   // XXX Should this be false?  vmemu are a bit slower than vmem.
324   if (Fast)
325     *Fast = true;
326   return true;
327 }
328 
329 SDValue
330 HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
331                                           SelectionDAG &DAG) const {
332   if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
333     ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
334 
335   unsigned ElemWidth = ElemTy.getSizeInBits();
336   if (ElemWidth == 8)
337     return ElemIdx;
338 
339   unsigned L = Log2_32(ElemWidth/8);
340   const SDLoc &dl(ElemIdx);
341   return DAG.getNode(ISD::SHL, dl, MVT::i32,
342                      {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
343 }
344 
345 SDValue
346 HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
347                                         SelectionDAG &DAG) const {
348   unsigned ElemWidth = ElemTy.getSizeInBits();
349   assert(ElemWidth >= 8 && ElemWidth <= 32);
350   if (ElemWidth == 32)
351     return Idx;
352 
353   if (ty(Idx) != MVT::i32)
354     Idx = DAG.getBitcast(MVT::i32, Idx);
355   const SDLoc &dl(Idx);
356   SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
357   SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
358   return SubIdx;
359 }
360 
361 SDValue
362 HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
363                                       SDValue Op1, ArrayRef<int> Mask,
364                                       SelectionDAG &DAG) const {
365   MVT OpTy = ty(Op0);
366   assert(OpTy == ty(Op1));
367 
368   MVT ElemTy = OpTy.getVectorElementType();
369   if (ElemTy == MVT::i8)
370     return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
371   assert(ElemTy.getSizeInBits() >= 8);
372 
373   MVT ResTy = tyVector(OpTy, MVT::i8);
374   unsigned ElemSize = ElemTy.getSizeInBits() / 8;
375 
376   SmallVector<int,128> ByteMask;
377   for (int M : Mask) {
378     if (M < 0) {
379       for (unsigned I = 0; I != ElemSize; ++I)
380         ByteMask.push_back(-1);
381     } else {
382       int NewM = M*ElemSize;
383       for (unsigned I = 0; I != ElemSize; ++I)
384         ByteMask.push_back(NewM+I);
385     }
386   }
387   assert(ResTy.getVectorNumElements() == ByteMask.size());
388   return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
389                               opCastElem(Op1, MVT::i8, DAG), ByteMask);
390 }
391 
392 SDValue
393 HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
394                                          const SDLoc &dl, MVT VecTy,
395                                          SelectionDAG &DAG) const {
396   unsigned VecLen = Values.size();
397   MachineFunction &MF = DAG.getMachineFunction();
398   MVT ElemTy = VecTy.getVectorElementType();
399   unsigned ElemWidth = ElemTy.getSizeInBits();
400   unsigned HwLen = Subtarget.getVectorLength();
401 
402   unsigned ElemSize = ElemWidth / 8;
403   assert(ElemSize*VecLen == HwLen);
404   SmallVector<SDValue,32> Words;
405 
406   if (VecTy.getVectorElementType() != MVT::i32) {
407     assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
408     unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
409     MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
410     for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
411       SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
412       Words.push_back(DAG.getBitcast(MVT::i32, W));
413     }
414   } else {
415     Words.assign(Values.begin(), Values.end());
416   }
417 
418   unsigned NumWords = Words.size();
419   bool IsSplat = true, IsUndef = true;
420   SDValue SplatV;
421   for (unsigned i = 0; i != NumWords && IsSplat; ++i) {
422     if (isUndef(Words[i]))
423       continue;
424     IsUndef = false;
425     if (!SplatV.getNode())
426       SplatV = Words[i];
427     else if (SplatV != Words[i])
428       IsSplat = false;
429   }
430   if (IsUndef)
431     return DAG.getUNDEF(VecTy);
432   if (IsSplat) {
433     assert(SplatV.getNode());
434     auto *IdxN = dyn_cast<ConstantSDNode>(SplatV.getNode());
435     if (IdxN && IdxN->isNullValue())
436       return getZero(dl, VecTy, DAG);
437     return DAG.getNode(HexagonISD::VSPLATW, dl, VecTy, SplatV);
438   }
439 
440   // Delay recognizing constant vectors until here, so that we can generate
441   // a vsplat.
442   SmallVector<ConstantInt*, 128> Consts(VecLen);
443   bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
444   if (AllConst) {
445     ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
446                             (Constant**)Consts.end());
447     Constant *CV = ConstantVector::get(Tmp);
448     unsigned Align = HwLen;
449     SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
450     return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
451                        MachinePointerInfo::getConstantPool(MF), Align);
452   }
453 
454   // A special case is a situation where the vector is built entirely from
455   // elements extracted from another vector. This could be done via a shuffle
456   // more efficiently, but typically, the size of the source vector will not
457   // match the size of the vector being built (which precludes the use of a
458   // shuffle directly).
459   // This only handles a single source vector, and the vector being built
460   // should be of a sub-vector type of the source vector type.
461   auto IsBuildFromExtracts = [this,&Values] (SDValue &SrcVec,
462                                              SmallVectorImpl<int> &SrcIdx) {
463     SDValue Vec;
464     for (SDValue V : Values) {
465       if (isUndef(V)) {
466         SrcIdx.push_back(-1);
467         continue;
468       }
469       if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
470         return false;
471       // All extracts should come from the same vector.
472       SDValue T = V.getOperand(0);
473       if (Vec.getNode() != nullptr && T.getNode() != Vec.getNode())
474         return false;
475       Vec = T;
476       ConstantSDNode *C = dyn_cast<ConstantSDNode>(V.getOperand(1));
477       if (C == nullptr)
478         return false;
479       int I = C->getSExtValue();
480       assert(I >= 0 && "Negative element index");
481       SrcIdx.push_back(I);
482     }
483     SrcVec = Vec;
484     return true;
485   };
486 
487   SmallVector<int,128> ExtIdx;
488   SDValue ExtVec;
489   if (IsBuildFromExtracts(ExtVec, ExtIdx)) {
490     MVT ExtTy = ty(ExtVec);
491     unsigned ExtLen = ExtTy.getVectorNumElements();
492     if (ExtLen == VecLen || ExtLen == 2*VecLen) {
493       // Construct a new shuffle mask that will produce a vector with the same
494       // number of elements as the input vector, and such that the vector we
495       // want will be the initial subvector of it.
496       SmallVector<int,128> Mask;
497       BitVector Used(ExtLen);
498 
499       for (int M : ExtIdx) {
500         Mask.push_back(M);
501         if (M >= 0)
502           Used.set(M);
503       }
504       // Fill the rest of the mask with the unused elements of ExtVec in hopes
505       // that it will result in a permutation of ExtVec's elements. It's still
506       // fine if it doesn't (e.g. if undefs are present, or elements are
507       // repeated), but permutations can always be done efficiently via vdelta
508       // and vrdelta.
509       for (unsigned I = 0; I != ExtLen; ++I) {
510         if (Mask.size() == ExtLen)
511           break;
512         if (!Used.test(I))
513           Mask.push_back(I);
514       }
515 
516       SDValue S = DAG.getVectorShuffle(ExtTy, dl, ExtVec,
517                                        DAG.getUNDEF(ExtTy), Mask);
518       if (ExtLen == VecLen)
519         return S;
520       return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, VecTy, S);
521     }
522   }
523 
524   // Construct two halves in parallel, then or them together.
525   assert(4*Words.size() == Subtarget.getVectorLength());
526   SDValue HalfV0 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
527   SDValue HalfV1 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
528   SDValue S = DAG.getConstant(4, dl, MVT::i32);
529   for (unsigned i = 0; i != NumWords/2; ++i) {
530     SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
531                             {HalfV0, Words[i]});
532     SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
533                             {HalfV1, Words[i+NumWords/2]});
534     HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
535     HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
536   }
537 
538   HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
539                        {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
540   SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
541   return DstV;
542 }
543 
544 SDValue
545 HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
546       unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
547   MVT PredTy = ty(PredV);
548   unsigned HwLen = Subtarget.getVectorLength();
549   MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
550 
551   if (Subtarget.isHVXVectorType(PredTy, true)) {
552     // Move the vector predicate SubV to a vector register, and scale it
553     // down to match the representation (bytes per type element) that VecV
554     // uses. The scaling down will pick every 2nd or 4th (every Scale-th
555     // in general) element and put them at the front of the resulting
556     // vector. This subvector will then be inserted into the Q2V of VecV.
557     // To avoid having an operation that generates an illegal type (short
558     // vector), generate a full size vector.
559     //
560     SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
561     SmallVector<int,128> Mask(HwLen);
562     // Scale = BitBytes(PredV) / Given BitBytes.
563     unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
564     unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
565 
566     for (unsigned i = 0; i != HwLen; ++i) {
567       unsigned Num = i % Scale;
568       unsigned Off = i / Scale;
569       Mask[BlockLen*Num + Off] = i;
570     }
571     SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
572     if (!ZeroFill)
573       return S;
574     // Fill the bytes beyond BlockLen with 0s.
575     MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
576     SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
577                          {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
578     SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
579     return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
580   }
581 
582   // Make sure that this is a valid scalar predicate.
583   assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
584 
585   unsigned Bytes = 8 / PredTy.getVectorNumElements();
586   SmallVector<SDValue,4> Words[2];
587   unsigned IdxW = 0;
588 
589   auto Lo32 = [&DAG, &dl] (SDValue P) {
590     return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, P);
591   };
592   auto Hi32 = [&DAG, &dl] (SDValue P) {
593     return DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, P);
594   };
595 
596   SDValue W0 = isUndef(PredV)
597                   ? DAG.getUNDEF(MVT::i64)
598                   : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
599   Words[IdxW].push_back(Hi32(W0));
600   Words[IdxW].push_back(Lo32(W0));
601 
602   while (Bytes < BitBytes) {
603     IdxW ^= 1;
604     Words[IdxW].clear();
605 
606     if (Bytes < 4) {
607       for (const SDValue &W : Words[IdxW ^ 1]) {
608         SDValue T = expandPredicate(W, dl, DAG);
609         Words[IdxW].push_back(Hi32(T));
610         Words[IdxW].push_back(Lo32(T));
611       }
612     } else {
613       for (const SDValue &W : Words[IdxW ^ 1]) {
614         Words[IdxW].push_back(W);
615         Words[IdxW].push_back(W);
616       }
617     }
618     Bytes *= 2;
619   }
620 
621   assert(Bytes == BitBytes);
622 
623   SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
624   SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
625   for (const SDValue &W : Words[IdxW]) {
626     Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
627     Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
628   }
629 
630   return Vec;
631 }
632 
633 SDValue
634 HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
635                                           const SDLoc &dl, MVT VecTy,
636                                           SelectionDAG &DAG) const {
637   // Construct a vector V of bytes, such that a comparison V >u 0 would
638   // produce the required vector predicate.
639   unsigned VecLen = Values.size();
640   unsigned HwLen = Subtarget.getVectorLength();
641   assert(VecLen <= HwLen || VecLen == 8*HwLen);
642   SmallVector<SDValue,128> Bytes;
643   bool AllT = true, AllF = true;
644 
645   auto IsTrue = [] (SDValue V) {
646     if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
647       return !N->isNullValue();
648     return false;
649   };
650   auto IsFalse = [] (SDValue V) {
651     if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
652       return N->isNullValue();
653     return false;
654   };
655 
656   if (VecLen <= HwLen) {
657     // In the hardware, each bit of a vector predicate corresponds to a byte
658     // of a vector register. Calculate how many bytes does a bit of VecTy
659     // correspond to.
660     assert(HwLen % VecLen == 0);
661     unsigned BitBytes = HwLen / VecLen;
662     for (SDValue V : Values) {
663       AllT &= IsTrue(V);
664       AllF &= IsFalse(V);
665 
666       SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
667                                  : DAG.getUNDEF(MVT::i8);
668       for (unsigned B = 0; B != BitBytes; ++B)
669         Bytes.push_back(Ext);
670     }
671   } else {
672     // There are as many i1 values, as there are bits in a vector register.
673     // Divide the values into groups of 8 and check that each group consists
674     // of the same value (ignoring undefs).
675     for (unsigned I = 0; I != VecLen; I += 8) {
676       unsigned B = 0;
677       // Find the first non-undef value in this group.
678       for (; B != 8; ++B) {
679         if (!Values[I+B].isUndef())
680           break;
681       }
682       SDValue F = Values[I+B];
683       AllT &= IsTrue(F);
684       AllF &= IsFalse(F);
685 
686       SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
687                             : DAG.getUNDEF(MVT::i8);
688       Bytes.push_back(Ext);
689       // Verify that the rest of values in the group are the same as the
690       // first.
691       for (; B != 8; ++B)
692         assert(Values[I+B].isUndef() || Values[I+B] == F);
693     }
694   }
695 
696   if (AllT)
697     return DAG.getNode(HexagonISD::QTRUE, dl, VecTy);
698   if (AllF)
699     return DAG.getNode(HexagonISD::QFALSE, dl, VecTy);
700 
701   MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
702   SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
703   return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
704 }
705 
706 SDValue
707 HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
708       const SDLoc &dl, MVT ResTy, 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   SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
716   SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
717                                {VecV, ByteIdx});
718   if (ElemTy == MVT::i32)
719     return ExWord;
720 
721   // Have an extracted word, need to extract the smaller element out of it.
722   // 1. Extract the bits of (the original) IdxV that correspond to the index
723   //    of the desired element in the 32-bit word.
724   SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
725   // 2. Extract the element from the word.
726   SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
727   return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
728 }
729 
730 SDValue
731 HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
732       const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
733   // Implement other return types if necessary.
734   assert(ResTy == MVT::i1);
735 
736   unsigned HwLen = Subtarget.getVectorLength();
737   MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
738   SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
739 
740   unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
741   SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
742   IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
743 
744   SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
745   SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
746   return getInstr(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
747 }
748 
749 SDValue
750 HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
751       SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
752   MVT ElemTy = ty(VecV).getVectorElementType();
753 
754   unsigned ElemWidth = ElemTy.getSizeInBits();
755   assert(ElemWidth >= 8 && ElemWidth <= 32);
756   (void)ElemWidth;
757 
758   auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
759                                      SDValue ByteIdxV) {
760     MVT VecTy = ty(VecV);
761     unsigned HwLen = Subtarget.getVectorLength();
762     SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
763                                 {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
764     SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
765     SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
766     SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
767                                {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
768     SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
769     return TorV;
770   };
771 
772   SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
773   if (ElemTy == MVT::i32)
774     return InsertWord(VecV, ValV, ByteIdx);
775 
776   // If this is not inserting a 32-bit word, convert it into such a thing.
777   // 1. Extract the existing word from the target vector.
778   SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
779                                 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
780   SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
781                                      dl, MVT::i32, DAG);
782 
783   // 2. Treating the extracted word as a 32-bit vector, insert the given
784   //    value into it.
785   SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
786   MVT SubVecTy = tyVector(ty(Ext), ElemTy);
787   SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
788                              ValV, SubIdx, dl, ElemTy, DAG);
789 
790   // 3. Insert the 32-bit word back into the original vector.
791   return InsertWord(VecV, Ins, ByteIdx);
792 }
793 
794 SDValue
795 HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
796       SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
797   unsigned HwLen = Subtarget.getVectorLength();
798   MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
799   SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
800 
801   unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
802   SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
803   IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
804   ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
805 
806   SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
807   return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
808 }
809 
810 SDValue
811 HexagonTargetLowering::extractHvxSubvectorReg(SDValue VecV, SDValue IdxV,
812       const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
813   MVT VecTy = ty(VecV);
814   unsigned HwLen = Subtarget.getVectorLength();
815   unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
816   MVT ElemTy = VecTy.getVectorElementType();
817   unsigned ElemWidth = ElemTy.getSizeInBits();
818 
819   // If the source vector is a vector pair, get the single vector containing
820   // the subvector of interest. The subvector will never overlap two single
821   // vectors.
822   if (isHvxPairTy(VecTy)) {
823     unsigned SubIdx;
824     if (Idx * ElemWidth >= 8*HwLen) {
825       SubIdx = Hexagon::vsub_hi;
826       Idx -= VecTy.getVectorNumElements() / 2;
827     } else {
828       SubIdx = Hexagon::vsub_lo;
829     }
830     VecTy = typeSplit(VecTy).first;
831     VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
832     if (VecTy == ResTy)
833       return VecV;
834   }
835 
836   // The only meaningful subvectors of a single HVX vector are those that
837   // fit in a scalar register.
838   assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
839 
840   MVT WordTy = tyVector(VecTy, MVT::i32);
841   SDValue WordVec = DAG.getBitcast(WordTy, VecV);
842   unsigned WordIdx = (Idx*ElemWidth) / 32;
843 
844   SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
845   SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
846   if (ResTy.getSizeInBits() == 32)
847     return DAG.getBitcast(ResTy, W0);
848 
849   SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
850   SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
851   SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64, {W1, W0});
852   return DAG.getBitcast(ResTy, WW);
853 }
854 
855 SDValue
856 HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
857       const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
858   MVT VecTy = ty(VecV);
859   unsigned HwLen = Subtarget.getVectorLength();
860   MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
861   SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
862   // IdxV is required to be a constant.
863   unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
864 
865   unsigned ResLen = ResTy.getVectorNumElements();
866   unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
867   unsigned Offset = Idx * BitBytes;
868   SDValue Undef = DAG.getUNDEF(ByteTy);
869   SmallVector<int,128> Mask;
870 
871   if (Subtarget.isHVXVectorType(ResTy, true)) {
872     // Converting between two vector predicates. Since the result is shorter
873     // than the source, it will correspond to a vector predicate with the
874     // relevant bits replicated. The replication count is the ratio of the
875     // source and target vector lengths.
876     unsigned Rep = VecTy.getVectorNumElements() / ResLen;
877     assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
878     for (unsigned i = 0; i != HwLen/Rep; ++i) {
879       for (unsigned j = 0; j != Rep; ++j)
880         Mask.push_back(i + Offset);
881     }
882     SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
883     return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
884   }
885 
886   // Converting between a vector predicate and a scalar predicate. In the
887   // vector predicate, a group of BitBytes bits will correspond to a single
888   // i1 element of the source vector type. Those bits will all have the same
889   // value. The same will be true for ByteVec, where each byte corresponds
890   // to a bit in the vector predicate.
891   // The algorithm is to traverse the ByteVec, going over the i1 values from
892   // the source vector, and generate the corresponding representation in an
893   // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
894   // elements so that the interesting 8 bytes will be in the low end of the
895   // vector.
896   unsigned Rep = 8 / ResLen;
897   // Make sure the output fill the entire vector register, so repeat the
898   // 8-byte groups as many times as necessary.
899   for (unsigned r = 0; r != HwLen/ResLen; ++r) {
900     // This will generate the indexes of the 8 interesting bytes.
901     for (unsigned i = 0; i != ResLen; ++i) {
902       for (unsigned j = 0; j != Rep; ++j)
903         Mask.push_back(Offset + i*BitBytes);
904     }
905   }
906 
907   SDValue Zero = getZero(dl, MVT::i32, DAG);
908   SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
909   // Combine the two low words from ShuffV into a v8i8, and byte-compare
910   // them against 0.
911   SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
912   SDValue W1 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
913                            {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
914   SDValue Vec64 = DAG.getNode(HexagonISD::COMBINE, dl, MVT::v8i8, {W1, W0});
915   return getInstr(Hexagon::A4_vcmpbgtui, dl, ResTy,
916                   {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
917 }
918 
919 SDValue
920 HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
921       SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
922   MVT VecTy = ty(VecV);
923   MVT SubTy = ty(SubV);
924   unsigned HwLen = Subtarget.getVectorLength();
925   MVT ElemTy = VecTy.getVectorElementType();
926   unsigned ElemWidth = ElemTy.getSizeInBits();
927 
928   bool IsPair = isHvxPairTy(VecTy);
929   MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
930   // The two single vectors that VecV consists of, if it's a pair.
931   SDValue V0, V1;
932   SDValue SingleV = VecV;
933   SDValue PickHi;
934 
935   if (IsPair) {
936     V0 = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, VecV);
937     V1 = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, VecV);
938 
939     SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
940                                     dl, MVT::i32);
941     PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
942     if (isHvxSingleTy(SubTy)) {
943       if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
944         unsigned Idx = CN->getZExtValue();
945         assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
946         unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
947         return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
948       }
949       // If IdxV is not a constant, generate the two variants: with the
950       // SubV as the high and as the low subregister, and select the right
951       // pair based on the IdxV.
952       SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
953       SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
954       return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
955     }
956     // The subvector being inserted must be entirely contained in one of
957     // the vectors V0 or V1. Set SingleV to the correct one, and update
958     // IdxV to be the index relative to the beginning of that vector.
959     SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
960     IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
961     SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
962   }
963 
964   // The only meaningful subvectors of a single HVX vector are those that
965   // fit in a scalar register.
966   assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
967   // Convert IdxV to be index in bytes.
968   auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
969   if (!IdxN || !IdxN->isNullValue()) {
970     IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
971                        DAG.getConstant(ElemWidth/8, dl, MVT::i32));
972     SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
973   }
974   // When inserting a single word, the rotation back to the original position
975   // would be by HwLen-Idx, but if two words are inserted, it will need to be
976   // by (HwLen-4)-Idx.
977   unsigned RolBase = HwLen;
978   if (VecTy.getSizeInBits() == 32) {
979     SDValue V = DAG.getBitcast(MVT::i32, SubV);
980     SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, V);
981   } else {
982     SDValue V = DAG.getBitcast(MVT::i64, SubV);
983     SDValue R0 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, V);
984     SDValue R1 = DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, V);
985     SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
986     SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
987                           DAG.getConstant(4, dl, MVT::i32));
988     SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
989     RolBase = HwLen-4;
990   }
991   // If the vector wasn't ror'ed, don't ror it back.
992   if (RolBase != 4 || !IdxN || !IdxN->isNullValue()) {
993     SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
994                                DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
995     SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
996   }
997 
998   if (IsPair) {
999     SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
1000     SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
1001     return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
1002   }
1003   return SingleV;
1004 }
1005 
1006 SDValue
1007 HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
1008       SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
1009   MVT VecTy = ty(VecV);
1010   MVT SubTy = ty(SubV);
1011   assert(Subtarget.isHVXVectorType(VecTy, true));
1012   // VecV is an HVX vector predicate. SubV may be either an HVX vector
1013   // predicate as well, or it can be a scalar predicate.
1014 
1015   unsigned VecLen = VecTy.getVectorNumElements();
1016   unsigned HwLen = Subtarget.getVectorLength();
1017   assert(HwLen % VecLen == 0 && "Unexpected vector type");
1018 
1019   unsigned Scale = VecLen / SubTy.getVectorNumElements();
1020   unsigned BitBytes = HwLen / VecLen;
1021   unsigned BlockLen = HwLen / Scale;
1022 
1023   MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1024   SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
1025   SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
1026   SDValue ByteIdx;
1027 
1028   auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
1029   if (!IdxN || !IdxN->isNullValue()) {
1030     ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
1031                           DAG.getConstant(BitBytes, dl, MVT::i32));
1032     ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
1033   }
1034 
1035   // ByteVec is the target vector VecV rotated in such a way that the
1036   // subvector should be inserted at index 0. Generate a predicate mask
1037   // and use vmux to do the insertion.
1038   MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
1039   SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
1040                        {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
1041   ByteVec = getInstr(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
1042   // Rotate ByteVec back, and convert to a vector predicate.
1043   if (!IdxN || !IdxN->isNullValue()) {
1044     SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
1045     SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
1046     ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
1047   }
1048   return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
1049 }
1050 
1051 SDValue
1052 HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
1053       MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
1054   // Sign- and any-extending of a vector predicate to a vector register is
1055   // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
1056   // a vector of 1s (where the 1s are of type matching the vector type).
1057   assert(Subtarget.isHVXVectorType(ResTy));
1058   if (!ZeroExt)
1059     return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
1060 
1061   assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
1062   SDValue True = DAG.getNode(HexagonISD::VSPLAT, dl, ResTy,
1063                              DAG.getConstant(1, dl, MVT::i32));
1064   SDValue False = getZero(dl, ResTy, DAG);
1065   return DAG.getSelect(dl, ResTy, VecV, True, False);
1066 }
1067 
1068 SDValue
1069 HexagonTargetLowering::compressHvxPred(SDValue VecQ, const SDLoc &dl,
1070       MVT ResTy, SelectionDAG &DAG) const {
1071   // Given a predicate register VecQ, transfer bits VecQ[0..HwLen-1]
1072   // (i.e. the entire predicate register) to bits [0..HwLen-1] of a
1073   // vector register. The remaining bits of the vector register are
1074   // unspecified.
1075 
1076   MachineFunction &MF = DAG.getMachineFunction();
1077   unsigned HwLen = Subtarget.getVectorLength();
1078   MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1079   MVT PredTy = ty(VecQ);
1080   unsigned PredLen = PredTy.getVectorNumElements();
1081   assert(HwLen % PredLen == 0);
1082   MVT VecTy = MVT::getVectorVT(MVT::getIntegerVT(8*HwLen/PredLen), PredLen);
1083 
1084   Type *Int8Ty = Type::getInt8Ty(*DAG.getContext());
1085   SmallVector<Constant*, 128> Tmp;
1086   // Create an array of bytes (hex): 01,02,04,08,10,20,40,80, 01,02,04,08,...
1087   // These are bytes with the LSB rotated left with respect to their index.
1088   for (unsigned i = 0; i != HwLen/8; ++i) {
1089     for (unsigned j = 0; j != 8; ++j)
1090       Tmp.push_back(ConstantInt::get(Int8Ty, 1ull << j));
1091   }
1092   Constant *CV = ConstantVector::get(Tmp);
1093   unsigned Align = HwLen;
1094   SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, ByteTy, Align), DAG);
1095   SDValue Bytes = DAG.getLoad(ByteTy, dl, DAG.getEntryNode(), CP,
1096       MachinePointerInfo::getConstantPool(MF), Align);
1097 
1098   // Select the bytes that correspond to true bits in the vector predicate.
1099   SDValue Sel = DAG.getSelect(dl, VecTy, VecQ, DAG.getBitcast(VecTy, Bytes),
1100       getZero(dl, VecTy, DAG));
1101   // Calculate the OR of all bytes in each group of 8. That will compress
1102   // all the individual bits into a single byte.
1103   // First, OR groups of 4, via vrmpy with 0x01010101.
1104   SDValue All1 =
1105       DAG.getSplatBuildVector(MVT::v4i8, dl, DAG.getConstant(1, dl, MVT::i32));
1106   SDValue Vrmpy = getInstr(Hexagon::V6_vrmpyub, dl, ByteTy, {Sel, All1}, DAG);
1107   // Then rotate the accumulated vector by 4 bytes, and do the final OR.
1108   SDValue Rot = getInstr(Hexagon::V6_valignbi, dl, ByteTy,
1109       {Vrmpy, Vrmpy, DAG.getTargetConstant(4, dl, MVT::i32)}, DAG);
1110   SDValue Vor = DAG.getNode(ISD::OR, dl, ByteTy, {Vrmpy, Rot});
1111 
1112   // Pick every 8th byte and coalesce them at the beginning of the output.
1113   // For symmetry, coalesce every 1+8th byte after that, then every 2+8th
1114   // byte and so on.
1115   SmallVector<int,128> Mask;
1116   for (unsigned i = 0; i != HwLen; ++i)
1117     Mask.push_back((8*i) % HwLen + i/(HwLen/8));
1118   SDValue Collect =
1119       DAG.getVectorShuffle(ByteTy, dl, Vor, DAG.getUNDEF(ByteTy), Mask);
1120   return DAG.getBitcast(ResTy, Collect);
1121 }
1122 
1123 SDValue
1124 HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
1125       const {
1126   const SDLoc &dl(Op);
1127   MVT VecTy = ty(Op);
1128 
1129   unsigned Size = Op.getNumOperands();
1130   SmallVector<SDValue,128> Ops;
1131   for (unsigned i = 0; i != Size; ++i)
1132     Ops.push_back(Op.getOperand(i));
1133 
1134   if (VecTy.getVectorElementType() == MVT::i1)
1135     return buildHvxVectorPred(Ops, dl, VecTy, DAG);
1136 
1137   if (VecTy.getSizeInBits() == 16*Subtarget.getVectorLength()) {
1138     ArrayRef<SDValue> A(Ops);
1139     MVT SingleTy = typeSplit(VecTy).first;
1140     SDValue V0 = buildHvxVectorReg(A.take_front(Size/2), dl, SingleTy, DAG);
1141     SDValue V1 = buildHvxVectorReg(A.drop_front(Size/2), dl, SingleTy, DAG);
1142     return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
1143   }
1144 
1145   return buildHvxVectorReg(Ops, dl, VecTy, DAG);
1146 }
1147 
1148 SDValue
1149 HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
1150       const {
1151   // Vector concatenation of two integer (non-bool) vectors does not need
1152   // special lowering. Custom-lower concats of bool vectors and expand
1153   // concats of more than 2 vectors.
1154   MVT VecTy = ty(Op);
1155   const SDLoc &dl(Op);
1156   unsigned NumOp = Op.getNumOperands();
1157   if (VecTy.getVectorElementType() != MVT::i1) {
1158     if (NumOp == 2)
1159       return Op;
1160     // Expand the other cases into a build-vector.
1161     SmallVector<SDValue,8> Elems;
1162     for (SDValue V : Op.getNode()->ops())
1163       DAG.ExtractVectorElements(V, Elems);
1164     // A vector of i16 will be broken up into a build_vector of i16's.
1165     // This is a problem, since at the time of operation legalization,
1166     // all operations are expected to be type-legalized, and i16 is not
1167     // a legal type. If any of the extracted elements is not of a valid
1168     // type, sign-extend it to a valid one.
1169     for (unsigned i = 0, e = Elems.size(); i != e; ++i) {
1170       SDValue V = Elems[i];
1171       MVT Ty = ty(V);
1172       if (!isTypeLegal(Ty)) {
1173         EVT NTy = getTypeToTransformTo(*DAG.getContext(), Ty);
1174         if (V.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1175           Elems[i] = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NTy,
1176                                  DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NTy,
1177                                              V.getOperand(0), V.getOperand(1)),
1178                                  DAG.getValueType(Ty));
1179           continue;
1180         }
1181         // A few less complicated cases.
1182         if (V.getOpcode() == ISD::Constant)
1183           Elems[i] = DAG.getSExtOrTrunc(V, dl, NTy);
1184         else if (V.isUndef())
1185           Elems[i] = DAG.getUNDEF(NTy);
1186         else
1187           llvm_unreachable("Unexpected vector element");
1188       }
1189     }
1190     return DAG.getBuildVector(VecTy, dl, Elems);
1191   }
1192 
1193   assert(VecTy.getVectorElementType() == MVT::i1);
1194   unsigned HwLen = Subtarget.getVectorLength();
1195   assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
1196 
1197   SDValue Op0 = Op.getOperand(0);
1198 
1199   // If the operands are HVX types (i.e. not scalar predicates), then
1200   // defer the concatenation, and create QCAT instead.
1201   if (Subtarget.isHVXVectorType(ty(Op0), true)) {
1202     if (NumOp == 2)
1203       return DAG.getNode(HexagonISD::QCAT, dl, VecTy, Op0, Op.getOperand(1));
1204 
1205     ArrayRef<SDUse> U(Op.getNode()->ops());
1206     SmallVector<SDValue,4> SV(U.begin(), U.end());
1207     ArrayRef<SDValue> Ops(SV);
1208 
1209     MVT HalfTy = typeSplit(VecTy).first;
1210     SDValue V0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1211                              Ops.take_front(NumOp/2));
1212     SDValue V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1213                              Ops.take_back(NumOp/2));
1214     return DAG.getNode(HexagonISD::QCAT, dl, VecTy, V0, V1);
1215   }
1216 
1217   // Count how many bytes (in a vector register) each bit in VecTy
1218   // corresponds to.
1219   unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
1220 
1221   SmallVector<SDValue,8> Prefixes;
1222   for (SDValue V : Op.getNode()->op_values()) {
1223     SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
1224     Prefixes.push_back(P);
1225   }
1226 
1227   unsigned InpLen = ty(Op.getOperand(0)).getVectorNumElements();
1228   MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1229   SDValue S = DAG.getConstant(InpLen*BitBytes, dl, MVT::i32);
1230   SDValue Res = getZero(dl, ByteTy, DAG);
1231   for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
1232     Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
1233     Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
1234   }
1235   return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
1236 }
1237 
1238 SDValue
1239 HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
1240       const {
1241   // Change the type of the extracted element to i32.
1242   SDValue VecV = Op.getOperand(0);
1243   MVT ElemTy = ty(VecV).getVectorElementType();
1244   const SDLoc &dl(Op);
1245   SDValue IdxV = Op.getOperand(1);
1246   if (ElemTy == MVT::i1)
1247     return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
1248 
1249   return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
1250 }
1251 
1252 SDValue
1253 HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
1254       const {
1255   const SDLoc &dl(Op);
1256   SDValue VecV = Op.getOperand(0);
1257   SDValue ValV = Op.getOperand(1);
1258   SDValue IdxV = Op.getOperand(2);
1259   MVT ElemTy = ty(VecV).getVectorElementType();
1260   if (ElemTy == MVT::i1)
1261     return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
1262 
1263   return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
1264 }
1265 
1266 SDValue
1267 HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
1268       const {
1269   SDValue SrcV = Op.getOperand(0);
1270   MVT SrcTy = ty(SrcV);
1271   MVT DstTy = ty(Op);
1272   SDValue IdxV = Op.getOperand(1);
1273   unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
1274   assert(Idx % DstTy.getVectorNumElements() == 0);
1275   (void)Idx;
1276   const SDLoc &dl(Op);
1277 
1278   MVT ElemTy = SrcTy.getVectorElementType();
1279   if (ElemTy == MVT::i1)
1280     return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
1281 
1282   return extractHvxSubvectorReg(SrcV, IdxV, dl, DstTy, DAG);
1283 }
1284 
1285 SDValue
1286 HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
1287       const {
1288   // Idx does not need to be a constant.
1289   SDValue VecV = Op.getOperand(0);
1290   SDValue ValV = Op.getOperand(1);
1291   SDValue IdxV = Op.getOperand(2);
1292 
1293   const SDLoc &dl(Op);
1294   MVT VecTy = ty(VecV);
1295   MVT ElemTy = VecTy.getVectorElementType();
1296   if (ElemTy == MVT::i1)
1297     return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
1298 
1299   return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
1300 }
1301 
1302 SDValue
1303 HexagonTargetLowering::LowerHvxAnyExt(SDValue Op, SelectionDAG &DAG) const {
1304   // Lower any-extends of boolean vectors to sign-extends, since they
1305   // translate directly to Q2V. Zero-extending could also be done equally
1306   // fast, but Q2V is used/recognized in more places.
1307   // For all other vectors, use zero-extend.
1308   MVT ResTy = ty(Op);
1309   SDValue InpV = Op.getOperand(0);
1310   MVT ElemTy = ty(InpV).getVectorElementType();
1311   if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1312     return LowerHvxSignExt(Op, DAG);
1313   return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(Op), ResTy, InpV);
1314 }
1315 
1316 SDValue
1317 HexagonTargetLowering::LowerHvxSignExt(SDValue Op, SelectionDAG &DAG) const {
1318   MVT ResTy = ty(Op);
1319   SDValue InpV = Op.getOperand(0);
1320   MVT ElemTy = ty(InpV).getVectorElementType();
1321   if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1322     return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), false, DAG);
1323   return Op;
1324 }
1325 
1326 SDValue
1327 HexagonTargetLowering::LowerHvxZeroExt(SDValue Op, SelectionDAG &DAG) const {
1328   MVT ResTy = ty(Op);
1329   SDValue InpV = Op.getOperand(0);
1330   MVT ElemTy = ty(InpV).getVectorElementType();
1331   if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1332     return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), true, DAG);
1333   return Op;
1334 }
1335 
1336 SDValue
1337 HexagonTargetLowering::LowerHvxCttz(SDValue Op, SelectionDAG &DAG) const {
1338   // Lower vector CTTZ into a computation using CTLZ (Hacker's Delight):
1339   // cttz(x) = bitwidth(x) - ctlz(~x & (x-1))
1340   const SDLoc &dl(Op);
1341   MVT ResTy = ty(Op);
1342   SDValue InpV = Op.getOperand(0);
1343   assert(ResTy == ty(InpV));
1344 
1345   // Calculate the vectors of 1 and bitwidth(x).
1346   MVT ElemTy = ty(InpV).getVectorElementType();
1347   unsigned ElemWidth = ElemTy.getSizeInBits();
1348   // Using uint64_t because a shift by 32 can happen.
1349   uint64_t Splat1 = 0, SplatW = 0;
1350   assert(isPowerOf2_32(ElemWidth) && ElemWidth <= 32);
1351   for (unsigned i = 0; i != 32/ElemWidth; ++i) {
1352     Splat1 = (Splat1 << ElemWidth) | 1;
1353     SplatW = (SplatW << ElemWidth) | ElemWidth;
1354   }
1355   SDValue Vec1 = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
1356                              DAG.getConstant(uint32_t(Splat1), dl, MVT::i32));
1357   SDValue VecW = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
1358                              DAG.getConstant(uint32_t(SplatW), dl, MVT::i32));
1359   SDValue VecN1 = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
1360                               DAG.getConstant(-1, dl, MVT::i32));
1361   // Do not use DAG.getNOT, because that would create BUILD_VECTOR with
1362   // a BITCAST. Here we can skip the BITCAST (so we don't have to handle
1363   // it separately in custom combine or selection).
1364   SDValue A = DAG.getNode(ISD::AND, dl, ResTy,
1365                           {DAG.getNode(ISD::XOR, dl, ResTy, {InpV, VecN1}),
1366                            DAG.getNode(ISD::SUB, dl, ResTy, {InpV, Vec1})});
1367   return DAG.getNode(ISD::SUB, dl, ResTy,
1368                      {VecW, DAG.getNode(ISD::CTLZ, dl, ResTy, A)});
1369 }
1370 
1371 SDValue
1372 HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
1373   MVT ResTy = ty(Op);
1374   assert(ResTy.isVector() && isHvxSingleTy(ResTy));
1375   const SDLoc &dl(Op);
1376   SmallVector<int,256> ShuffMask;
1377 
1378   MVT ElemTy = ResTy.getVectorElementType();
1379   unsigned VecLen = ResTy.getVectorNumElements();
1380   SDValue Vs = Op.getOperand(0);
1381   SDValue Vt = Op.getOperand(1);
1382 
1383   switch (ElemTy.SimpleTy) {
1384     case MVT::i8: {
1385       // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1386       // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1387       // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
1388       MVT ExtTy = typeExtElem(ResTy, 2);
1389       unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
1390                                           : Hexagon::V6_vmpyhv;
1391       SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
1392 
1393       // Discard high halves of the resulting values, collect the low halves.
1394       for (unsigned I = 0; I < VecLen; I += 2) {
1395         ShuffMask.push_back(I);         // Pick even element.
1396         ShuffMask.push_back(I+VecLen);  // Pick odd element.
1397       }
1398       VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
1399       SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1400       return DAG.getBitcast(ResTy, BS);
1401     }
1402     case MVT::i16:
1403       // For i16 there is V6_vmpyih, which acts exactly like the MUL opcode.
1404       // (There is also V6_vmpyhv, which behaves in an analogous way to
1405       // V6_vmpybv.)
1406       return getInstr(Hexagon::V6_vmpyih, dl, ResTy, {Vs, Vt}, DAG);
1407     case MVT::i32: {
1408       // Use the following sequence for signed word multiply:
1409       // T0 = V6_vmpyiowh Vs, Vt
1410       // T1 = V6_vaslw T0, 16
1411       // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
1412       SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
1413       SDValue T0 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
1414       SDValue T1 = getInstr(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
1415       SDValue T2 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1416                             {T1, Vs, Vt}, DAG);
1417       return T2;
1418     }
1419     default:
1420       break;
1421   }
1422   return SDValue();
1423 }
1424 
1425 SDValue
1426 HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
1427   MVT ResTy = ty(Op);
1428   assert(ResTy.isVector());
1429   const SDLoc &dl(Op);
1430   SmallVector<int,256> ShuffMask;
1431 
1432   MVT ElemTy = ResTy.getVectorElementType();
1433   unsigned VecLen = ResTy.getVectorNumElements();
1434   SDValue Vs = Op.getOperand(0);
1435   SDValue Vt = Op.getOperand(1);
1436   bool IsSigned = Op.getOpcode() == ISD::MULHS;
1437 
1438   if (ElemTy == MVT::i8 || ElemTy == MVT::i16) {
1439     // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1440     // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1441     // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
1442     // For i16, use V6_vmpyhv, which behaves in an analogous way to
1443     // V6_vmpybv: results Lo and Hi are products of even/odd elements
1444     // respectively.
1445     MVT ExtTy = typeExtElem(ResTy, 2);
1446     unsigned MpyOpc = ElemTy == MVT::i8
1447         ? (IsSigned ? Hexagon::V6_vmpybv : Hexagon::V6_vmpyubv)
1448         : (IsSigned ? Hexagon::V6_vmpyhv : Hexagon::V6_vmpyuhv);
1449     SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
1450 
1451     // Discard low halves of the resulting values, collect the high halves.
1452     for (unsigned I = 0; I < VecLen; I += 2) {
1453       ShuffMask.push_back(I+1);         // Pick even element.
1454       ShuffMask.push_back(I+VecLen+1);  // Pick odd element.
1455     }
1456     VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
1457     SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1458     return DAG.getBitcast(ResTy, BS);
1459   }
1460 
1461   assert(ElemTy == MVT::i32);
1462   SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
1463 
1464   if (IsSigned) {
1465     // mulhs(Vs,Vt) =
1466     //   = [(Hi(Vs)*2^16 + Lo(Vs)) *s (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1467     //   = [Hi(Vs)*2^16 *s Hi(Vt)*2^16 + Hi(Vs) *su Lo(Vt)*2^16
1468     //      + Lo(Vs) *us (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1469     //   = [Hi(Vs) *s Hi(Vt)*2^32 + Hi(Vs) *su Lo(Vt)*2^16
1470     //      + Lo(Vs) *us Vt] >> 32
1471     // The low half of Lo(Vs)*Lo(Vt) will be discarded (it's not added to
1472     // anything, so it cannot produce any carry over to higher bits),
1473     // so everything in [] can be shifted by 16 without loss of precision.
1474     //   = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + Lo(Vs)*Vt >> 16] >> 16
1475     //   = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + V6_vmpyewuh(Vs,Vt)] >> 16
1476     // Denote Hi(Vs) = Vs':
1477     //   = [Vs'*s Hi(Vt)*2^16 + Vs' *su Lo(Vt) + V6_vmpyewuh(Vt,Vs)] >> 16
1478     //   = Vs'*s Hi(Vt) + (V6_vmpyiewuh(Vs',Vt) + V6_vmpyewuh(Vt,Vs)) >> 16
1479     SDValue T0 = getInstr(Hexagon::V6_vmpyewuh, dl, ResTy, {Vt, Vs}, DAG);
1480     // Get Vs':
1481     SDValue S0 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {Vs, S16}, DAG);
1482     SDValue T1 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1483                           {T0, S0, Vt}, DAG);
1484     // Shift by 16:
1485     SDValue S2 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {T1, S16}, DAG);
1486     // Get Vs'*Hi(Vt):
1487     SDValue T2 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {S0, Vt}, DAG);
1488     // Add:
1489     SDValue T3 = DAG.getNode(ISD::ADD, dl, ResTy, {S2, T2});
1490     return T3;
1491   }
1492 
1493   // Unsigned mulhw. (Would expansion using signed mulhw be better?)
1494 
1495   auto LoVec = [&DAG,ResTy,dl] (SDValue Pair) {
1496     return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResTy, Pair);
1497   };
1498   auto HiVec = [&DAG,ResTy,dl] (SDValue Pair) {
1499     return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResTy, Pair);
1500   };
1501 
1502   MVT PairTy = typeJoin({ResTy, ResTy});
1503   SDValue P = getInstr(Hexagon::V6_lvsplatw, dl, ResTy,
1504                        {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
1505   // Multiply-unsigned halfwords:
1506   //   LoVec = Vs.uh[2i] * Vt.uh[2i],
1507   //   HiVec = Vs.uh[2i+1] * Vt.uh[2i+1]
1508   SDValue T0 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, Vt}, DAG);
1509   // The low halves in the LoVec of the pair can be discarded. They are
1510   // not added to anything (in the full-precision product), so they cannot
1511   // produce a carry into the higher bits.
1512   SDValue T1 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {LoVec(T0), S16}, DAG);
1513   // Swap low and high halves in Vt, and do the halfword multiplication
1514   // to get products Vs.uh[2i] * Vt.uh[2i+1] and Vs.uh[2i+1] * Vt.uh[2i].
1515   SDValue D0 = getInstr(Hexagon::V6_vdelta, dl, ResTy, {Vt, P}, DAG);
1516   SDValue T2 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, D0}, DAG);
1517   // T2 has mixed products of halfwords: Lo(Vt)*Hi(Vs) and Hi(Vt)*Lo(Vs).
1518   // These products are words, but cannot be added directly because the
1519   // sums could overflow. Add these products, by halfwords, where each sum
1520   // of a pair of halfwords gives a word.
1521   SDValue T3 = getInstr(Hexagon::V6_vadduhw, dl, PairTy,
1522                         {LoVec(T2), HiVec(T2)}, DAG);
1523   // Add the high halfwords from the products of the low halfwords.
1524   SDValue T4 = DAG.getNode(ISD::ADD, dl, ResTy, {T1, LoVec(T3)});
1525   SDValue T5 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {T4, S16}, DAG);
1526   SDValue T6 = DAG.getNode(ISD::ADD, dl, ResTy, {HiVec(T0), HiVec(T3)});
1527   SDValue T7 = DAG.getNode(ISD::ADD, dl, ResTy, {T5, T6});
1528   return T7;
1529 }
1530 
1531 SDValue
1532 HexagonTargetLowering::LowerHvxBitcast(SDValue Op, SelectionDAG &DAG) const {
1533   SDValue ValQ = Op.getOperand(0);
1534   MVT ResTy = ty(Op);
1535   MVT VecTy = ty(ValQ);
1536   const SDLoc &dl(Op);
1537 
1538   if (isHvxBoolTy(VecTy) && ResTy.isScalarInteger()) {
1539     unsigned HwLen = Subtarget.getVectorLength();
1540     MVT WordTy = MVT::getVectorVT(MVT::i32, HwLen/4);
1541     SDValue VQ = compressHvxPred(ValQ, dl, WordTy, DAG);
1542     unsigned BitWidth = ResTy.getSizeInBits();
1543 
1544     if (BitWidth < 64) {
1545       SDValue W0 = extractHvxElementReg(VQ, DAG.getConstant(0, dl, MVT::i32),
1546           dl, MVT::i32, DAG);
1547       if (BitWidth == 32)
1548         return W0;
1549       assert(BitWidth < 32u);
1550       return DAG.getZExtOrTrunc(W0, dl, ResTy);
1551     }
1552 
1553     // The result is >= 64 bits. The only options are 64 or 128.
1554     assert(BitWidth == 64 || BitWidth == 128);
1555     SmallVector<SDValue,4> Words;
1556     for (unsigned i = 0; i != BitWidth/32; ++i) {
1557       SDValue W = extractHvxElementReg(
1558           VQ, DAG.getConstant(i, dl, MVT::i32), dl, MVT::i32, DAG);
1559       Words.push_back(W);
1560     }
1561     SmallVector<SDValue,2> Combines;
1562     assert(Words.size() % 2 == 0);
1563     for (unsigned i = 0, e = Words.size(); i < e; i += 2) {
1564       SDValue C = DAG.getNode(
1565           HexagonISD::COMBINE, dl, MVT::i64, {Words[i+1], Words[i]});
1566       Combines.push_back(C);
1567     }
1568 
1569     if (BitWidth == 64)
1570       return Combines[0];
1571 
1572     return DAG.getNode(ISD::BUILD_PAIR, dl, ResTy, Combines);
1573   }
1574 
1575   return Op;
1576 }
1577 
1578 SDValue
1579 HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
1580   // Sign- and zero-extends are legal.
1581   assert(Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG);
1582   return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, SDLoc(Op), ty(Op),
1583                      Op.getOperand(0));
1584 }
1585 
1586 SDValue
1587 HexagonTargetLowering::LowerHvxShift(SDValue Op, SelectionDAG &DAG) const {
1588   if (SDValue S = getVectorShiftByInt(Op, DAG))
1589     return S;
1590   return Op;
1591 }
1592 
1593 SDValue
1594 HexagonTargetLowering::LowerHvxIntrinsic(SDValue Op, SelectionDAG &DAG) const {
1595   const SDLoc &dl(Op);
1596   MVT ResTy = ty(Op);
1597 
1598   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1599   bool Use64b = Subtarget.useHVX64BOps();
1600   unsigned IntPredCast = Use64b ? Intrinsic::hexagon_V6_pred_typecast
1601                                 : Intrinsic::hexagon_V6_pred_typecast_128B;
1602   if (IntNo == IntPredCast) {
1603     SDValue Vs = Op.getOperand(1);
1604     MVT OpTy = ty(Vs);
1605     if (isHvxBoolTy(ResTy) && isHvxBoolTy(OpTy)) {
1606       if (ResTy == OpTy)
1607         return Vs;
1608       return DAG.getNode(HexagonISD::TYPECAST, dl, ResTy, Vs);
1609     }
1610   }
1611 
1612   return Op;
1613 }
1614 
1615 SDValue
1616 HexagonTargetLowering::SplitHvxPairOp(SDValue Op, SelectionDAG &DAG) const {
1617   assert(!Op.isMachineOpcode());
1618   SmallVector<SDValue,2> OpsL, OpsH;
1619   const SDLoc &dl(Op);
1620 
1621   auto SplitVTNode = [&DAG,this] (const VTSDNode *N) {
1622     MVT Ty = typeSplit(N->getVT().getSimpleVT()).first;
1623     SDValue TV = DAG.getValueType(Ty);
1624     return std::make_pair(TV, TV);
1625   };
1626 
1627   for (SDValue A : Op.getNode()->ops()) {
1628     VectorPair P = Subtarget.isHVXVectorType(ty(A), true)
1629                     ? opSplit(A, dl, DAG)
1630                     : std::make_pair(A, A);
1631     // Special case for type operand.
1632     if (Op.getOpcode() == ISD::SIGN_EXTEND_INREG) {
1633       if (const auto *N = dyn_cast<const VTSDNode>(A.getNode()))
1634         P = SplitVTNode(N);
1635     }
1636     OpsL.push_back(P.first);
1637     OpsH.push_back(P.second);
1638   }
1639 
1640   MVT ResTy = ty(Op);
1641   MVT HalfTy = typeSplit(ResTy).first;
1642   SDValue L = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsL);
1643   SDValue H = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsH);
1644   SDValue S = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, L, H);
1645   return S;
1646 }
1647 
1648 SDValue
1649 HexagonTargetLowering::SplitHvxMemOp(SDValue Op, SelectionDAG &DAG) const {
1650   LSBaseSDNode *BN = cast<LSBaseSDNode>(Op.getNode());
1651   assert(BN->isUnindexed());
1652   MVT MemTy = BN->getMemoryVT().getSimpleVT();
1653   if (!isHvxPairTy(MemTy))
1654     return Op;
1655 
1656   const SDLoc &dl(Op);
1657   unsigned HwLen = Subtarget.getVectorLength();
1658   MVT SingleTy = typeSplit(MemTy).first;
1659   SDValue Chain = BN->getChain();
1660   SDValue Base0 = BN->getBasePtr();
1661   SDValue Base1 = DAG.getMemBasePlusOffset(Base0, HwLen, dl);
1662 
1663   MachineMemOperand *MOp0 = nullptr, *MOp1 = nullptr;
1664   if (MachineMemOperand *MMO = BN->getMemOperand()) {
1665     MachineFunction &MF = DAG.getMachineFunction();
1666     MOp0 = MF.getMachineMemOperand(MMO, 0, HwLen);
1667     MOp1 = MF.getMachineMemOperand(MMO, HwLen, HwLen);
1668   }
1669 
1670   unsigned MemOpc = BN->getOpcode();
1671   SDValue NewOp;
1672 
1673   if (MemOpc == ISD::LOAD) {
1674     SDValue Load0 = DAG.getLoad(SingleTy, dl, Chain, Base0, MOp0);
1675     SDValue Load1 = DAG.getLoad(SingleTy, dl, Chain, Base1, MOp1);
1676     NewOp = DAG.getMergeValues(
1677               { DAG.getNode(ISD::CONCAT_VECTORS, dl, MemTy, Load0, Load1),
1678                 DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1679                             Load0.getValue(1), Load1.getValue(1)) }, dl);
1680   } else {
1681     assert(MemOpc == ISD::STORE);
1682     VectorPair Vals = opSplit(cast<StoreSDNode>(Op)->getValue(), dl, DAG);
1683     SDValue Store0 = DAG.getStore(Chain, dl, Vals.first, Base0, MOp0);
1684     SDValue Store1 = DAG.getStore(Chain, dl, Vals.second, Base1, MOp1);
1685     NewOp = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store0, Store1);
1686   }
1687 
1688   return NewOp;
1689 }
1690 
1691 SDValue
1692 HexagonTargetLowering::LowerHvxOperation(SDValue Op, SelectionDAG &DAG) const {
1693   unsigned Opc = Op.getOpcode();
1694   bool IsPairOp = isHvxPairTy(ty(Op)) ||
1695                   llvm::any_of(Op.getNode()->ops(), [this] (SDValue V) {
1696                     return isHvxPairTy(ty(V));
1697                   });
1698 
1699   if (IsPairOp) {
1700     switch (Opc) {
1701       default:
1702         break;
1703       case ISD::LOAD:
1704       case ISD::STORE:
1705         return SplitHvxMemOp(Op, DAG);
1706       case ISD::CTPOP:
1707       case ISD::CTLZ:
1708       case ISD::CTTZ:
1709       case ISD::MUL:
1710       case ISD::MULHS:
1711       case ISD::MULHU:
1712       case ISD::AND:
1713       case ISD::OR:
1714       case ISD::XOR:
1715       case ISD::SRA:
1716       case ISD::SHL:
1717       case ISD::SRL:
1718       case ISD::SETCC:
1719       case ISD::VSELECT:
1720       case ISD::SIGN_EXTEND:
1721       case ISD::ZERO_EXTEND:
1722       case ISD::SIGN_EXTEND_INREG:
1723         return SplitHvxPairOp(Op, DAG);
1724     }
1725   }
1726 
1727   switch (Opc) {
1728     default:
1729       break;
1730     case ISD::BUILD_VECTOR:            return LowerHvxBuildVector(Op, DAG);
1731     case ISD::CONCAT_VECTORS:          return LowerHvxConcatVectors(Op, DAG);
1732     case ISD::INSERT_SUBVECTOR:        return LowerHvxInsertSubvector(Op, DAG);
1733     case ISD::INSERT_VECTOR_ELT:       return LowerHvxInsertElement(Op, DAG);
1734     case ISD::EXTRACT_SUBVECTOR:       return LowerHvxExtractSubvector(Op, DAG);
1735     case ISD::EXTRACT_VECTOR_ELT:      return LowerHvxExtractElement(Op, DAG);
1736     case ISD::BITCAST:                 return LowerHvxBitcast(Op, DAG);
1737     case ISD::ANY_EXTEND:              return LowerHvxAnyExt(Op, DAG);
1738     case ISD::SIGN_EXTEND:             return LowerHvxSignExt(Op, DAG);
1739     case ISD::ZERO_EXTEND:             return LowerHvxZeroExt(Op, DAG);
1740     case ISD::CTTZ:                    return LowerHvxCttz(Op, DAG);
1741     case ISD::SRA:
1742     case ISD::SHL:
1743     case ISD::SRL:                     return LowerHvxShift(Op, DAG);
1744     case ISD::MUL:                     return LowerHvxMul(Op, DAG);
1745     case ISD::MULHS:
1746     case ISD::MULHU:                   return LowerHvxMulh(Op, DAG);
1747     case ISD::ANY_EXTEND_VECTOR_INREG: return LowerHvxExtend(Op, DAG);
1748     case ISD::SETCC:
1749     case ISD::INTRINSIC_VOID:          return Op;
1750     case ISD::INTRINSIC_WO_CHAIN:      return LowerHvxIntrinsic(Op, DAG);
1751     // Unaligned loads will be handled by the default lowering.
1752     case ISD::LOAD:                    return SDValue();
1753   }
1754 #ifndef NDEBUG
1755   Op.dumpr(&DAG);
1756 #endif
1757   llvm_unreachable("Unhandled HVX operation");
1758 }
1759 
1760 void
1761 HexagonTargetLowering::LowerHvxOperationWrapper(SDNode *N,
1762       SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const {
1763 }
1764 
1765 void
1766 HexagonTargetLowering::ReplaceHvxNodeResults(SDNode *N,
1767       SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const {
1768   unsigned Opc = N->getOpcode();
1769   switch (Opc) {
1770     case ISD::BITCAST:
1771       if (isHvxBoolTy(ty(N->getOperand(0)))) {
1772         SDValue Op(N, 0);
1773         SDValue C = LowerHvxBitcast(Op, DAG);
1774         Results.push_back(C);
1775       }
1776       break;
1777     default:
1778       break;
1779   }
1780 }
1781 
1782 SDValue
1783 HexagonTargetLowering::PerformHvxDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
1784       const {
1785   const SDLoc &dl(N);
1786   SDValue Op(N, 0);
1787 
1788   unsigned Opc = Op.getOpcode();
1789   if (Opc == ISD::VSELECT) {
1790     // (vselect (xor x, qtrue), v0, v1) -> (vselect x, v1, v0)
1791     SDValue Cond = Op.getOperand(0);
1792     if (Cond->getOpcode() == ISD::XOR) {
1793       SDValue C0 = Cond.getOperand(0), C1 = Cond.getOperand(1);
1794       if (C1->getOpcode() == HexagonISD::QTRUE) {
1795         SDValue VSel = DCI.DAG.getNode(ISD::VSELECT, dl, ty(Op), C0,
1796                                        Op.getOperand(2), Op.getOperand(1));
1797         return VSel;
1798       }
1799     }
1800   }
1801   return SDValue();
1802 }
1803 
1804 bool
1805 HexagonTargetLowering::isHvxOperation(SDValue Op) const {
1806   // If the type of the result, or any operand type are HVX vector types,
1807   // this is an HVX operation.
1808   return Subtarget.isHVXVectorType(ty(Op), true) ||
1809          llvm::any_of(Op.getNode()->ops(),
1810                       [this] (SDValue V) {
1811                         return Subtarget.isHVXVectorType(ty(V), true);
1812                       });
1813 }
1814 
1815 bool
1816 HexagonTargetLowering::isHvxOperation(SDNode *N) const {
1817   // If the type of any result, or any operand type are HVX vector types,
1818   // this is an HVX operation.
1819   auto IsHvxTy = [this] (EVT Ty) {
1820     return Ty.isSimple() && Subtarget.isHVXVectorType(Ty.getSimpleVT(), true);
1821   };
1822   auto IsHvxOp = [this] (SDValue Op) {
1823     return Subtarget.isHVXVectorType(ty(Op), true);
1824   };
1825   return llvm::any_of(N->values(), IsHvxTy) || llvm::any_of(N->ops(), IsHvxOp);
1826 }
1827