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