blob: 4b73217180525432348ed51b0f743ca5bbb7c079 [file] [log] [blame]
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001//===-- HexagonISelLoweringHVX.cpp --- Lowering HVX operations ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "HexagonISelLowering.h"
11#include "HexagonRegisterInfo.h"
12#include "HexagonSubtarget.h"
13
14using namespace llvm;
15
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +000016static const MVT LegalV64[] = { MVT::v64i8, MVT::v32i16, MVT::v16i32 };
17static const MVT LegalW64[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
18static const MVT LegalV128[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
19static const MVT LegalW128[] = { MVT::v256i8, MVT::v128i16, MVT::v64i32 };
20
21
22void
23HexagonTargetLowering::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
69 setOperationAction(ISD::VECTOR_SHUFFLE, ByteV, Legal);
70 setOperationAction(ISD::VECTOR_SHUFFLE, ByteW, Legal);
71 setOperationAction(ISD::AND, ByteV, Legal);
72 setOperationAction(ISD::OR, ByteV, Legal);
73 setOperationAction(ISD::XOR, ByteV, Legal);
74
75 for (MVT T : LegalV) {
76 setIndexedLoadAction(ISD::POST_INC, T, Legal);
77 setIndexedStoreAction(ISD::POST_INC, T, Legal);
78
79 setOperationAction(ISD::ADD, T, Legal);
80 setOperationAction(ISD::SUB, T, Legal);
81 if (T != ByteV) {
82 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, T, Legal);
83 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, T, Legal);
84 }
85
86 setOperationAction(ISD::MUL, T, Custom);
87 setOperationAction(ISD::MULHS, T, Custom);
88 setOperationAction(ISD::MULHU, T, Custom);
89 setOperationAction(ISD::BUILD_VECTOR, T, Custom);
90 // Make concat-vectors custom to handle concats of more than 2 vectors.
91 setOperationAction(ISD::CONCAT_VECTORS, T, Custom);
92 setOperationAction(ISD::INSERT_SUBVECTOR, T, Custom);
93 setOperationAction(ISD::INSERT_VECTOR_ELT, T, Custom);
94 setOperationAction(ISD::EXTRACT_SUBVECTOR, T, Custom);
95 setOperationAction(ISD::EXTRACT_VECTOR_ELT, T, Custom);
96 setOperationAction(ISD::ANY_EXTEND, T, Custom);
97 setOperationAction(ISD::SIGN_EXTEND, T, Custom);
98 setOperationAction(ISD::ZERO_EXTEND, T, Custom);
99 if (T != ByteV) {
100 setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, T, Custom);
101 // HVX only has shifts of words and halfwords.
102 setOperationAction(ISD::SRA, T, Custom);
103 setOperationAction(ISD::SHL, T, Custom);
104 setOperationAction(ISD::SRL, T, Custom);
105 }
106
107 setCondCodeAction(ISD::SETNE, T, Expand);
108 setCondCodeAction(ISD::SETLE, T, Expand);
109 setCondCodeAction(ISD::SETGE, T, Expand);
110 setCondCodeAction(ISD::SETLT, T, Expand);
111 setCondCodeAction(ISD::SETULE, T, Expand);
112 setCondCodeAction(ISD::SETUGE, T, Expand);
113 setCondCodeAction(ISD::SETULT, T, Expand);
114 }
115
116 for (MVT T : LegalV) {
117 MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
118 setOperationAction(ISD::BUILD_VECTOR, BoolV, Custom);
119 setOperationAction(ISD::CONCAT_VECTORS, BoolV, Custom);
120 setOperationAction(ISD::INSERT_SUBVECTOR, BoolV, Custom);
121 setOperationAction(ISD::INSERT_VECTOR_ELT, BoolV, Custom);
122 setOperationAction(ISD::EXTRACT_SUBVECTOR, BoolV, Custom);
123 setOperationAction(ISD::EXTRACT_VECTOR_ELT, BoolV, Custom);
124 }
125
126 for (MVT T : LegalV) {
127 if (T == ByteV)
128 continue;
129 // Promote all shuffles to operate on vectors of bytes.
130 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteV);
131 setPromoteTo(ISD::AND, T, ByteV);
132 setPromoteTo(ISD::OR, T, ByteV);
133 setPromoteTo(ISD::XOR, T, ByteV);
134 }
135
136 for (MVT T : LegalW) {
137 // Custom-lower BUILD_VECTOR for vector pairs. The standard (target-
138 // independent) handling of it would convert it to a load, which is
139 // not always the optimal choice.
140 setOperationAction(ISD::BUILD_VECTOR, T, Custom);
141 // Make concat-vectors custom to handle concats of more than 2 vectors.
142 setOperationAction(ISD::CONCAT_VECTORS, T, Custom);
143
144 // Custom-lower these operations for pairs. Expand them into a concat
145 // of the corresponding operations on individual vectors.
146 setOperationAction(ISD::ANY_EXTEND, T, Custom);
147 setOperationAction(ISD::SIGN_EXTEND, T, Custom);
148 setOperationAction(ISD::ZERO_EXTEND, T, Custom);
149 setOperationAction(ISD::SIGN_EXTEND_INREG, T, Custom);
150 setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, T, Custom);
151 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, T, Legal);
152 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, T, Legal);
153
154 setOperationAction(ISD::ADD, T, Legal);
155 setOperationAction(ISD::SUB, T, Legal);
156 setOperationAction(ISD::MUL, T, Custom);
157 setOperationAction(ISD::MULHS, T, Custom);
158 setOperationAction(ISD::MULHU, T, Custom);
159 setOperationAction(ISD::AND, T, Custom);
160 setOperationAction(ISD::OR, T, Custom);
161 setOperationAction(ISD::XOR, T, Custom);
162 setOperationAction(ISD::SETCC, T, Custom);
163 setOperationAction(ISD::VSELECT, T, Custom);
164 if (T != ByteW) {
165 setOperationAction(ISD::SRA, T, Custom);
166 setOperationAction(ISD::SHL, T, Custom);
167 setOperationAction(ISD::SRL, T, Custom);
168
169 // Promote all shuffles to operate on vectors of bytes.
170 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteW);
171 }
172
173 MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
174 setOperationAction(ISD::SETCC, BoolV, Custom);
175 }
176}
177
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000178SDValue
179HexagonTargetLowering::getInt(unsigned IntId, MVT ResTy, ArrayRef<SDValue> Ops,
180 const SDLoc &dl, SelectionDAG &DAG) const {
181 SmallVector<SDValue,4> IntOps;
182 IntOps.push_back(DAG.getConstant(IntId, dl, MVT::i32));
183 for (const SDValue &Op : Ops)
184 IntOps.push_back(Op);
185 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, ResTy, IntOps);
186}
187
188MVT
189HexagonTargetLowering::typeJoin(const TypePair &Tys) const {
190 assert(Tys.first.getVectorElementType() == Tys.second.getVectorElementType());
191
192 MVT ElemTy = Tys.first.getVectorElementType();
193 return MVT::getVectorVT(ElemTy, Tys.first.getVectorNumElements() +
194 Tys.second.getVectorNumElements());
195}
196
197HexagonTargetLowering::TypePair
198HexagonTargetLowering::typeSplit(MVT VecTy) const {
199 assert(VecTy.isVector());
200 unsigned NumElem = VecTy.getVectorNumElements();
201 assert((NumElem % 2) == 0 && "Expecting even-sized vector type");
202 MVT HalfTy = MVT::getVectorVT(VecTy.getVectorElementType(), NumElem/2);
203 return { HalfTy, HalfTy };
204}
205
206MVT
207HexagonTargetLowering::typeExtElem(MVT VecTy, unsigned Factor) const {
208 MVT ElemTy = VecTy.getVectorElementType();
209 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() * Factor);
210 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
211}
212
213MVT
214HexagonTargetLowering::typeTruncElem(MVT VecTy, unsigned Factor) const {
215 MVT ElemTy = VecTy.getVectorElementType();
216 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() / Factor);
217 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
218}
219
220SDValue
221HexagonTargetLowering::opCastElem(SDValue Vec, MVT ElemTy,
222 SelectionDAG &DAG) const {
223 if (ty(Vec).getVectorElementType() == ElemTy)
224 return Vec;
225 MVT CastTy = tyVector(Vec.getValueType().getSimpleVT(), ElemTy);
226 return DAG.getBitcast(CastTy, Vec);
227}
228
229SDValue
230HexagonTargetLowering::opJoin(const VectorPair &Ops, const SDLoc &dl,
231 SelectionDAG &DAG) const {
232 return DAG.getNode(ISD::CONCAT_VECTORS, dl, typeJoin(ty(Ops)),
233 Ops.second, Ops.first);
234}
235
236HexagonTargetLowering::VectorPair
237HexagonTargetLowering::opSplit(SDValue Vec, const SDLoc &dl,
238 SelectionDAG &DAG) const {
239 TypePair Tys = typeSplit(ty(Vec));
Krzysztof Parzyszek1d52a852018-02-06 15:15:13 +0000240 if (Vec.getOpcode() == HexagonISD::QCAT)
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +0000241 return VectorPair(Vec.getOperand(0), Vec.getOperand(1));
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000242 return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
243}
244
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000245bool
246HexagonTargetLowering::isHvxSingleTy(MVT Ty) const {
247 return Subtarget.isHVXVectorType(Ty) &&
248 Ty.getSizeInBits() == 8 * Subtarget.getVectorLength();
249}
250
251bool
252HexagonTargetLowering::isHvxPairTy(MVT Ty) const {
253 return Subtarget.isHVXVectorType(Ty) &&
254 Ty.getSizeInBits() == 16 * Subtarget.getVectorLength();
255}
256
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000257SDValue
258HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
259 SelectionDAG &DAG) const {
260 if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
261 ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
262
263 unsigned ElemWidth = ElemTy.getSizeInBits();
264 if (ElemWidth == 8)
265 return ElemIdx;
266
267 unsigned L = Log2_32(ElemWidth/8);
268 const SDLoc &dl(ElemIdx);
269 return DAG.getNode(ISD::SHL, dl, MVT::i32,
270 {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
271}
272
273SDValue
274HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
275 SelectionDAG &DAG) const {
276 unsigned ElemWidth = ElemTy.getSizeInBits();
277 assert(ElemWidth >= 8 && ElemWidth <= 32);
278 if (ElemWidth == 32)
279 return Idx;
280
281 if (ty(Idx) != MVT::i32)
282 Idx = DAG.getBitcast(MVT::i32, Idx);
283 const SDLoc &dl(Idx);
284 SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
285 SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
286 return SubIdx;
287}
288
289SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000290HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
291 SDValue Op1, ArrayRef<int> Mask,
292 SelectionDAG &DAG) const {
293 MVT OpTy = ty(Op0);
294 assert(OpTy == ty(Op1));
295
296 MVT ElemTy = OpTy.getVectorElementType();
297 if (ElemTy == MVT::i8)
298 return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
299 assert(ElemTy.getSizeInBits() >= 8);
300
301 MVT ResTy = tyVector(OpTy, MVT::i8);
302 unsigned ElemSize = ElemTy.getSizeInBits() / 8;
303
304 SmallVector<int,128> ByteMask;
305 for (int M : Mask) {
306 if (M < 0) {
307 for (unsigned I = 0; I != ElemSize; ++I)
308 ByteMask.push_back(-1);
309 } else {
310 int NewM = M*ElemSize;
311 for (unsigned I = 0; I != ElemSize; ++I)
312 ByteMask.push_back(NewM+I);
313 }
314 }
315 assert(ResTy.getVectorNumElements() == ByteMask.size());
316 return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
317 opCastElem(Op1, MVT::i8, DAG), ByteMask);
318}
319
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000320SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000321HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
322 const SDLoc &dl, MVT VecTy,
323 SelectionDAG &DAG) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000324 unsigned VecLen = Values.size();
325 MachineFunction &MF = DAG.getMachineFunction();
326 MVT ElemTy = VecTy.getVectorElementType();
327 unsigned ElemWidth = ElemTy.getSizeInBits();
328 unsigned HwLen = Subtarget.getVectorLength();
329
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000330 unsigned ElemSize = ElemWidth / 8;
331 assert(ElemSize*VecLen == HwLen);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000332 SmallVector<SDValue,32> Words;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000333
334 if (VecTy.getVectorElementType() != MVT::i32) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000335 assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
336 unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
337 MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000338 for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
339 SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000340 Words.push_back(DAG.getBitcast(MVT::i32, W));
341 }
342 } else {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000343 Words.assign(Values.begin(), Values.end());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000344 }
345
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000346 unsigned NumWords = Words.size();
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000347 bool IsSplat = true, IsUndef = true;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000348 SDValue SplatV;
349 for (unsigned i = 0; i != NumWords && IsSplat; ++i) {
350 if (isUndef(Words[i]))
351 continue;
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000352 IsUndef = false;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000353 if (!SplatV.getNode())
354 SplatV = Words[i];
355 else if (SplatV != Words[i])
356 IsSplat = false;
357 }
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000358 if (IsUndef)
359 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000360 if (IsSplat) {
361 assert(SplatV.getNode());
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000362 auto *IdxN = dyn_cast<ConstantSDNode>(SplatV.getNode());
363 if (IdxN && IdxN->isNullValue())
364 return getZero(dl, VecTy, DAG);
365 MVT WordTy = MVT::getVectorVT(MVT::i32, HwLen/4);
366 SDValue SV = DAG.getNode(HexagonISD::VSPLAT, dl, WordTy, SplatV);
367 return DAG.getBitcast(VecTy, SV);
368 }
369
370 // Delay recognizing constant vectors until here, so that we can generate
371 // a vsplat.
372 SmallVector<ConstantInt*, 128> Consts(VecLen);
373 bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
374 if (AllConst) {
375 ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
376 (Constant**)Consts.end());
377 Constant *CV = ConstantVector::get(Tmp);
378 unsigned Align = HwLen;
379 SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
380 return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
381 MachinePointerInfo::getConstantPool(MF), Align);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000382 }
383
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000384 // Construct two halves in parallel, then or them together.
385 assert(4*Words.size() == Subtarget.getVectorLength());
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000386 SDValue HalfV0 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
387 SDValue HalfV1 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000388 SDValue S = DAG.getConstant(4, dl, MVT::i32);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000389 for (unsigned i = 0; i != NumWords/2; ++i) {
390 SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
391 {HalfV0, Words[i]});
392 SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
393 {HalfV1, Words[i+NumWords/2]});
394 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
395 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
396 }
397
398 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
399 {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
400 SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
401 return DstV;
402}
403
404SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000405HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
406 unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
407 MVT PredTy = ty(PredV);
408 unsigned HwLen = Subtarget.getVectorLength();
409 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
410
411 if (Subtarget.isHVXVectorType(PredTy, true)) {
412 // Move the vector predicate SubV to a vector register, and scale it
413 // down to match the representation (bytes per type element) that VecV
414 // uses. The scaling down will pick every 2nd or 4th (every Scale-th
Hiroshi Inoue0909ca12018-01-26 08:15:29 +0000415 // in general) element and put them at the front of the resulting
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000416 // vector. This subvector will then be inserted into the Q2V of VecV.
417 // To avoid having an operation that generates an illegal type (short
418 // vector), generate a full size vector.
419 //
420 SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
421 SmallVector<int,128> Mask(HwLen);
422 // Scale = BitBytes(PredV) / Given BitBytes.
423 unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
424 unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
425
426 for (unsigned i = 0; i != HwLen; ++i) {
427 unsigned Num = i % Scale;
428 unsigned Off = i / Scale;
429 Mask[BlockLen*Num + Off] = i;
430 }
431 SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
432 if (!ZeroFill)
433 return S;
434 // Fill the bytes beyond BlockLen with 0s.
435 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000436 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
437 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000438 SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
439 return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
440 }
441
442 // Make sure that this is a valid scalar predicate.
443 assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
444
445 unsigned Bytes = 8 / PredTy.getVectorNumElements();
446 SmallVector<SDValue,4> Words[2];
447 unsigned IdxW = 0;
448
449 auto Lo32 = [&DAG, &dl] (SDValue P) {
450 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, P);
451 };
452 auto Hi32 = [&DAG, &dl] (SDValue P) {
453 return DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, P);
454 };
455
456 SDValue W0 = isUndef(PredV)
457 ? DAG.getUNDEF(MVT::i64)
458 : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
459 Words[IdxW].push_back(Hi32(W0));
460 Words[IdxW].push_back(Lo32(W0));
461
462 while (Bytes < BitBytes) {
463 IdxW ^= 1;
464 Words[IdxW].clear();
465
466 if (Bytes < 4) {
467 for (const SDValue &W : Words[IdxW ^ 1]) {
468 SDValue T = expandPredicate(W, dl, DAG);
469 Words[IdxW].push_back(Hi32(T));
470 Words[IdxW].push_back(Lo32(T));
471 }
472 } else {
473 for (const SDValue &W : Words[IdxW ^ 1]) {
474 Words[IdxW].push_back(W);
475 Words[IdxW].push_back(W);
476 }
477 }
478 Bytes *= 2;
479 }
480
481 assert(Bytes == BitBytes);
482
483 SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
484 SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
485 for (const SDValue &W : Words[IdxW]) {
486 Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
487 Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
488 }
489
490 return Vec;
491}
492
493SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000494HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
495 const SDLoc &dl, MVT VecTy,
496 SelectionDAG &DAG) const {
497 // Construct a vector V of bytes, such that a comparison V >u 0 would
498 // produce the required vector predicate.
499 unsigned VecLen = Values.size();
500 unsigned HwLen = Subtarget.getVectorLength();
501 assert(VecLen <= HwLen || VecLen == 8*HwLen);
502 SmallVector<SDValue,128> Bytes;
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000503 bool AllT = true, AllF = true;
504
505 auto IsTrue = [] (SDValue V) {
506 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
507 return !N->isNullValue();
508 return false;
509 };
510 auto IsFalse = [] (SDValue V) {
511 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
512 return N->isNullValue();
513 return false;
514 };
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000515
516 if (VecLen <= HwLen) {
517 // In the hardware, each bit of a vector predicate corresponds to a byte
518 // of a vector register. Calculate how many bytes does a bit of VecTy
519 // correspond to.
520 assert(HwLen % VecLen == 0);
521 unsigned BitBytes = HwLen / VecLen;
522 for (SDValue V : Values) {
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000523 AllT &= IsTrue(V);
524 AllF &= IsFalse(V);
525
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000526 SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000527 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000528 for (unsigned B = 0; B != BitBytes; ++B)
529 Bytes.push_back(Ext);
530 }
531 } else {
532 // There are as many i1 values, as there are bits in a vector register.
533 // Divide the values into groups of 8 and check that each group consists
534 // of the same value (ignoring undefs).
535 for (unsigned I = 0; I != VecLen; I += 8) {
536 unsigned B = 0;
537 // Find the first non-undef value in this group.
538 for (; B != 8; ++B) {
539 if (!Values[I+B].isUndef())
540 break;
541 }
542 SDValue F = Values[I+B];
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000543 AllT &= IsTrue(F);
544 AllF &= IsFalse(F);
545
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000546 SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000547 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000548 Bytes.push_back(Ext);
549 // Verify that the rest of values in the group are the same as the
550 // first.
551 for (; B != 8; ++B)
552 assert(Values[I+B].isUndef() || Values[I+B] == F);
553 }
554 }
555
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000556 if (AllT)
557 return DAG.getNode(HexagonISD::QTRUE, dl, VecTy);
558 if (AllF)
559 return DAG.getNode(HexagonISD::QFALSE, dl, VecTy);
560
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000561 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000562 SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
563 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
564}
565
566SDValue
567HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
568 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
569 MVT ElemTy = ty(VecV).getVectorElementType();
570
571 unsigned ElemWidth = ElemTy.getSizeInBits();
572 assert(ElemWidth >= 8 && ElemWidth <= 32);
573 (void)ElemWidth;
574
575 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
576 SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
577 {VecV, ByteIdx});
578 if (ElemTy == MVT::i32)
579 return ExWord;
580
581 // Have an extracted word, need to extract the smaller element out of it.
582 // 1. Extract the bits of (the original) IdxV that correspond to the index
583 // of the desired element in the 32-bit word.
584 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
585 // 2. Extract the element from the word.
586 SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
587 return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
588}
589
590SDValue
591HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
592 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
593 // Implement other return types if necessary.
594 assert(ResTy == MVT::i1);
595
596 unsigned HwLen = Subtarget.getVectorLength();
597 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
598 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
599
600 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
601 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
602 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
603
604 SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
605 SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000606 return getInstr(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000607}
608
609SDValue
610HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
611 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
612 MVT ElemTy = ty(VecV).getVectorElementType();
613
614 unsigned ElemWidth = ElemTy.getSizeInBits();
615 assert(ElemWidth >= 8 && ElemWidth <= 32);
616 (void)ElemWidth;
617
618 auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
619 SDValue ByteIdxV) {
620 MVT VecTy = ty(VecV);
621 unsigned HwLen = Subtarget.getVectorLength();
622 SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
623 {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
624 SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
625 SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
626 SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
627 {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
628 SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
629 return TorV;
630 };
631
632 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
633 if (ElemTy == MVT::i32)
634 return InsertWord(VecV, ValV, ByteIdx);
635
636 // If this is not inserting a 32-bit word, convert it into such a thing.
637 // 1. Extract the existing word from the target vector.
638 SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
639 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
640 SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
641 dl, MVT::i32, DAG);
642
643 // 2. Treating the extracted word as a 32-bit vector, insert the given
644 // value into it.
645 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
646 MVT SubVecTy = tyVector(ty(Ext), ElemTy);
647 SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
648 ValV, SubIdx, dl, ElemTy, DAG);
649
650 // 3. Insert the 32-bit word back into the original vector.
651 return InsertWord(VecV, Ins, ByteIdx);
652}
653
654SDValue
655HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
656 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
657 unsigned HwLen = Subtarget.getVectorLength();
658 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
659 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
660
661 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
662 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
663 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
664 ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
665
666 SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
667 return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
668}
669
670SDValue
671HexagonTargetLowering::extractHvxSubvectorReg(SDValue VecV, SDValue IdxV,
672 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
673 MVT VecTy = ty(VecV);
674 unsigned HwLen = Subtarget.getVectorLength();
675 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
676 MVT ElemTy = VecTy.getVectorElementType();
677 unsigned ElemWidth = ElemTy.getSizeInBits();
678
679 // If the source vector is a vector pair, get the single vector containing
680 // the subvector of interest. The subvector will never overlap two single
681 // vectors.
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000682 if (isHvxPairTy(VecTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000683 unsigned SubIdx;
684 if (Idx * ElemWidth >= 8*HwLen) {
685 SubIdx = Hexagon::vsub_hi;
686 Idx -= VecTy.getVectorNumElements() / 2;
687 } else {
688 SubIdx = Hexagon::vsub_lo;
689 }
690 VecTy = typeSplit(VecTy).first;
691 VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
692 if (VecTy == ResTy)
693 return VecV;
694 }
695
696 // The only meaningful subvectors of a single HVX vector are those that
697 // fit in a scalar register.
698 assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
699
700 MVT WordTy = tyVector(VecTy, MVT::i32);
701 SDValue WordVec = DAG.getBitcast(WordTy, VecV);
702 unsigned WordIdx = (Idx*ElemWidth) / 32;
703
704 SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
705 SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
706 if (ResTy.getSizeInBits() == 32)
707 return DAG.getBitcast(ResTy, W0);
708
709 SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
710 SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
711 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64, {W1, W0});
712 return DAG.getBitcast(ResTy, WW);
713}
714
715SDValue
716HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
717 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
718 MVT VecTy = ty(VecV);
719 unsigned HwLen = Subtarget.getVectorLength();
720 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
721 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
722 // IdxV is required to be a constant.
723 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
724
725 unsigned ResLen = ResTy.getVectorNumElements();
726 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
727 unsigned Offset = Idx * BitBytes;
728 SDValue Undef = DAG.getUNDEF(ByteTy);
729 SmallVector<int,128> Mask;
730
731 if (Subtarget.isHVXVectorType(ResTy, true)) {
732 // Converting between two vector predicates. Since the result is shorter
733 // than the source, it will correspond to a vector predicate with the
734 // relevant bits replicated. The replication count is the ratio of the
735 // source and target vector lengths.
736 unsigned Rep = VecTy.getVectorNumElements() / ResLen;
737 assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
738 for (unsigned i = 0; i != HwLen/Rep; ++i) {
739 for (unsigned j = 0; j != Rep; ++j)
740 Mask.push_back(i + Offset);
741 }
742 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
743 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
744 }
745
746 // Converting between a vector predicate and a scalar predicate. In the
747 // vector predicate, a group of BitBytes bits will correspond to a single
748 // i1 element of the source vector type. Those bits will all have the same
749 // value. The same will be true for ByteVec, where each byte corresponds
750 // to a bit in the vector predicate.
751 // The algorithm is to traverse the ByteVec, going over the i1 values from
752 // the source vector, and generate the corresponding representation in an
753 // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
754 // elements so that the interesting 8 bytes will be in the low end of the
755 // vector.
756 unsigned Rep = 8 / ResLen;
757 // Make sure the output fill the entire vector register, so repeat the
758 // 8-byte groups as many times as necessary.
759 for (unsigned r = 0; r != HwLen/ResLen; ++r) {
760 // This will generate the indexes of the 8 interesting bytes.
761 for (unsigned i = 0; i != ResLen; ++i) {
762 for (unsigned j = 0; j != Rep; ++j)
763 Mask.push_back(Offset + i*BitBytes);
764 }
765 }
766
767 SDValue Zero = getZero(dl, MVT::i32, DAG);
768 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
769 // Combine the two low words from ShuffV into a v8i8, and byte-compare
770 // them against 0.
771 SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
772 SDValue W1 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
773 {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
774 SDValue Vec64 = DAG.getNode(HexagonISD::COMBINE, dl, MVT::v8i8, {W1, W0});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000775 return getInstr(Hexagon::A4_vcmpbgtui, dl, ResTy,
776 {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000777}
778
779SDValue
780HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
781 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
782 MVT VecTy = ty(VecV);
783 MVT SubTy = ty(SubV);
784 unsigned HwLen = Subtarget.getVectorLength();
785 MVT ElemTy = VecTy.getVectorElementType();
786 unsigned ElemWidth = ElemTy.getSizeInBits();
787
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000788 bool IsPair = isHvxPairTy(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000789 MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
790 // The two single vectors that VecV consists of, if it's a pair.
791 SDValue V0, V1;
792 SDValue SingleV = VecV;
793 SDValue PickHi;
794
795 if (IsPair) {
796 V0 = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, VecV);
797 V1 = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, VecV);
798
799 SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
800 dl, MVT::i32);
801 PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000802 if (isHvxSingleTy(SubTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000803 if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
804 unsigned Idx = CN->getZExtValue();
805 assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
806 unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
807 return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
808 }
809 // If IdxV is not a constant, generate the two variants: with the
810 // SubV as the high and as the low subregister, and select the right
811 // pair based on the IdxV.
812 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
813 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
814 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
815 }
816 // The subvector being inserted must be entirely contained in one of
817 // the vectors V0 or V1. Set SingleV to the correct one, and update
818 // IdxV to be the index relative to the beginning of that vector.
819 SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
820 IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
821 SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
822 }
823
824 // The only meaningful subvectors of a single HVX vector are those that
825 // fit in a scalar register.
826 assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
827 // Convert IdxV to be index in bytes.
828 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
829 if (!IdxN || !IdxN->isNullValue()) {
830 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
831 DAG.getConstant(ElemWidth/8, dl, MVT::i32));
832 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
833 }
834 // When inserting a single word, the rotation back to the original position
835 // would be by HwLen-Idx, but if two words are inserted, it will need to be
836 // by (HwLen-4)-Idx.
837 unsigned RolBase = HwLen;
838 if (VecTy.getSizeInBits() == 32) {
839 SDValue V = DAG.getBitcast(MVT::i32, SubV);
840 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, V);
841 } else {
842 SDValue V = DAG.getBitcast(MVT::i64, SubV);
843 SDValue R0 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, V);
844 SDValue R1 = DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, V);
845 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
846 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
847 DAG.getConstant(4, dl, MVT::i32));
848 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
849 RolBase = HwLen-4;
850 }
851 // If the vector wasn't ror'ed, don't ror it back.
852 if (RolBase != 4 || !IdxN || !IdxN->isNullValue()) {
853 SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
854 DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
855 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
856 }
857
858 if (IsPair) {
859 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
860 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
861 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
862 }
863 return SingleV;
864}
865
866SDValue
867HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
868 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
869 MVT VecTy = ty(VecV);
870 MVT SubTy = ty(SubV);
871 assert(Subtarget.isHVXVectorType(VecTy, true));
872 // VecV is an HVX vector predicate. SubV may be either an HVX vector
873 // predicate as well, or it can be a scalar predicate.
874
875 unsigned VecLen = VecTy.getVectorNumElements();
876 unsigned HwLen = Subtarget.getVectorLength();
877 assert(HwLen % VecLen == 0 && "Unexpected vector type");
878
879 unsigned Scale = VecLen / SubTy.getVectorNumElements();
880 unsigned BitBytes = HwLen / VecLen;
881 unsigned BlockLen = HwLen / Scale;
882
883 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
884 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
885 SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
886 SDValue ByteIdx;
887
888 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
889 if (!IdxN || !IdxN->isNullValue()) {
890 ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
891 DAG.getConstant(BitBytes, dl, MVT::i32));
892 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
893 }
894
895 // ByteVec is the target vector VecV rotated in such a way that the
896 // subvector should be inserted at index 0. Generate a predicate mask
897 // and use vmux to do the insertion.
898 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000899 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
900 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
901 ByteVec = getInstr(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000902 // Rotate ByteVec back, and convert to a vector predicate.
903 if (!IdxN || !IdxN->isNullValue()) {
904 SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
905 SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
906 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
907 }
908 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
909}
910
911SDValue
912HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
913 MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
914 // Sign- and any-extending of a vector predicate to a vector register is
915 // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
916 // a vector of 1s (where the 1s are of type matching the vector type).
917 assert(Subtarget.isHVXVectorType(ResTy));
918 if (!ZeroExt)
919 return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
920
921 assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
922 SDValue True = DAG.getNode(HexagonISD::VSPLAT, dl, ResTy,
923 DAG.getConstant(1, dl, MVT::i32));
924 SDValue False = getZero(dl, ResTy, DAG);
925 return DAG.getSelect(dl, ResTy, VecV, True, False);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000926}
927
928SDValue
929HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
930 const {
931 const SDLoc &dl(Op);
932 MVT VecTy = ty(Op);
933
934 unsigned Size = Op.getNumOperands();
935 SmallVector<SDValue,128> Ops;
936 for (unsigned i = 0; i != Size; ++i)
937 Ops.push_back(Op.getOperand(i));
938
939 if (VecTy.getVectorElementType() == MVT::i1)
940 return buildHvxVectorPred(Ops, dl, VecTy, DAG);
941
942 if (VecTy.getSizeInBits() == 16*Subtarget.getVectorLength()) {
943 ArrayRef<SDValue> A(Ops);
944 MVT SingleTy = typeSplit(VecTy).first;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000945 SDValue V0 = buildHvxVectorReg(A.take_front(Size/2), dl, SingleTy, DAG);
946 SDValue V1 = buildHvxVectorReg(A.drop_front(Size/2), dl, SingleTy, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000947 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
948 }
949
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000950 return buildHvxVectorReg(Ops, dl, VecTy, DAG);
951}
952
953SDValue
954HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
955 const {
Krzysztof Parzyszek97a50952018-02-06 20:18:58 +0000956 // Vector concatenation of two integer (non-bool) vectors does not need
957 // special lowering. Custom-lower concats of bool vectors and expand
958 // concats of more than 2 vectors.
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000959 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000960 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000961 unsigned NumOp = Op.getNumOperands();
Krzysztof Parzyszek97a50952018-02-06 20:18:58 +0000962 if (VecTy.getVectorElementType() != MVT::i1) {
963 if (NumOp == 2)
964 return Op;
965 // Expand the other cases into a build-vector.
966 SmallVector<SDValue,8> Elems;
967 for (SDValue V : Op.getNode()->ops())
968 DAG.ExtractVectorElements(V, Elems);
969 return DAG.getBuildVector(VecTy, dl, Elems);
970 }
971
972 assert(VecTy.getVectorElementType() == MVT::i1);
973 unsigned HwLen = Subtarget.getVectorLength();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000974 assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +0000975
976 SDValue Op0 = Op.getOperand(0);
977
978 // If the operands are HVX types (i.e. not scalar predicates), then
979 // defer the concatenation, and create QCAT instead.
980 if (Subtarget.isHVXVectorType(ty(Op0), true)) {
981 if (NumOp == 2)
982 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, Op0, Op.getOperand(1));
983
984 ArrayRef<SDUse> U(Op.getNode()->ops());
985 SmallVector<SDValue,4> SV(U.begin(), U.end());
986 ArrayRef<SDValue> Ops(SV);
987
988 MVT HalfTy = typeSplit(VecTy).first;
989 SDValue V0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
990 Ops.take_front(NumOp/2));
991 SDValue V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
992 Ops.take_back(NumOp/2));
993 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, V0, V1);
994 }
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000995
996 // Count how many bytes (in a vector register) each bit in VecTy
997 // corresponds to.
998 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
999
1000 SmallVector<SDValue,8> Prefixes;
1001 for (SDValue V : Op.getNode()->op_values()) {
1002 SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
1003 Prefixes.push_back(P);
1004 }
1005
1006 unsigned InpLen = ty(Op.getOperand(0)).getVectorNumElements();
1007 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1008 SDValue S = DAG.getConstant(InpLen*BitBytes, dl, MVT::i32);
1009 SDValue Res = getZero(dl, ByteTy, DAG);
1010 for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
1011 Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
1012 Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
1013 }
1014 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001015}
1016
1017SDValue
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001018HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
1019 const {
1020 // Change the type of the extracted element to i32.
1021 SDValue VecV = Op.getOperand(0);
1022 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001023 const SDLoc &dl(Op);
1024 SDValue IdxV = Op.getOperand(1);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001025 if (ElemTy == MVT::i1)
1026 return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001027
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001028 return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001029}
1030
1031SDValue
1032HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
1033 const {
1034 const SDLoc &dl(Op);
1035 SDValue VecV = Op.getOperand(0);
1036 SDValue ValV = Op.getOperand(1);
1037 SDValue IdxV = Op.getOperand(2);
1038 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001039 if (ElemTy == MVT::i1)
1040 return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001041
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001042 return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001043}
1044
1045SDValue
1046HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
1047 const {
1048 SDValue SrcV = Op.getOperand(0);
1049 MVT SrcTy = ty(SrcV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001050 MVT DstTy = ty(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001051 SDValue IdxV = Op.getOperand(1);
1052 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001053 assert(Idx % DstTy.getVectorNumElements() == 0);
1054 (void)Idx;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001055 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001056
1057 MVT ElemTy = SrcTy.getVectorElementType();
1058 if (ElemTy == MVT::i1)
1059 return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
1060
1061 return extractHvxSubvectorReg(SrcV, IdxV, dl, DstTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001062}
1063
1064SDValue
1065HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
1066 const {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001067 // Idx does not need to be a constant.
1068 SDValue VecV = Op.getOperand(0);
1069 SDValue ValV = Op.getOperand(1);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001070 SDValue IdxV = Op.getOperand(2);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001071
1072 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001073 MVT VecTy = ty(VecV);
1074 MVT ElemTy = VecTy.getVectorElementType();
1075 if (ElemTy == MVT::i1)
1076 return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
1077
1078 return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001079}
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001080
1081SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001082HexagonTargetLowering::LowerHvxAnyExt(SDValue Op, SelectionDAG &DAG) const {
1083 // Lower any-extends of boolean vectors to sign-extends, since they
1084 // translate directly to Q2V. Zero-extending could also be done equally
1085 // fast, but Q2V is used/recognized in more places.
1086 // For all other vectors, use zero-extend.
1087 MVT ResTy = ty(Op);
1088 SDValue InpV = Op.getOperand(0);
1089 MVT ElemTy = ty(InpV).getVectorElementType();
1090 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1091 return LowerHvxSignExt(Op, DAG);
1092 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(Op), ResTy, InpV);
1093}
1094
1095SDValue
1096HexagonTargetLowering::LowerHvxSignExt(SDValue Op, SelectionDAG &DAG) const {
1097 MVT ResTy = ty(Op);
1098 SDValue InpV = Op.getOperand(0);
1099 MVT ElemTy = ty(InpV).getVectorElementType();
1100 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1101 return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), false, DAG);
1102 return Op;
1103}
1104
1105SDValue
1106HexagonTargetLowering::LowerHvxZeroExt(SDValue Op, SelectionDAG &DAG) const {
1107 MVT ResTy = ty(Op);
1108 SDValue InpV = Op.getOperand(0);
1109 MVT ElemTy = ty(InpV).getVectorElementType();
1110 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1111 return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), true, DAG);
1112 return Op;
1113}
1114
1115SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001116HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
1117 MVT ResTy = ty(Op);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001118 assert(ResTy.isVector() && isHvxSingleTy(ResTy));
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001119 const SDLoc &dl(Op);
1120 SmallVector<int,256> ShuffMask;
1121
1122 MVT ElemTy = ResTy.getVectorElementType();
1123 unsigned VecLen = ResTy.getVectorNumElements();
1124 SDValue Vs = Op.getOperand(0);
1125 SDValue Vt = Op.getOperand(1);
1126
1127 switch (ElemTy.SimpleTy) {
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +00001128 case MVT::i8: {
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001129 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1130 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1131 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001132 MVT ExtTy = typeExtElem(ResTy, 2);
1133 unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
1134 : Hexagon::V6_vmpyhv;
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001135 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001136
1137 // Discard high halves of the resulting values, collect the low halves.
1138 for (unsigned I = 0; I < VecLen; I += 2) {
1139 ShuffMask.push_back(I); // Pick even element.
1140 ShuffMask.push_back(I+VecLen); // Pick odd element.
1141 }
1142 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
Krzysztof Parzyszek0f5d9762018-01-05 20:45:34 +00001143 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1144 return DAG.getBitcast(ResTy, BS);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001145 }
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +00001146 case MVT::i16:
1147 // For i16 there is V6_vmpyih, which acts exactly like the MUL opcode.
1148 // (There is also V6_vmpyhv, which behaves in an analogous way to
1149 // V6_vmpybv.)
1150 return getInstr(Hexagon::V6_vmpyih, dl, ResTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001151 case MVT::i32: {
1152 // Use the following sequence for signed word multiply:
1153 // T0 = V6_vmpyiowh Vs, Vt
1154 // T1 = V6_vaslw T0, 16
1155 // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
1156 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001157 SDValue T0 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
1158 SDValue T1 = getInstr(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
1159 SDValue T2 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1160 {T1, Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001161 return T2;
1162 }
1163 default:
1164 break;
1165 }
1166 return SDValue();
1167}
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001168
1169SDValue
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001170HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
1171 MVT ResTy = ty(Op);
1172 assert(ResTy.isVector());
1173 const SDLoc &dl(Op);
1174 SmallVector<int,256> ShuffMask;
1175
1176 MVT ElemTy = ResTy.getVectorElementType();
1177 unsigned VecLen = ResTy.getVectorNumElements();
1178 SDValue Vs = Op.getOperand(0);
1179 SDValue Vt = Op.getOperand(1);
1180 bool IsSigned = Op.getOpcode() == ISD::MULHS;
1181
1182 if (ElemTy == MVT::i8 || ElemTy == MVT::i16) {
1183 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1184 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1185 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
1186 // For i16, use V6_vmpyhv, which behaves in an analogous way to
1187 // V6_vmpybv: results Lo and Hi are products of even/odd elements
1188 // respectively.
1189 MVT ExtTy = typeExtElem(ResTy, 2);
1190 unsigned MpyOpc = ElemTy == MVT::i8
1191 ? (IsSigned ? Hexagon::V6_vmpybv : Hexagon::V6_vmpyubv)
1192 : (IsSigned ? Hexagon::V6_vmpyhv : Hexagon::V6_vmpyuhv);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001193 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001194
1195 // Discard low halves of the resulting values, collect the high halves.
1196 for (unsigned I = 0; I < VecLen; I += 2) {
1197 ShuffMask.push_back(I+1); // Pick even element.
1198 ShuffMask.push_back(I+VecLen+1); // Pick odd element.
1199 }
1200 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
1201 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1202 return DAG.getBitcast(ResTy, BS);
1203 }
1204
1205 assert(ElemTy == MVT::i32);
1206 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
1207
1208 if (IsSigned) {
1209 // mulhs(Vs,Vt) =
1210 // = [(Hi(Vs)*2^16 + Lo(Vs)) *s (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1211 // = [Hi(Vs)*2^16 *s Hi(Vt)*2^16 + Hi(Vs) *su Lo(Vt)*2^16
1212 // + Lo(Vs) *us (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1213 // = [Hi(Vs) *s Hi(Vt)*2^32 + Hi(Vs) *su Lo(Vt)*2^16
1214 // + Lo(Vs) *us Vt] >> 32
1215 // The low half of Lo(Vs)*Lo(Vt) will be discarded (it's not added to
1216 // anything, so it cannot produce any carry over to higher bits),
1217 // so everything in [] can be shifted by 16 without loss of precision.
1218 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + Lo(Vs)*Vt >> 16] >> 16
1219 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + V6_vmpyewuh(Vs,Vt)] >> 16
1220 // Denote Hi(Vs) = Vs':
1221 // = [Vs'*s Hi(Vt)*2^16 + Vs' *su Lo(Vt) + V6_vmpyewuh(Vt,Vs)] >> 16
1222 // = Vs'*s Hi(Vt) + (V6_vmpyiewuh(Vs',Vt) + V6_vmpyewuh(Vt,Vs)) >> 16
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001223 SDValue T0 = getInstr(Hexagon::V6_vmpyewuh, dl, ResTy, {Vt, Vs}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001224 // Get Vs':
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001225 SDValue S0 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {Vs, S16}, DAG);
1226 SDValue T1 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1227 {T0, S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001228 // Shift by 16:
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001229 SDValue S2 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {T1, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001230 // Get Vs'*Hi(Vt):
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001231 SDValue T2 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001232 // Add:
1233 SDValue T3 = DAG.getNode(ISD::ADD, dl, ResTy, {S2, T2});
1234 return T3;
1235 }
1236
1237 // Unsigned mulhw. (Would expansion using signed mulhw be better?)
1238
1239 auto LoVec = [&DAG,ResTy,dl] (SDValue Pair) {
1240 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResTy, Pair);
1241 };
1242 auto HiVec = [&DAG,ResTy,dl] (SDValue Pair) {
1243 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResTy, Pair);
1244 };
1245
1246 MVT PairTy = typeJoin({ResTy, ResTy});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001247 SDValue P = getInstr(Hexagon::V6_lvsplatw, dl, ResTy,
1248 {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001249 // Multiply-unsigned halfwords:
1250 // LoVec = Vs.uh[2i] * Vt.uh[2i],
1251 // HiVec = Vs.uh[2i+1] * Vt.uh[2i+1]
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001252 SDValue T0 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001253 // The low halves in the LoVec of the pair can be discarded. They are
1254 // not added to anything (in the full-precision product), so they cannot
1255 // produce a carry into the higher bits.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001256 SDValue T1 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {LoVec(T0), S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001257 // Swap low and high halves in Vt, and do the halfword multiplication
1258 // to get products Vs.uh[2i] * Vt.uh[2i+1] and Vs.uh[2i+1] * Vt.uh[2i].
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001259 SDValue D0 = getInstr(Hexagon::V6_vdelta, dl, ResTy, {Vt, P}, DAG);
1260 SDValue T2 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, D0}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001261 // T2 has mixed products of halfwords: Lo(Vt)*Hi(Vs) and Hi(Vt)*Lo(Vs).
1262 // These products are words, but cannot be added directly because the
1263 // sums could overflow. Add these products, by halfwords, where each sum
1264 // of a pair of halfwords gives a word.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001265 SDValue T3 = getInstr(Hexagon::V6_vadduhw, dl, PairTy,
1266 {LoVec(T2), HiVec(T2)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001267 // Add the high halfwords from the products of the low halfwords.
1268 SDValue T4 = DAG.getNode(ISD::ADD, dl, ResTy, {T1, LoVec(T3)});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001269 SDValue T5 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {T4, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001270 SDValue T6 = DAG.getNode(ISD::ADD, dl, ResTy, {HiVec(T0), HiVec(T3)});
1271 SDValue T7 = DAG.getNode(ISD::ADD, dl, ResTy, {T5, T6});
1272 return T7;
1273}
1274
1275SDValue
Krzysztof Parzyszek6b589e52017-12-18 18:32:27 +00001276HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
1277 // Sign- and zero-extends are legal.
1278 assert(Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG);
1279 return DAG.getZeroExtendVectorInReg(Op.getOperand(0), SDLoc(Op), ty(Op));
1280}
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001281
1282SDValue
1283HexagonTargetLowering::LowerHvxShift(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001284 if (SDValue S = getVectorShiftByInt(Op, DAG))
1285 return S;
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001286 return Op;
1287}
1288
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001289SDValue
1290HexagonTargetLowering::SplitHvxPairOp(SDValue Op, SelectionDAG &DAG) const {
1291 assert(!Op.isMachineOpcode());
1292 SmallVector<SDValue,2> OpsL, OpsH;
1293 const SDLoc &dl(Op);
1294
1295 auto SplitVTNode = [&DAG,this] (const VTSDNode *N) {
1296 MVT Ty = typeSplit(N->getVT().getSimpleVT()).first;
1297 SDValue TV = DAG.getValueType(Ty);
1298 return std::make_pair(TV, TV);
1299 };
1300
1301 for (SDValue A : Op.getNode()->ops()) {
1302 VectorPair P = Subtarget.isHVXVectorType(ty(A), true)
1303 ? opSplit(A, dl, DAG)
1304 : std::make_pair(A, A);
1305 // Special case for type operand.
1306 if (Op.getOpcode() == ISD::SIGN_EXTEND_INREG) {
1307 if (const auto *N = dyn_cast<const VTSDNode>(A.getNode()))
1308 P = SplitVTNode(N);
1309 }
1310 OpsL.push_back(P.first);
1311 OpsH.push_back(P.second);
1312 }
1313
1314 MVT ResTy = ty(Op);
1315 MVT HalfTy = typeSplit(ResTy).first;
1316 SDValue L = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsL);
1317 SDValue H = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsH);
1318 SDValue S = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, L, H);
1319 return S;
1320}
1321
1322SDValue
1323HexagonTargetLowering::LowerHvxOperation(SDValue Op, SelectionDAG &DAG) const {
1324 unsigned Opc = Op.getOpcode();
1325 bool IsPairOp = isHvxPairTy(ty(Op)) ||
1326 llvm::any_of(Op.getNode()->ops(), [this] (SDValue V) {
1327 return isHvxPairTy(ty(V));
1328 });
1329
1330 if (IsPairOp) {
1331 switch (Opc) {
1332 default:
1333 break;
1334 case ISD::MUL:
1335 case ISD::MULHS:
1336 case ISD::MULHU:
1337 case ISD::AND:
1338 case ISD::OR:
1339 case ISD::XOR:
1340 case ISD::SRA:
1341 case ISD::SHL:
1342 case ISD::SRL:
1343 case ISD::SETCC:
1344 case ISD::VSELECT:
1345 case ISD::SIGN_EXTEND_INREG:
1346 return SplitHvxPairOp(Op, DAG);
1347 }
1348 }
1349
1350 switch (Opc) {
1351 default:
1352 break;
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001353 case ISD::BUILD_VECTOR: return LowerHvxBuildVector(Op, DAG);
1354 case ISD::CONCAT_VECTORS: return LowerHvxConcatVectors(Op, DAG);
1355 case ISD::INSERT_SUBVECTOR: return LowerHvxInsertSubvector(Op, DAG);
1356 case ISD::INSERT_VECTOR_ELT: return LowerHvxInsertElement(Op, DAG);
1357 case ISD::EXTRACT_SUBVECTOR: return LowerHvxExtractSubvector(Op, DAG);
1358 case ISD::EXTRACT_VECTOR_ELT: return LowerHvxExtractElement(Op, DAG);
1359
1360 case ISD::ANY_EXTEND: return LowerHvxAnyExt(Op, DAG);
1361 case ISD::SIGN_EXTEND: return LowerHvxSignExt(Op, DAG);
1362 case ISD::ZERO_EXTEND: return LowerHvxZeroExt(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001363 case ISD::SRA:
1364 case ISD::SHL:
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001365 case ISD::SRL: return LowerHvxShift(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001366 case ISD::MUL: return LowerHvxMul(Op, DAG);
1367 case ISD::MULHS:
1368 case ISD::MULHU: return LowerHvxMulh(Op, DAG);
1369 case ISD::ANY_EXTEND_VECTOR_INREG: return LowerHvxExtend(Op, DAG);
1370 case ISD::SETCC:
1371 case ISD::INTRINSIC_VOID: return Op;
1372 }
1373#ifndef NDEBUG
1374 Op.dumpr(&DAG);
1375#endif
1376 llvm_unreachable("Unhandled HVX operation");
1377}
1378
1379bool
1380HexagonTargetLowering::isHvxOperation(SDValue Op) const {
1381 // If the type of the result, or any operand type are HVX vector types,
1382 // this is an HVX operation.
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001383 return Subtarget.isHVXVectorType(ty(Op), true) ||
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001384 llvm::any_of(Op.getNode()->ops(),
1385 [this] (SDValue V) {
1386 return Subtarget.isHVXVectorType(ty(V), true);
1387 });
1388}