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