blob: c026923645f5796ee0a56c5a1afb46782268a366 [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
16SDValue
17HexagonTargetLowering::getInt(unsigned IntId, MVT ResTy, ArrayRef<SDValue> Ops,
18 const SDLoc &dl, SelectionDAG &DAG) const {
19 SmallVector<SDValue,4> IntOps;
20 IntOps.push_back(DAG.getConstant(IntId, dl, MVT::i32));
21 for (const SDValue &Op : Ops)
22 IntOps.push_back(Op);
23 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, ResTy, IntOps);
24}
25
26MVT
27HexagonTargetLowering::typeJoin(const TypePair &Tys) const {
28 assert(Tys.first.getVectorElementType() == Tys.second.getVectorElementType());
29
30 MVT ElemTy = Tys.first.getVectorElementType();
31 return MVT::getVectorVT(ElemTy, Tys.first.getVectorNumElements() +
32 Tys.second.getVectorNumElements());
33}
34
35HexagonTargetLowering::TypePair
36HexagonTargetLowering::typeSplit(MVT VecTy) const {
37 assert(VecTy.isVector());
38 unsigned NumElem = VecTy.getVectorNumElements();
39 assert((NumElem % 2) == 0 && "Expecting even-sized vector type");
40 MVT HalfTy = MVT::getVectorVT(VecTy.getVectorElementType(), NumElem/2);
41 return { HalfTy, HalfTy };
42}
43
44MVT
45HexagonTargetLowering::typeExtElem(MVT VecTy, unsigned Factor) const {
46 MVT ElemTy = VecTy.getVectorElementType();
47 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() * Factor);
48 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
49}
50
51MVT
52HexagonTargetLowering::typeTruncElem(MVT VecTy, unsigned Factor) const {
53 MVT ElemTy = VecTy.getVectorElementType();
54 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() / Factor);
55 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
56}
57
58SDValue
59HexagonTargetLowering::opCastElem(SDValue Vec, MVT ElemTy,
60 SelectionDAG &DAG) const {
61 if (ty(Vec).getVectorElementType() == ElemTy)
62 return Vec;
63 MVT CastTy = tyVector(Vec.getValueType().getSimpleVT(), ElemTy);
64 return DAG.getBitcast(CastTy, Vec);
65}
66
67SDValue
68HexagonTargetLowering::opJoin(const VectorPair &Ops, const SDLoc &dl,
69 SelectionDAG &DAG) const {
70 return DAG.getNode(ISD::CONCAT_VECTORS, dl, typeJoin(ty(Ops)),
71 Ops.second, Ops.first);
72}
73
74HexagonTargetLowering::VectorPair
75HexagonTargetLowering::opSplit(SDValue Vec, const SDLoc &dl,
76 SelectionDAG &DAG) const {
77 TypePair Tys = typeSplit(ty(Vec));
78 return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
79}
80
81SDValue
82HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
83 SelectionDAG &DAG) const {
84 if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
85 ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
86
87 unsigned ElemWidth = ElemTy.getSizeInBits();
88 if (ElemWidth == 8)
89 return ElemIdx;
90
91 unsigned L = Log2_32(ElemWidth/8);
92 const SDLoc &dl(ElemIdx);
93 return DAG.getNode(ISD::SHL, dl, MVT::i32,
94 {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
95}
96
97SDValue
98HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
99 SelectionDAG &DAG) const {
100 unsigned ElemWidth = ElemTy.getSizeInBits();
101 assert(ElemWidth >= 8 && ElemWidth <= 32);
102 if (ElemWidth == 32)
103 return Idx;
104
105 if (ty(Idx) != MVT::i32)
106 Idx = DAG.getBitcast(MVT::i32, Idx);
107 const SDLoc &dl(Idx);
108 SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
109 SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
110 return SubIdx;
111}
112
113SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000114HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
115 SDValue Op1, ArrayRef<int> Mask,
116 SelectionDAG &DAG) const {
117 MVT OpTy = ty(Op0);
118 assert(OpTy == ty(Op1));
119
120 MVT ElemTy = OpTy.getVectorElementType();
121 if (ElemTy == MVT::i8)
122 return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
123 assert(ElemTy.getSizeInBits() >= 8);
124
125 MVT ResTy = tyVector(OpTy, MVT::i8);
126 unsigned ElemSize = ElemTy.getSizeInBits() / 8;
127
128 SmallVector<int,128> ByteMask;
129 for (int M : Mask) {
130 if (M < 0) {
131 for (unsigned I = 0; I != ElemSize; ++I)
132 ByteMask.push_back(-1);
133 } else {
134 int NewM = M*ElemSize;
135 for (unsigned I = 0; I != ElemSize; ++I)
136 ByteMask.push_back(NewM+I);
137 }
138 }
139 assert(ResTy.getVectorNumElements() == ByteMask.size());
140 return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
141 opCastElem(Op1, MVT::i8, DAG), ByteMask);
142}
143
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000144SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000145HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
146 const SDLoc &dl, MVT VecTy,
147 SelectionDAG &DAG) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000148 unsigned VecLen = Values.size();
149 MachineFunction &MF = DAG.getMachineFunction();
150 MVT ElemTy = VecTy.getVectorElementType();
151 unsigned ElemWidth = ElemTy.getSizeInBits();
152 unsigned HwLen = Subtarget.getVectorLength();
153
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000154 // TODO: Recognize constant splats.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000155 SmallVector<ConstantInt*, 128> Consts(VecLen);
156 bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
157 if (AllConst) {
158 if (llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
159 return getZero(dl, VecTy, DAG);
160
161 ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
162 (Constant**)Consts.end());
163 Constant *CV = ConstantVector::get(Tmp);
164 unsigned Align = HwLen;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000165 SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
166 return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
167 MachinePointerInfo::getConstantPool(MF), Align);
168 }
169
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000170 unsigned ElemSize = ElemWidth / 8;
171 assert(ElemSize*VecLen == HwLen);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000172 SmallVector<SDValue,32> Words;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000173
174 if (VecTy.getVectorElementType() != MVT::i32) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000175 assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
176 unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
177 MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000178 for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
179 SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000180 Words.push_back(DAG.getBitcast(MVT::i32, W));
181 }
182 } else {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000183 Words.assign(Values.begin(), Values.end());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000184 }
185
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000186 unsigned NumWords = Words.size();
Eric Christophera8bdf532018-01-24 01:51:57 +0000187 bool IsSplat = true;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000188 SDValue SplatV;
189 for (unsigned i = 0; i != NumWords && IsSplat; ++i) {
190 if (isUndef(Words[i]))
191 continue;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000192 if (!SplatV.getNode())
193 SplatV = Words[i];
194 else if (SplatV != Words[i])
195 IsSplat = false;
196 }
197 if (IsSplat) {
198 assert(SplatV.getNode());
199 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, SplatV);
200 }
201
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000202 // Construct two halves in parallel, then or them together.
203 assert(4*Words.size() == Subtarget.getVectorLength());
204 SDValue HalfV0 = getNode(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
205 SDValue HalfV1 = getNode(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
206 SDValue S = DAG.getConstant(4, dl, MVT::i32);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000207 for (unsigned i = 0; i != NumWords/2; ++i) {
208 SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
209 {HalfV0, Words[i]});
210 SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
211 {HalfV1, Words[i+NumWords/2]});
212 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
213 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
214 }
215
216 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
217 {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
218 SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
219 return DstV;
220}
221
222SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000223HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
224 unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
225 MVT PredTy = ty(PredV);
226 unsigned HwLen = Subtarget.getVectorLength();
227 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
228
229 if (Subtarget.isHVXVectorType(PredTy, true)) {
230 // Move the vector predicate SubV to a vector register, and scale it
231 // down to match the representation (bytes per type element) that VecV
232 // uses. The scaling down will pick every 2nd or 4th (every Scale-th
233 // in general) element and put them at at the front of the resulting
234 // vector. This subvector will then be inserted into the Q2V of VecV.
235 // To avoid having an operation that generates an illegal type (short
236 // vector), generate a full size vector.
237 //
238 SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
239 SmallVector<int,128> Mask(HwLen);
240 // Scale = BitBytes(PredV) / Given BitBytes.
241 unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
242 unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
243
244 for (unsigned i = 0; i != HwLen; ++i) {
245 unsigned Num = i % Scale;
246 unsigned Off = i / Scale;
247 Mask[BlockLen*Num + Off] = i;
248 }
249 SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
250 if (!ZeroFill)
251 return S;
252 // Fill the bytes beyond BlockLen with 0s.
253 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
254 SDValue Q = getNode(Hexagon::V6_pred_scalar2, dl, BoolTy,
255 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
256 SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
257 return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
258 }
259
260 // Make sure that this is a valid scalar predicate.
261 assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
262
263 unsigned Bytes = 8 / PredTy.getVectorNumElements();
264 SmallVector<SDValue,4> Words[2];
265 unsigned IdxW = 0;
266
267 auto Lo32 = [&DAG, &dl] (SDValue P) {
268 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, P);
269 };
270 auto Hi32 = [&DAG, &dl] (SDValue P) {
271 return DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, P);
272 };
273
274 SDValue W0 = isUndef(PredV)
275 ? DAG.getUNDEF(MVT::i64)
276 : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
277 Words[IdxW].push_back(Hi32(W0));
278 Words[IdxW].push_back(Lo32(W0));
279
280 while (Bytes < BitBytes) {
281 IdxW ^= 1;
282 Words[IdxW].clear();
283
284 if (Bytes < 4) {
285 for (const SDValue &W : Words[IdxW ^ 1]) {
286 SDValue T = expandPredicate(W, dl, DAG);
287 Words[IdxW].push_back(Hi32(T));
288 Words[IdxW].push_back(Lo32(T));
289 }
290 } else {
291 for (const SDValue &W : Words[IdxW ^ 1]) {
292 Words[IdxW].push_back(W);
293 Words[IdxW].push_back(W);
294 }
295 }
296 Bytes *= 2;
297 }
298
299 assert(Bytes == BitBytes);
300
301 SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
302 SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
303 for (const SDValue &W : Words[IdxW]) {
304 Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
305 Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
306 }
307
308 return Vec;
309}
310
311SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000312HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
313 const SDLoc &dl, MVT VecTy,
314 SelectionDAG &DAG) const {
315 // Construct a vector V of bytes, such that a comparison V >u 0 would
316 // produce the required vector predicate.
317 unsigned VecLen = Values.size();
318 unsigned HwLen = Subtarget.getVectorLength();
319 assert(VecLen <= HwLen || VecLen == 8*HwLen);
320 SmallVector<SDValue,128> Bytes;
321
322 if (VecLen <= HwLen) {
323 // In the hardware, each bit of a vector predicate corresponds to a byte
324 // of a vector register. Calculate how many bytes does a bit of VecTy
325 // correspond to.
326 assert(HwLen % VecLen == 0);
327 unsigned BitBytes = HwLen / VecLen;
328 for (SDValue V : Values) {
329 SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
330 : DAG.getConstant(0, dl, MVT::i8);
331 for (unsigned B = 0; B != BitBytes; ++B)
332 Bytes.push_back(Ext);
333 }
334 } else {
335 // There are as many i1 values, as there are bits in a vector register.
336 // Divide the values into groups of 8 and check that each group consists
337 // of the same value (ignoring undefs).
338 for (unsigned I = 0; I != VecLen; I += 8) {
339 unsigned B = 0;
340 // Find the first non-undef value in this group.
341 for (; B != 8; ++B) {
342 if (!Values[I+B].isUndef())
343 break;
344 }
345 SDValue F = Values[I+B];
346 SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
347 : DAG.getConstant(0, dl, MVT::i8);
348 Bytes.push_back(Ext);
349 // Verify that the rest of values in the group are the same as the
350 // first.
351 for (; B != 8; ++B)
352 assert(Values[I+B].isUndef() || Values[I+B] == F);
353 }
354 }
355
356 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000357 SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
358 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
359}
360
361SDValue
362HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
363 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
364 MVT ElemTy = ty(VecV).getVectorElementType();
365
366 unsigned ElemWidth = ElemTy.getSizeInBits();
367 assert(ElemWidth >= 8 && ElemWidth <= 32);
368 (void)ElemWidth;
369
370 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
371 SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
372 {VecV, ByteIdx});
373 if (ElemTy == MVT::i32)
374 return ExWord;
375
376 // Have an extracted word, need to extract the smaller element out of it.
377 // 1. Extract the bits of (the original) IdxV that correspond to the index
378 // of the desired element in the 32-bit word.
379 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
380 // 2. Extract the element from the word.
381 SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
382 return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
383}
384
385SDValue
386HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
387 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
388 // Implement other return types if necessary.
389 assert(ResTy == MVT::i1);
390
391 unsigned HwLen = Subtarget.getVectorLength();
392 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
393 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
394
395 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
396 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
397 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
398
399 SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
400 SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
401 return getNode(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
402}
403
404SDValue
405HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
406 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
407 MVT ElemTy = ty(VecV).getVectorElementType();
408
409 unsigned ElemWidth = ElemTy.getSizeInBits();
410 assert(ElemWidth >= 8 && ElemWidth <= 32);
411 (void)ElemWidth;
412
413 auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
414 SDValue ByteIdxV) {
415 MVT VecTy = ty(VecV);
416 unsigned HwLen = Subtarget.getVectorLength();
417 SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
418 {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
419 SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
420 SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
421 SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
422 {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
423 SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
424 return TorV;
425 };
426
427 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
428 if (ElemTy == MVT::i32)
429 return InsertWord(VecV, ValV, ByteIdx);
430
431 // If this is not inserting a 32-bit word, convert it into such a thing.
432 // 1. Extract the existing word from the target vector.
433 SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
434 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
435 SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
436 dl, MVT::i32, DAG);
437
438 // 2. Treating the extracted word as a 32-bit vector, insert the given
439 // value into it.
440 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
441 MVT SubVecTy = tyVector(ty(Ext), ElemTy);
442 SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
443 ValV, SubIdx, dl, ElemTy, DAG);
444
445 // 3. Insert the 32-bit word back into the original vector.
446 return InsertWord(VecV, Ins, ByteIdx);
447}
448
449SDValue
450HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
451 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
452 unsigned HwLen = Subtarget.getVectorLength();
453 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
454 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
455
456 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
457 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
458 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
459 ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
460
461 SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
462 return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
463}
464
465SDValue
466HexagonTargetLowering::extractHvxSubvectorReg(SDValue VecV, SDValue IdxV,
467 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
468 MVT VecTy = ty(VecV);
469 unsigned HwLen = Subtarget.getVectorLength();
470 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
471 MVT ElemTy = VecTy.getVectorElementType();
472 unsigned ElemWidth = ElemTy.getSizeInBits();
473
474 // If the source vector is a vector pair, get the single vector containing
475 // the subvector of interest. The subvector will never overlap two single
476 // vectors.
477 if (VecTy.getSizeInBits() == 16*HwLen) {
478 unsigned SubIdx;
479 if (Idx * ElemWidth >= 8*HwLen) {
480 SubIdx = Hexagon::vsub_hi;
481 Idx -= VecTy.getVectorNumElements() / 2;
482 } else {
483 SubIdx = Hexagon::vsub_lo;
484 }
485 VecTy = typeSplit(VecTy).first;
486 VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
487 if (VecTy == ResTy)
488 return VecV;
489 }
490
491 // The only meaningful subvectors of a single HVX vector are those that
492 // fit in a scalar register.
493 assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
494
495 MVT WordTy = tyVector(VecTy, MVT::i32);
496 SDValue WordVec = DAG.getBitcast(WordTy, VecV);
497 unsigned WordIdx = (Idx*ElemWidth) / 32;
498
499 SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
500 SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
501 if (ResTy.getSizeInBits() == 32)
502 return DAG.getBitcast(ResTy, W0);
503
504 SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
505 SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
506 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64, {W1, W0});
507 return DAG.getBitcast(ResTy, WW);
508}
509
510SDValue
511HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
512 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
513 MVT VecTy = ty(VecV);
514 unsigned HwLen = Subtarget.getVectorLength();
515 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
516 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
517 // IdxV is required to be a constant.
518 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
519
520 unsigned ResLen = ResTy.getVectorNumElements();
521 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
522 unsigned Offset = Idx * BitBytes;
523 SDValue Undef = DAG.getUNDEF(ByteTy);
524 SmallVector<int,128> Mask;
525
526 if (Subtarget.isHVXVectorType(ResTy, true)) {
527 // Converting between two vector predicates. Since the result is shorter
528 // than the source, it will correspond to a vector predicate with the
529 // relevant bits replicated. The replication count is the ratio of the
530 // source and target vector lengths.
531 unsigned Rep = VecTy.getVectorNumElements() / ResLen;
532 assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
533 for (unsigned i = 0; i != HwLen/Rep; ++i) {
534 for (unsigned j = 0; j != Rep; ++j)
535 Mask.push_back(i + Offset);
536 }
537 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
538 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
539 }
540
541 // Converting between a vector predicate and a scalar predicate. In the
542 // vector predicate, a group of BitBytes bits will correspond to a single
543 // i1 element of the source vector type. Those bits will all have the same
544 // value. The same will be true for ByteVec, where each byte corresponds
545 // to a bit in the vector predicate.
546 // The algorithm is to traverse the ByteVec, going over the i1 values from
547 // the source vector, and generate the corresponding representation in an
548 // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
549 // elements so that the interesting 8 bytes will be in the low end of the
550 // vector.
551 unsigned Rep = 8 / ResLen;
552 // Make sure the output fill the entire vector register, so repeat the
553 // 8-byte groups as many times as necessary.
554 for (unsigned r = 0; r != HwLen/ResLen; ++r) {
555 // This will generate the indexes of the 8 interesting bytes.
556 for (unsigned i = 0; i != ResLen; ++i) {
557 for (unsigned j = 0; j != Rep; ++j)
558 Mask.push_back(Offset + i*BitBytes);
559 }
560 }
561
562 SDValue Zero = getZero(dl, MVT::i32, DAG);
563 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
564 // Combine the two low words from ShuffV into a v8i8, and byte-compare
565 // them against 0.
566 SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
567 SDValue W1 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
568 {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
569 SDValue Vec64 = DAG.getNode(HexagonISD::COMBINE, dl, MVT::v8i8, {W1, W0});
570 return getNode(Hexagon::A4_vcmpbgtui, dl, ResTy,
571 {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
572}
573
574SDValue
575HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
576 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
577 MVT VecTy = ty(VecV);
578 MVT SubTy = ty(SubV);
579 unsigned HwLen = Subtarget.getVectorLength();
580 MVT ElemTy = VecTy.getVectorElementType();
581 unsigned ElemWidth = ElemTy.getSizeInBits();
582
583 bool IsPair = VecTy.getSizeInBits() == 16*HwLen;
584 MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
585 // The two single vectors that VecV consists of, if it's a pair.
586 SDValue V0, V1;
587 SDValue SingleV = VecV;
588 SDValue PickHi;
589
590 if (IsPair) {
591 V0 = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, VecV);
592 V1 = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, VecV);
593
594 SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
595 dl, MVT::i32);
596 PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
597 if (SubTy.getSizeInBits() == 8*HwLen) {
598 if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
599 unsigned Idx = CN->getZExtValue();
600 assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
601 unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
602 return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
603 }
604 // If IdxV is not a constant, generate the two variants: with the
605 // SubV as the high and as the low subregister, and select the right
606 // pair based on the IdxV.
607 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
608 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
609 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
610 }
611 // The subvector being inserted must be entirely contained in one of
612 // the vectors V0 or V1. Set SingleV to the correct one, and update
613 // IdxV to be the index relative to the beginning of that vector.
614 SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
615 IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
616 SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
617 }
618
619 // The only meaningful subvectors of a single HVX vector are those that
620 // fit in a scalar register.
621 assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
622 // Convert IdxV to be index in bytes.
623 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
624 if (!IdxN || !IdxN->isNullValue()) {
625 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
626 DAG.getConstant(ElemWidth/8, dl, MVT::i32));
627 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
628 }
629 // When inserting a single word, the rotation back to the original position
630 // would be by HwLen-Idx, but if two words are inserted, it will need to be
631 // by (HwLen-4)-Idx.
632 unsigned RolBase = HwLen;
633 if (VecTy.getSizeInBits() == 32) {
634 SDValue V = DAG.getBitcast(MVT::i32, SubV);
635 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, V);
636 } else {
637 SDValue V = DAG.getBitcast(MVT::i64, SubV);
638 SDValue R0 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, V);
639 SDValue R1 = DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, V);
640 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
641 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
642 DAG.getConstant(4, dl, MVT::i32));
643 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
644 RolBase = HwLen-4;
645 }
646 // If the vector wasn't ror'ed, don't ror it back.
647 if (RolBase != 4 || !IdxN || !IdxN->isNullValue()) {
648 SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
649 DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
650 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
651 }
652
653 if (IsPair) {
654 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
655 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
656 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
657 }
658 return SingleV;
659}
660
661SDValue
662HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
663 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
664 MVT VecTy = ty(VecV);
665 MVT SubTy = ty(SubV);
666 assert(Subtarget.isHVXVectorType(VecTy, true));
667 // VecV is an HVX vector predicate. SubV may be either an HVX vector
668 // predicate as well, or it can be a scalar predicate.
669
670 unsigned VecLen = VecTy.getVectorNumElements();
671 unsigned HwLen = Subtarget.getVectorLength();
672 assert(HwLen % VecLen == 0 && "Unexpected vector type");
673
674 unsigned Scale = VecLen / SubTy.getVectorNumElements();
675 unsigned BitBytes = HwLen / VecLen;
676 unsigned BlockLen = HwLen / Scale;
677
678 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
679 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
680 SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
681 SDValue ByteIdx;
682
683 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
684 if (!IdxN || !IdxN->isNullValue()) {
685 ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
686 DAG.getConstant(BitBytes, dl, MVT::i32));
687 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
688 }
689
690 // ByteVec is the target vector VecV rotated in such a way that the
691 // subvector should be inserted at index 0. Generate a predicate mask
692 // and use vmux to do the insertion.
693 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
694 SDValue Q = getNode(Hexagon::V6_pred_scalar2, dl, BoolTy,
695 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
696 ByteVec = getNode(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
697 // Rotate ByteVec back, and convert to a vector predicate.
698 if (!IdxN || !IdxN->isNullValue()) {
699 SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
700 SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
701 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
702 }
703 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
704}
705
706SDValue
707HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
708 MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
709 // Sign- and any-extending of a vector predicate to a vector register is
710 // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
711 // a vector of 1s (where the 1s are of type matching the vector type).
712 assert(Subtarget.isHVXVectorType(ResTy));
713 if (!ZeroExt)
714 return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
715
716 assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
717 SDValue True = DAG.getNode(HexagonISD::VSPLAT, dl, ResTy,
718 DAG.getConstant(1, dl, MVT::i32));
719 SDValue False = getZero(dl, ResTy, DAG);
720 return DAG.getSelect(dl, ResTy, VecV, True, False);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000721}
722
723SDValue
724HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
725 const {
726 const SDLoc &dl(Op);
727 MVT VecTy = ty(Op);
728
729 unsigned Size = Op.getNumOperands();
730 SmallVector<SDValue,128> Ops;
731 for (unsigned i = 0; i != Size; ++i)
732 Ops.push_back(Op.getOperand(i));
733
734 if (VecTy.getVectorElementType() == MVT::i1)
735 return buildHvxVectorPred(Ops, dl, VecTy, DAG);
736
737 if (VecTy.getSizeInBits() == 16*Subtarget.getVectorLength()) {
738 ArrayRef<SDValue> A(Ops);
739 MVT SingleTy = typeSplit(VecTy).first;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000740 SDValue V0 = buildHvxVectorReg(A.take_front(Size/2), dl, SingleTy, DAG);
741 SDValue V1 = buildHvxVectorReg(A.drop_front(Size/2), dl, SingleTy, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000742 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
743 }
744
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000745 return buildHvxVectorReg(Ops, dl, VecTy, DAG);
746}
747
748SDValue
749HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
750 const {
751 // This should only be called for vectors of i1. The "scalar" vector
752 // concatenation does not need special lowering (assuming that only
753 // two vectors are concatenated at a time).
754 MVT VecTy = ty(Op);
755 assert(VecTy.getVectorElementType() == MVT::i1);
756
757 const SDLoc &dl(Op);
758 unsigned HwLen = Subtarget.getVectorLength();
759 unsigned NumOp = Op.getNumOperands();
760 assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
Krzysztof Parzyszekae3e9342018-01-23 18:16:52 +0000761 (void)NumOp;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000762
763 // Count how many bytes (in a vector register) each bit in VecTy
764 // corresponds to.
765 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
766
767 SmallVector<SDValue,8> Prefixes;
768 for (SDValue V : Op.getNode()->op_values()) {
769 SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
770 Prefixes.push_back(P);
771 }
772
773 unsigned InpLen = ty(Op.getOperand(0)).getVectorNumElements();
774 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
775 SDValue S = DAG.getConstant(InpLen*BitBytes, dl, MVT::i32);
776 SDValue Res = getZero(dl, ByteTy, DAG);
777 for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
778 Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
779 Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
780 }
781 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000782}
783
784SDValue
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000785HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
786 const {
787 // Change the type of the extracted element to i32.
788 SDValue VecV = Op.getOperand(0);
789 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000790 const SDLoc &dl(Op);
791 SDValue IdxV = Op.getOperand(1);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000792 if (ElemTy == MVT::i1)
793 return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000794
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000795 return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000796}
797
798SDValue
799HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
800 const {
801 const SDLoc &dl(Op);
802 SDValue VecV = Op.getOperand(0);
803 SDValue ValV = Op.getOperand(1);
804 SDValue IdxV = Op.getOperand(2);
805 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000806 if (ElemTy == MVT::i1)
807 return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000808
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000809 return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000810}
811
812SDValue
813HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
814 const {
815 SDValue SrcV = Op.getOperand(0);
816 MVT SrcTy = ty(SrcV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000817 MVT DstTy = ty(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000818 SDValue IdxV = Op.getOperand(1);
819 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000820 assert(Idx % DstTy.getVectorNumElements() == 0);
821 (void)Idx;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000822 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000823
824 MVT ElemTy = SrcTy.getVectorElementType();
825 if (ElemTy == MVT::i1)
826 return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
827
828 return extractHvxSubvectorReg(SrcV, IdxV, dl, DstTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000829}
830
831SDValue
832HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
833 const {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000834 // Idx does not need to be a constant.
835 SDValue VecV = Op.getOperand(0);
836 SDValue ValV = Op.getOperand(1);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000837 SDValue IdxV = Op.getOperand(2);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000838
839 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000840 MVT VecTy = ty(VecV);
841 MVT ElemTy = VecTy.getVectorElementType();
842 if (ElemTy == MVT::i1)
843 return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
844
845 return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000846}
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000847
848SDValue
849HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
850 MVT ResTy = ty(Op);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000851 assert(ResTy.isVector());
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000852 const SDLoc &dl(Op);
853 SmallVector<int,256> ShuffMask;
854
855 MVT ElemTy = ResTy.getVectorElementType();
856 unsigned VecLen = ResTy.getVectorNumElements();
857 SDValue Vs = Op.getOperand(0);
858 SDValue Vt = Op.getOperand(1);
859
860 switch (ElemTy.SimpleTy) {
861 case MVT::i8:
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000862 case MVT::i16: { // V6_vmpyih
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000863 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
864 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
865 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
866 // For i16, use V6_vmpyhv, which behaves in an analogous way to
867 // V6_vmpybv: results Lo and Hi are products of even/odd elements
868 // respectively.
869 MVT ExtTy = typeExtElem(ResTy, 2);
870 unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
871 : Hexagon::V6_vmpyhv;
872 SDValue M = getNode(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
873
874 // Discard high halves of the resulting values, collect the low halves.
875 for (unsigned I = 0; I < VecLen; I += 2) {
876 ShuffMask.push_back(I); // Pick even element.
877 ShuffMask.push_back(I+VecLen); // Pick odd element.
878 }
879 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
Krzysztof Parzyszek0f5d9762018-01-05 20:45:34 +0000880 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
881 return DAG.getBitcast(ResTy, BS);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000882 }
883 case MVT::i32: {
884 // Use the following sequence for signed word multiply:
885 // T0 = V6_vmpyiowh Vs, Vt
886 // T1 = V6_vaslw T0, 16
887 // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
888 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
889 SDValue T0 = getNode(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
890 SDValue T1 = getNode(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
891 SDValue T2 = getNode(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
892 {T1, Vs, Vt}, DAG);
893 return T2;
894 }
895 default:
896 break;
897 }
898 return SDValue();
899}
Krzysztof Parzyszek47076052017-12-14 21:28:48 +0000900
901SDValue
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000902HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
903 MVT ResTy = ty(Op);
904 assert(ResTy.isVector());
905 const SDLoc &dl(Op);
906 SmallVector<int,256> ShuffMask;
907
908 MVT ElemTy = ResTy.getVectorElementType();
909 unsigned VecLen = ResTy.getVectorNumElements();
910 SDValue Vs = Op.getOperand(0);
911 SDValue Vt = Op.getOperand(1);
912 bool IsSigned = Op.getOpcode() == ISD::MULHS;
913
914 if (ElemTy == MVT::i8 || ElemTy == MVT::i16) {
915 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
916 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
917 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
918 // For i16, use V6_vmpyhv, which behaves in an analogous way to
919 // V6_vmpybv: results Lo and Hi are products of even/odd elements
920 // respectively.
921 MVT ExtTy = typeExtElem(ResTy, 2);
922 unsigned MpyOpc = ElemTy == MVT::i8
923 ? (IsSigned ? Hexagon::V6_vmpybv : Hexagon::V6_vmpyubv)
924 : (IsSigned ? Hexagon::V6_vmpyhv : Hexagon::V6_vmpyuhv);
925 SDValue M = getNode(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
926
927 // Discard low halves of the resulting values, collect the high halves.
928 for (unsigned I = 0; I < VecLen; I += 2) {
929 ShuffMask.push_back(I+1); // Pick even element.
930 ShuffMask.push_back(I+VecLen+1); // Pick odd element.
931 }
932 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
933 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
934 return DAG.getBitcast(ResTy, BS);
935 }
936
937 assert(ElemTy == MVT::i32);
938 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
939
940 if (IsSigned) {
941 // mulhs(Vs,Vt) =
942 // = [(Hi(Vs)*2^16 + Lo(Vs)) *s (Hi(Vt)*2^16 + Lo(Vt))] >> 32
943 // = [Hi(Vs)*2^16 *s Hi(Vt)*2^16 + Hi(Vs) *su Lo(Vt)*2^16
944 // + Lo(Vs) *us (Hi(Vt)*2^16 + Lo(Vt))] >> 32
945 // = [Hi(Vs) *s Hi(Vt)*2^32 + Hi(Vs) *su Lo(Vt)*2^16
946 // + Lo(Vs) *us Vt] >> 32
947 // The low half of Lo(Vs)*Lo(Vt) will be discarded (it's not added to
948 // anything, so it cannot produce any carry over to higher bits),
949 // so everything in [] can be shifted by 16 without loss of precision.
950 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + Lo(Vs)*Vt >> 16] >> 16
951 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + V6_vmpyewuh(Vs,Vt)] >> 16
952 // Denote Hi(Vs) = Vs':
953 // = [Vs'*s Hi(Vt)*2^16 + Vs' *su Lo(Vt) + V6_vmpyewuh(Vt,Vs)] >> 16
954 // = Vs'*s Hi(Vt) + (V6_vmpyiewuh(Vs',Vt) + V6_vmpyewuh(Vt,Vs)) >> 16
955 SDValue T0 = getNode(Hexagon::V6_vmpyewuh, dl, ResTy, {Vt, Vs}, DAG);
956 // Get Vs':
957 SDValue S0 = getNode(Hexagon::V6_vasrw, dl, ResTy, {Vs, S16}, DAG);
958 SDValue T1 = getNode(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
959 {T0, S0, Vt}, DAG);
960 // Shift by 16:
961 SDValue S2 = getNode(Hexagon::V6_vasrw, dl, ResTy, {T1, S16}, DAG);
962 // Get Vs'*Hi(Vt):
963 SDValue T2 = getNode(Hexagon::V6_vmpyiowh, dl, ResTy, {S0, Vt}, DAG);
964 // Add:
965 SDValue T3 = DAG.getNode(ISD::ADD, dl, ResTy, {S2, T2});
966 return T3;
967 }
968
969 // Unsigned mulhw. (Would expansion using signed mulhw be better?)
970
971 auto LoVec = [&DAG,ResTy,dl] (SDValue Pair) {
972 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResTy, Pair);
973 };
974 auto HiVec = [&DAG,ResTy,dl] (SDValue Pair) {
975 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResTy, Pair);
976 };
977
978 MVT PairTy = typeJoin({ResTy, ResTy});
979 SDValue P = getNode(Hexagon::V6_lvsplatw, dl, ResTy,
980 {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
981 // Multiply-unsigned halfwords:
982 // LoVec = Vs.uh[2i] * Vt.uh[2i],
983 // HiVec = Vs.uh[2i+1] * Vt.uh[2i+1]
984 SDValue T0 = getNode(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, Vt}, DAG);
985 // The low halves in the LoVec of the pair can be discarded. They are
986 // not added to anything (in the full-precision product), so they cannot
987 // produce a carry into the higher bits.
988 SDValue T1 = getNode(Hexagon::V6_vlsrw, dl, ResTy, {LoVec(T0), S16}, DAG);
989 // Swap low and high halves in Vt, and do the halfword multiplication
990 // to get products Vs.uh[2i] * Vt.uh[2i+1] and Vs.uh[2i+1] * Vt.uh[2i].
991 SDValue D0 = getNode(Hexagon::V6_vdelta, dl, ResTy, {Vt, P}, DAG);
992 SDValue T2 = getNode(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, D0}, DAG);
993 // T2 has mixed products of halfwords: Lo(Vt)*Hi(Vs) and Hi(Vt)*Lo(Vs).
994 // These products are words, but cannot be added directly because the
995 // sums could overflow. Add these products, by halfwords, where each sum
996 // of a pair of halfwords gives a word.
997 SDValue T3 = getNode(Hexagon::V6_vadduhw, dl, PairTy,
998 {LoVec(T2), HiVec(T2)}, DAG);
999 // Add the high halfwords from the products of the low halfwords.
1000 SDValue T4 = DAG.getNode(ISD::ADD, dl, ResTy, {T1, LoVec(T3)});
1001 SDValue T5 = getNode(Hexagon::V6_vlsrw, dl, ResTy, {T4, S16}, DAG);
1002 SDValue T6 = DAG.getNode(ISD::ADD, dl, ResTy, {HiVec(T0), HiVec(T3)});
1003 SDValue T7 = DAG.getNode(ISD::ADD, dl, ResTy, {T5, T6});
1004 return T7;
1005}
1006
1007SDValue
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001008HexagonTargetLowering::LowerHvxSetCC(SDValue Op, SelectionDAG &DAG) const {
1009 MVT VecTy = ty(Op.getOperand(0));
1010 assert(VecTy == ty(Op.getOperand(1)));
1011
1012 SDValue Cmp = Op.getOperand(2);
1013 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1014 bool Negate = false, Swap = false;
1015
1016 // HVX has instructions for SETEQ, SETGT, SETUGT. The other comparisons
1017 // can be arranged as operand-swapped/negated versions of these. Since
1018 // the generated code will have the original CC expressed as
1019 // (negate (swap-op NewCmp)),
1020 // the condition code for the NewCmp should be calculated from the original
1021 // CC by applying these operations in the reverse order.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001022 //
1023 // This could also be done through setCondCodeAction, but for negation it
1024 // uses a xor with a vector of -1s, which it obtains from BUILD_VECTOR.
1025 // That is far too expensive for what can be done with a single instruction.
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001026
1027 switch (CC) {
1028 case ISD::SETNE: // !eq
1029 case ISD::SETLE: // !gt
1030 case ISD::SETGE: // !lt
1031 case ISD::SETULE: // !ugt
1032 case ISD::SETUGE: // !ult
1033 CC = ISD::getSetCCInverse(CC, true);
1034 Negate = true;
1035 break;
1036 default:
1037 break;
1038 }
1039
1040 switch (CC) {
1041 case ISD::SETLT: // swap gt
1042 case ISD::SETULT: // swap ugt
1043 CC = ISD::getSetCCSwappedOperands(CC);
1044 Swap = true;
1045 break;
1046 default:
1047 break;
1048 }
1049
1050 assert(CC == ISD::SETEQ || CC == ISD::SETGT || CC == ISD::SETUGT);
1051
1052 MVT ElemTy = VecTy.getVectorElementType();
1053 unsigned ElemWidth = ElemTy.getSizeInBits();
1054 assert(isPowerOf2_32(ElemWidth));
1055
1056 auto getIdx = [] (unsigned Code) {
1057 static const unsigned Idx[] = { ISD::SETEQ, ISD::SETGT, ISD::SETUGT };
1058 for (unsigned I = 0, E = array_lengthof(Idx); I != E; ++I)
1059 if (Code == Idx[I])
1060 return I;
1061 llvm_unreachable("Unhandled CondCode");
1062 };
1063
1064 static unsigned OpcTable[3][3] = {
1065 // SETEQ SETGT, SETUGT
1066 /* Byte */ { Hexagon::V6_veqb, Hexagon::V6_vgtb, Hexagon::V6_vgtub },
1067 /* Half */ { Hexagon::V6_veqh, Hexagon::V6_vgth, Hexagon::V6_vgtuh },
1068 /* Word */ { Hexagon::V6_veqw, Hexagon::V6_vgtw, Hexagon::V6_vgtuw }
1069 };
1070
1071 unsigned CmpOpc = OpcTable[Log2_32(ElemWidth)-3][getIdx(CC)];
1072
1073 MVT ResTy = ty(Op);
1074 const SDLoc &dl(Op);
1075 SDValue OpL = Swap ? Op.getOperand(1) : Op.getOperand(0);
1076 SDValue OpR = Swap ? Op.getOperand(0) : Op.getOperand(1);
1077 SDValue CmpV = getNode(CmpOpc, dl, ResTy, {OpL, OpR}, DAG);
1078 return Negate ? getNode(Hexagon::V6_pred_not, dl, ResTy, {CmpV}, DAG)
1079 : CmpV;
1080}
Krzysztof Parzyszek6b589e52017-12-18 18:32:27 +00001081
1082SDValue
1083HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
1084 // Sign- and zero-extends are legal.
1085 assert(Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG);
1086 return DAG.getZeroExtendVectorInReg(Op.getOperand(0), SDLoc(Op), ty(Op));
1087}