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