blob: 9d0e4beea87e63d3ddbbbf2b328dc7c0127b50b3 [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
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +000081bool
82HexagonTargetLowering::isHvxSingleTy(MVT Ty) const {
83 return Subtarget.isHVXVectorType(Ty) &&
84 Ty.getSizeInBits() == 8 * Subtarget.getVectorLength();
85}
86
87bool
88HexagonTargetLowering::isHvxPairTy(MVT Ty) const {
89 return Subtarget.isHVXVectorType(Ty) &&
90 Ty.getSizeInBits() == 16 * Subtarget.getVectorLength();
91}
92
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +000093SDValue
94HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
95 SelectionDAG &DAG) const {
96 if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
97 ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
98
99 unsigned ElemWidth = ElemTy.getSizeInBits();
100 if (ElemWidth == 8)
101 return ElemIdx;
102
103 unsigned L = Log2_32(ElemWidth/8);
104 const SDLoc &dl(ElemIdx);
105 return DAG.getNode(ISD::SHL, dl, MVT::i32,
106 {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
107}
108
109SDValue
110HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
111 SelectionDAG &DAG) const {
112 unsigned ElemWidth = ElemTy.getSizeInBits();
113 assert(ElemWidth >= 8 && ElemWidth <= 32);
114 if (ElemWidth == 32)
115 return Idx;
116
117 if (ty(Idx) != MVT::i32)
118 Idx = DAG.getBitcast(MVT::i32, Idx);
119 const SDLoc &dl(Idx);
120 SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
121 SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
122 return SubIdx;
123}
124
125SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000126HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
127 SDValue Op1, ArrayRef<int> Mask,
128 SelectionDAG &DAG) const {
129 MVT OpTy = ty(Op0);
130 assert(OpTy == ty(Op1));
131
132 MVT ElemTy = OpTy.getVectorElementType();
133 if (ElemTy == MVT::i8)
134 return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
135 assert(ElemTy.getSizeInBits() >= 8);
136
137 MVT ResTy = tyVector(OpTy, MVT::i8);
138 unsigned ElemSize = ElemTy.getSizeInBits() / 8;
139
140 SmallVector<int,128> ByteMask;
141 for (int M : Mask) {
142 if (M < 0) {
143 for (unsigned I = 0; I != ElemSize; ++I)
144 ByteMask.push_back(-1);
145 } else {
146 int NewM = M*ElemSize;
147 for (unsigned I = 0; I != ElemSize; ++I)
148 ByteMask.push_back(NewM+I);
149 }
150 }
151 assert(ResTy.getVectorNumElements() == ByteMask.size());
152 return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
153 opCastElem(Op1, MVT::i8, DAG), ByteMask);
154}
155
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000156SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000157HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
158 const SDLoc &dl, MVT VecTy,
159 SelectionDAG &DAG) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000160 unsigned VecLen = Values.size();
161 MachineFunction &MF = DAG.getMachineFunction();
162 MVT ElemTy = VecTy.getVectorElementType();
163 unsigned ElemWidth = ElemTy.getSizeInBits();
164 unsigned HwLen = Subtarget.getVectorLength();
165
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000166 unsigned ElemSize = ElemWidth / 8;
167 assert(ElemSize*VecLen == HwLen);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000168 SmallVector<SDValue,32> Words;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000169
170 if (VecTy.getVectorElementType() != MVT::i32) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000171 assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
172 unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
173 MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000174 for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
175 SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000176 Words.push_back(DAG.getBitcast(MVT::i32, W));
177 }
178 } else {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000179 Words.assign(Values.begin(), Values.end());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000180 }
181
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000182 unsigned NumWords = Words.size();
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000183 bool IsSplat = true, IsUndef = true;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000184 SDValue SplatV;
185 for (unsigned i = 0; i != NumWords && IsSplat; ++i) {
186 if (isUndef(Words[i]))
187 continue;
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000188 IsUndef = false;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000189 if (!SplatV.getNode())
190 SplatV = Words[i];
191 else if (SplatV != Words[i])
192 IsSplat = false;
193 }
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000194 if (IsUndef)
195 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000196 if (IsSplat) {
197 assert(SplatV.getNode());
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000198 auto *IdxN = dyn_cast<ConstantSDNode>(SplatV.getNode());
199 if (IdxN && IdxN->isNullValue())
200 return getZero(dl, VecTy, DAG);
201 MVT WordTy = MVT::getVectorVT(MVT::i32, HwLen/4);
202 SDValue SV = DAG.getNode(HexagonISD::VSPLAT, dl, WordTy, SplatV);
203 return DAG.getBitcast(VecTy, SV);
204 }
205
206 // Delay recognizing constant vectors until here, so that we can generate
207 // a vsplat.
208 SmallVector<ConstantInt*, 128> Consts(VecLen);
209 bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
210 if (AllConst) {
211 ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
212 (Constant**)Consts.end());
213 Constant *CV = ConstantVector::get(Tmp);
214 unsigned Align = HwLen;
215 SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
216 return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
217 MachinePointerInfo::getConstantPool(MF), Align);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000218 }
219
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000220 // Construct two halves in parallel, then or them together.
221 assert(4*Words.size() == Subtarget.getVectorLength());
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000222 SDValue HalfV0 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
223 SDValue HalfV1 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000224 SDValue S = DAG.getConstant(4, dl, MVT::i32);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000225 for (unsigned i = 0; i != NumWords/2; ++i) {
226 SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
227 {HalfV0, Words[i]});
228 SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
229 {HalfV1, Words[i+NumWords/2]});
230 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
231 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
232 }
233
234 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
235 {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
236 SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
237 return DstV;
238}
239
240SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000241HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
242 unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
243 MVT PredTy = ty(PredV);
244 unsigned HwLen = Subtarget.getVectorLength();
245 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
246
247 if (Subtarget.isHVXVectorType(PredTy, true)) {
248 // Move the vector predicate SubV to a vector register, and scale it
249 // down to match the representation (bytes per type element) that VecV
250 // uses. The scaling down will pick every 2nd or 4th (every Scale-th
Hiroshi Inoue0909ca12018-01-26 08:15:29 +0000251 // in general) element and put them at the front of the resulting
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000252 // vector. This subvector will then be inserted into the Q2V of VecV.
253 // To avoid having an operation that generates an illegal type (short
254 // vector), generate a full size vector.
255 //
256 SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
257 SmallVector<int,128> Mask(HwLen);
258 // Scale = BitBytes(PredV) / Given BitBytes.
259 unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
260 unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
261
262 for (unsigned i = 0; i != HwLen; ++i) {
263 unsigned Num = i % Scale;
264 unsigned Off = i / Scale;
265 Mask[BlockLen*Num + Off] = i;
266 }
267 SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
268 if (!ZeroFill)
269 return S;
270 // Fill the bytes beyond BlockLen with 0s.
271 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000272 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
273 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000274 SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
275 return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
276 }
277
278 // Make sure that this is a valid scalar predicate.
279 assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
280
281 unsigned Bytes = 8 / PredTy.getVectorNumElements();
282 SmallVector<SDValue,4> Words[2];
283 unsigned IdxW = 0;
284
285 auto Lo32 = [&DAG, &dl] (SDValue P) {
286 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, P);
287 };
288 auto Hi32 = [&DAG, &dl] (SDValue P) {
289 return DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, P);
290 };
291
292 SDValue W0 = isUndef(PredV)
293 ? DAG.getUNDEF(MVT::i64)
294 : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
295 Words[IdxW].push_back(Hi32(W0));
296 Words[IdxW].push_back(Lo32(W0));
297
298 while (Bytes < BitBytes) {
299 IdxW ^= 1;
300 Words[IdxW].clear();
301
302 if (Bytes < 4) {
303 for (const SDValue &W : Words[IdxW ^ 1]) {
304 SDValue T = expandPredicate(W, dl, DAG);
305 Words[IdxW].push_back(Hi32(T));
306 Words[IdxW].push_back(Lo32(T));
307 }
308 } else {
309 for (const SDValue &W : Words[IdxW ^ 1]) {
310 Words[IdxW].push_back(W);
311 Words[IdxW].push_back(W);
312 }
313 }
314 Bytes *= 2;
315 }
316
317 assert(Bytes == BitBytes);
318
319 SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
320 SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
321 for (const SDValue &W : Words[IdxW]) {
322 Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
323 Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
324 }
325
326 return Vec;
327}
328
329SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000330HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
331 const SDLoc &dl, MVT VecTy,
332 SelectionDAG &DAG) const {
333 // Construct a vector V of bytes, such that a comparison V >u 0 would
334 // produce the required vector predicate.
335 unsigned VecLen = Values.size();
336 unsigned HwLen = Subtarget.getVectorLength();
337 assert(VecLen <= HwLen || VecLen == 8*HwLen);
338 SmallVector<SDValue,128> Bytes;
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000339 bool AllT = true, AllF = true;
340
341 auto IsTrue = [] (SDValue V) {
342 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
343 return !N->isNullValue();
344 return false;
345 };
346 auto IsFalse = [] (SDValue V) {
347 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
348 return N->isNullValue();
349 return false;
350 };
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000351
352 if (VecLen <= HwLen) {
353 // In the hardware, each bit of a vector predicate corresponds to a byte
354 // of a vector register. Calculate how many bytes does a bit of VecTy
355 // correspond to.
356 assert(HwLen % VecLen == 0);
357 unsigned BitBytes = HwLen / VecLen;
358 for (SDValue V : Values) {
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000359 AllT &= IsTrue(V);
360 AllF &= IsFalse(V);
361
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000362 SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000363 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000364 for (unsigned B = 0; B != BitBytes; ++B)
365 Bytes.push_back(Ext);
366 }
367 } else {
368 // There are as many i1 values, as there are bits in a vector register.
369 // Divide the values into groups of 8 and check that each group consists
370 // of the same value (ignoring undefs).
371 for (unsigned I = 0; I != VecLen; I += 8) {
372 unsigned B = 0;
373 // Find the first non-undef value in this group.
374 for (; B != 8; ++B) {
375 if (!Values[I+B].isUndef())
376 break;
377 }
378 SDValue F = Values[I+B];
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000379 AllT &= IsTrue(F);
380 AllF &= IsFalse(F);
381
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000382 SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000383 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000384 Bytes.push_back(Ext);
385 // Verify that the rest of values in the group are the same as the
386 // first.
387 for (; B != 8; ++B)
388 assert(Values[I+B].isUndef() || Values[I+B] == F);
389 }
390 }
391
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000392 if (AllT)
393 return DAG.getNode(HexagonISD::QTRUE, dl, VecTy);
394 if (AllF)
395 return DAG.getNode(HexagonISD::QFALSE, dl, VecTy);
396
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000397 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000398 SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
399 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
400}
401
402SDValue
403HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
404 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
405 MVT ElemTy = ty(VecV).getVectorElementType();
406
407 unsigned ElemWidth = ElemTy.getSizeInBits();
408 assert(ElemWidth >= 8 && ElemWidth <= 32);
409 (void)ElemWidth;
410
411 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
412 SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
413 {VecV, ByteIdx});
414 if (ElemTy == MVT::i32)
415 return ExWord;
416
417 // Have an extracted word, need to extract the smaller element out of it.
418 // 1. Extract the bits of (the original) IdxV that correspond to the index
419 // of the desired element in the 32-bit word.
420 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
421 // 2. Extract the element from the word.
422 SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
423 return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
424}
425
426SDValue
427HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
428 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
429 // Implement other return types if necessary.
430 assert(ResTy == MVT::i1);
431
432 unsigned HwLen = Subtarget.getVectorLength();
433 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
434 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
435
436 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
437 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
438 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
439
440 SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
441 SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000442 return getInstr(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000443}
444
445SDValue
446HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
447 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
448 MVT ElemTy = ty(VecV).getVectorElementType();
449
450 unsigned ElemWidth = ElemTy.getSizeInBits();
451 assert(ElemWidth >= 8 && ElemWidth <= 32);
452 (void)ElemWidth;
453
454 auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
455 SDValue ByteIdxV) {
456 MVT VecTy = ty(VecV);
457 unsigned HwLen = Subtarget.getVectorLength();
458 SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
459 {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
460 SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
461 SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
462 SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
463 {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
464 SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
465 return TorV;
466 };
467
468 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
469 if (ElemTy == MVT::i32)
470 return InsertWord(VecV, ValV, ByteIdx);
471
472 // If this is not inserting a 32-bit word, convert it into such a thing.
473 // 1. Extract the existing word from the target vector.
474 SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
475 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
476 SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
477 dl, MVT::i32, DAG);
478
479 // 2. Treating the extracted word as a 32-bit vector, insert the given
480 // value into it.
481 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
482 MVT SubVecTy = tyVector(ty(Ext), ElemTy);
483 SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
484 ValV, SubIdx, dl, ElemTy, DAG);
485
486 // 3. Insert the 32-bit word back into the original vector.
487 return InsertWord(VecV, Ins, ByteIdx);
488}
489
490SDValue
491HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
492 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
493 unsigned HwLen = Subtarget.getVectorLength();
494 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
495 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
496
497 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
498 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
499 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
500 ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
501
502 SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
503 return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
504}
505
506SDValue
507HexagonTargetLowering::extractHvxSubvectorReg(SDValue VecV, SDValue IdxV,
508 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
509 MVT VecTy = ty(VecV);
510 unsigned HwLen = Subtarget.getVectorLength();
511 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
512 MVT ElemTy = VecTy.getVectorElementType();
513 unsigned ElemWidth = ElemTy.getSizeInBits();
514
515 // If the source vector is a vector pair, get the single vector containing
516 // the subvector of interest. The subvector will never overlap two single
517 // vectors.
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000518 if (isHvxPairTy(VecTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000519 unsigned SubIdx;
520 if (Idx * ElemWidth >= 8*HwLen) {
521 SubIdx = Hexagon::vsub_hi;
522 Idx -= VecTy.getVectorNumElements() / 2;
523 } else {
524 SubIdx = Hexagon::vsub_lo;
525 }
526 VecTy = typeSplit(VecTy).first;
527 VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
528 if (VecTy == ResTy)
529 return VecV;
530 }
531
532 // The only meaningful subvectors of a single HVX vector are those that
533 // fit in a scalar register.
534 assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
535
536 MVT WordTy = tyVector(VecTy, MVT::i32);
537 SDValue WordVec = DAG.getBitcast(WordTy, VecV);
538 unsigned WordIdx = (Idx*ElemWidth) / 32;
539
540 SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
541 SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
542 if (ResTy.getSizeInBits() == 32)
543 return DAG.getBitcast(ResTy, W0);
544
545 SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
546 SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
547 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64, {W1, W0});
548 return DAG.getBitcast(ResTy, WW);
549}
550
551SDValue
552HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
553 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
554 MVT VecTy = ty(VecV);
555 unsigned HwLen = Subtarget.getVectorLength();
556 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
557 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
558 // IdxV is required to be a constant.
559 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
560
561 unsigned ResLen = ResTy.getVectorNumElements();
562 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
563 unsigned Offset = Idx * BitBytes;
564 SDValue Undef = DAG.getUNDEF(ByteTy);
565 SmallVector<int,128> Mask;
566
567 if (Subtarget.isHVXVectorType(ResTy, true)) {
568 // Converting between two vector predicates. Since the result is shorter
569 // than the source, it will correspond to a vector predicate with the
570 // relevant bits replicated. The replication count is the ratio of the
571 // source and target vector lengths.
572 unsigned Rep = VecTy.getVectorNumElements() / ResLen;
573 assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
574 for (unsigned i = 0; i != HwLen/Rep; ++i) {
575 for (unsigned j = 0; j != Rep; ++j)
576 Mask.push_back(i + Offset);
577 }
578 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
579 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
580 }
581
582 // Converting between a vector predicate and a scalar predicate. In the
583 // vector predicate, a group of BitBytes bits will correspond to a single
584 // i1 element of the source vector type. Those bits will all have the same
585 // value. The same will be true for ByteVec, where each byte corresponds
586 // to a bit in the vector predicate.
587 // The algorithm is to traverse the ByteVec, going over the i1 values from
588 // the source vector, and generate the corresponding representation in an
589 // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
590 // elements so that the interesting 8 bytes will be in the low end of the
591 // vector.
592 unsigned Rep = 8 / ResLen;
593 // Make sure the output fill the entire vector register, so repeat the
594 // 8-byte groups as many times as necessary.
595 for (unsigned r = 0; r != HwLen/ResLen; ++r) {
596 // This will generate the indexes of the 8 interesting bytes.
597 for (unsigned i = 0; i != ResLen; ++i) {
598 for (unsigned j = 0; j != Rep; ++j)
599 Mask.push_back(Offset + i*BitBytes);
600 }
601 }
602
603 SDValue Zero = getZero(dl, MVT::i32, DAG);
604 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
605 // Combine the two low words from ShuffV into a v8i8, and byte-compare
606 // them against 0.
607 SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
608 SDValue W1 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
609 {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
610 SDValue Vec64 = DAG.getNode(HexagonISD::COMBINE, dl, MVT::v8i8, {W1, W0});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000611 return getInstr(Hexagon::A4_vcmpbgtui, dl, ResTy,
612 {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000613}
614
615SDValue
616HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
617 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
618 MVT VecTy = ty(VecV);
619 MVT SubTy = ty(SubV);
620 unsigned HwLen = Subtarget.getVectorLength();
621 MVT ElemTy = VecTy.getVectorElementType();
622 unsigned ElemWidth = ElemTy.getSizeInBits();
623
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000624 bool IsPair = isHvxPairTy(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000625 MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
626 // The two single vectors that VecV consists of, if it's a pair.
627 SDValue V0, V1;
628 SDValue SingleV = VecV;
629 SDValue PickHi;
630
631 if (IsPair) {
632 V0 = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, VecV);
633 V1 = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, VecV);
634
635 SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
636 dl, MVT::i32);
637 PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000638 if (isHvxSingleTy(SubTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000639 if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
640 unsigned Idx = CN->getZExtValue();
641 assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
642 unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
643 return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
644 }
645 // If IdxV is not a constant, generate the two variants: with the
646 // SubV as the high and as the low subregister, and select the right
647 // pair based on the IdxV.
648 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
649 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
650 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
651 }
652 // The subvector being inserted must be entirely contained in one of
653 // the vectors V0 or V1. Set SingleV to the correct one, and update
654 // IdxV to be the index relative to the beginning of that vector.
655 SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
656 IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
657 SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
658 }
659
660 // The only meaningful subvectors of a single HVX vector are those that
661 // fit in a scalar register.
662 assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
663 // Convert IdxV to be index in bytes.
664 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
665 if (!IdxN || !IdxN->isNullValue()) {
666 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
667 DAG.getConstant(ElemWidth/8, dl, MVT::i32));
668 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
669 }
670 // When inserting a single word, the rotation back to the original position
671 // would be by HwLen-Idx, but if two words are inserted, it will need to be
672 // by (HwLen-4)-Idx.
673 unsigned RolBase = HwLen;
674 if (VecTy.getSizeInBits() == 32) {
675 SDValue V = DAG.getBitcast(MVT::i32, SubV);
676 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, V);
677 } else {
678 SDValue V = DAG.getBitcast(MVT::i64, SubV);
679 SDValue R0 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, V);
680 SDValue R1 = DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, V);
681 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
682 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
683 DAG.getConstant(4, dl, MVT::i32));
684 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
685 RolBase = HwLen-4;
686 }
687 // If the vector wasn't ror'ed, don't ror it back.
688 if (RolBase != 4 || !IdxN || !IdxN->isNullValue()) {
689 SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
690 DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
691 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
692 }
693
694 if (IsPair) {
695 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
696 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
697 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
698 }
699 return SingleV;
700}
701
702SDValue
703HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
704 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
705 MVT VecTy = ty(VecV);
706 MVT SubTy = ty(SubV);
707 assert(Subtarget.isHVXVectorType(VecTy, true));
708 // VecV is an HVX vector predicate. SubV may be either an HVX vector
709 // predicate as well, or it can be a scalar predicate.
710
711 unsigned VecLen = VecTy.getVectorNumElements();
712 unsigned HwLen = Subtarget.getVectorLength();
713 assert(HwLen % VecLen == 0 && "Unexpected vector type");
714
715 unsigned Scale = VecLen / SubTy.getVectorNumElements();
716 unsigned BitBytes = HwLen / VecLen;
717 unsigned BlockLen = HwLen / Scale;
718
719 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
720 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
721 SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
722 SDValue ByteIdx;
723
724 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
725 if (!IdxN || !IdxN->isNullValue()) {
726 ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
727 DAG.getConstant(BitBytes, dl, MVT::i32));
728 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
729 }
730
731 // ByteVec is the target vector VecV rotated in such a way that the
732 // subvector should be inserted at index 0. Generate a predicate mask
733 // and use vmux to do the insertion.
734 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000735 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
736 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
737 ByteVec = getInstr(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000738 // Rotate ByteVec back, and convert to a vector predicate.
739 if (!IdxN || !IdxN->isNullValue()) {
740 SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
741 SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
742 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
743 }
744 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
745}
746
747SDValue
748HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
749 MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
750 // Sign- and any-extending of a vector predicate to a vector register is
751 // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
752 // a vector of 1s (where the 1s are of type matching the vector type).
753 assert(Subtarget.isHVXVectorType(ResTy));
754 if (!ZeroExt)
755 return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
756
757 assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
758 SDValue True = DAG.getNode(HexagonISD::VSPLAT, dl, ResTy,
759 DAG.getConstant(1, dl, MVT::i32));
760 SDValue False = getZero(dl, ResTy, DAG);
761 return DAG.getSelect(dl, ResTy, VecV, True, False);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000762}
763
764SDValue
765HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
766 const {
767 const SDLoc &dl(Op);
768 MVT VecTy = ty(Op);
769
770 unsigned Size = Op.getNumOperands();
771 SmallVector<SDValue,128> Ops;
772 for (unsigned i = 0; i != Size; ++i)
773 Ops.push_back(Op.getOperand(i));
774
775 if (VecTy.getVectorElementType() == MVT::i1)
776 return buildHvxVectorPred(Ops, dl, VecTy, DAG);
777
778 if (VecTy.getSizeInBits() == 16*Subtarget.getVectorLength()) {
779 ArrayRef<SDValue> A(Ops);
780 MVT SingleTy = typeSplit(VecTy).first;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000781 SDValue V0 = buildHvxVectorReg(A.take_front(Size/2), dl, SingleTy, DAG);
782 SDValue V1 = buildHvxVectorReg(A.drop_front(Size/2), dl, SingleTy, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000783 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
784 }
785
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000786 return buildHvxVectorReg(Ops, dl, VecTy, DAG);
787}
788
789SDValue
790HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
791 const {
792 // This should only be called for vectors of i1. The "scalar" vector
793 // concatenation does not need special lowering (assuming that only
794 // two vectors are concatenated at a time).
795 MVT VecTy = ty(Op);
796 assert(VecTy.getVectorElementType() == MVT::i1);
797
798 const SDLoc &dl(Op);
799 unsigned HwLen = Subtarget.getVectorLength();
800 unsigned NumOp = Op.getNumOperands();
801 assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
Krzysztof Parzyszekae3e9342018-01-23 18:16:52 +0000802 (void)NumOp;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000803
804 // Count how many bytes (in a vector register) each bit in VecTy
805 // corresponds to.
806 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
807
808 SmallVector<SDValue,8> Prefixes;
809 for (SDValue V : Op.getNode()->op_values()) {
810 SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
811 Prefixes.push_back(P);
812 }
813
814 unsigned InpLen = ty(Op.getOperand(0)).getVectorNumElements();
815 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
816 SDValue S = DAG.getConstant(InpLen*BitBytes, dl, MVT::i32);
817 SDValue Res = getZero(dl, ByteTy, DAG);
818 for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
819 Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
820 Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
821 }
822 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000823}
824
825SDValue
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000826HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
827 const {
828 // Change the type of the extracted element to i32.
829 SDValue VecV = Op.getOperand(0);
830 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000831 const SDLoc &dl(Op);
832 SDValue IdxV = Op.getOperand(1);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000833 if (ElemTy == MVT::i1)
834 return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000835
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000836 return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000837}
838
839SDValue
840HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
841 const {
842 const SDLoc &dl(Op);
843 SDValue VecV = Op.getOperand(0);
844 SDValue ValV = Op.getOperand(1);
845 SDValue IdxV = Op.getOperand(2);
846 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000847 if (ElemTy == MVT::i1)
848 return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000849
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000850 return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000851}
852
853SDValue
854HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
855 const {
856 SDValue SrcV = Op.getOperand(0);
857 MVT SrcTy = ty(SrcV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000858 MVT DstTy = ty(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000859 SDValue IdxV = Op.getOperand(1);
860 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000861 assert(Idx % DstTy.getVectorNumElements() == 0);
862 (void)Idx;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000863 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000864
865 MVT ElemTy = SrcTy.getVectorElementType();
866 if (ElemTy == MVT::i1)
867 return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
868
869 return extractHvxSubvectorReg(SrcV, IdxV, dl, DstTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000870}
871
872SDValue
873HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
874 const {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000875 // Idx does not need to be a constant.
876 SDValue VecV = Op.getOperand(0);
877 SDValue ValV = Op.getOperand(1);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000878 SDValue IdxV = Op.getOperand(2);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000879
880 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000881 MVT VecTy = ty(VecV);
882 MVT ElemTy = VecTy.getVectorElementType();
883 if (ElemTy == MVT::i1)
884 return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
885
886 return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000887}
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000888
889SDValue
890HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
891 MVT ResTy = ty(Op);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000892 assert(ResTy.isVector());
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000893 const SDLoc &dl(Op);
894 SmallVector<int,256> ShuffMask;
895
896 MVT ElemTy = ResTy.getVectorElementType();
897 unsigned VecLen = ResTy.getVectorNumElements();
898 SDValue Vs = Op.getOperand(0);
899 SDValue Vt = Op.getOperand(1);
900
901 switch (ElemTy.SimpleTy) {
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +0000902 case MVT::i8: {
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000903 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
904 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
905 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000906 MVT ExtTy = typeExtElem(ResTy, 2);
907 unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
908 : Hexagon::V6_vmpyhv;
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000909 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000910
911 // Discard high halves of the resulting values, collect the low halves.
912 for (unsigned I = 0; I < VecLen; I += 2) {
913 ShuffMask.push_back(I); // Pick even element.
914 ShuffMask.push_back(I+VecLen); // Pick odd element.
915 }
916 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
Krzysztof Parzyszek0f5d9762018-01-05 20:45:34 +0000917 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
918 return DAG.getBitcast(ResTy, BS);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000919 }
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +0000920 case MVT::i16:
921 // For i16 there is V6_vmpyih, which acts exactly like the MUL opcode.
922 // (There is also V6_vmpyhv, which behaves in an analogous way to
923 // V6_vmpybv.)
924 return getInstr(Hexagon::V6_vmpyih, dl, ResTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000925 case MVT::i32: {
926 // Use the following sequence for signed word multiply:
927 // T0 = V6_vmpyiowh Vs, Vt
928 // T1 = V6_vaslw T0, 16
929 // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
930 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000931 SDValue T0 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
932 SDValue T1 = getInstr(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
933 SDValue T2 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
934 {T1, Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000935 return T2;
936 }
937 default:
938 break;
939 }
940 return SDValue();
941}
Krzysztof Parzyszek47076052017-12-14 21:28:48 +0000942
943SDValue
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000944HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
945 MVT ResTy = ty(Op);
946 assert(ResTy.isVector());
947 const SDLoc &dl(Op);
948 SmallVector<int,256> ShuffMask;
949
950 MVT ElemTy = ResTy.getVectorElementType();
951 unsigned VecLen = ResTy.getVectorNumElements();
952 SDValue Vs = Op.getOperand(0);
953 SDValue Vt = Op.getOperand(1);
954 bool IsSigned = Op.getOpcode() == ISD::MULHS;
955
956 if (ElemTy == MVT::i8 || ElemTy == MVT::i16) {
957 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
958 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
959 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
960 // For i16, use V6_vmpyhv, which behaves in an analogous way to
961 // V6_vmpybv: results Lo and Hi are products of even/odd elements
962 // respectively.
963 MVT ExtTy = typeExtElem(ResTy, 2);
964 unsigned MpyOpc = ElemTy == MVT::i8
965 ? (IsSigned ? Hexagon::V6_vmpybv : Hexagon::V6_vmpyubv)
966 : (IsSigned ? Hexagon::V6_vmpyhv : Hexagon::V6_vmpyuhv);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000967 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000968
969 // Discard low halves of the resulting values, collect the high halves.
970 for (unsigned I = 0; I < VecLen; I += 2) {
971 ShuffMask.push_back(I+1); // Pick even element.
972 ShuffMask.push_back(I+VecLen+1); // Pick odd element.
973 }
974 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
975 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
976 return DAG.getBitcast(ResTy, BS);
977 }
978
979 assert(ElemTy == MVT::i32);
980 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
981
982 if (IsSigned) {
983 // mulhs(Vs,Vt) =
984 // = [(Hi(Vs)*2^16 + Lo(Vs)) *s (Hi(Vt)*2^16 + Lo(Vt))] >> 32
985 // = [Hi(Vs)*2^16 *s Hi(Vt)*2^16 + Hi(Vs) *su Lo(Vt)*2^16
986 // + Lo(Vs) *us (Hi(Vt)*2^16 + Lo(Vt))] >> 32
987 // = [Hi(Vs) *s Hi(Vt)*2^32 + Hi(Vs) *su Lo(Vt)*2^16
988 // + Lo(Vs) *us Vt] >> 32
989 // The low half of Lo(Vs)*Lo(Vt) will be discarded (it's not added to
990 // anything, so it cannot produce any carry over to higher bits),
991 // so everything in [] can be shifted by 16 without loss of precision.
992 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + Lo(Vs)*Vt >> 16] >> 16
993 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + V6_vmpyewuh(Vs,Vt)] >> 16
994 // Denote Hi(Vs) = Vs':
995 // = [Vs'*s Hi(Vt)*2^16 + Vs' *su Lo(Vt) + V6_vmpyewuh(Vt,Vs)] >> 16
996 // = Vs'*s Hi(Vt) + (V6_vmpyiewuh(Vs',Vt) + V6_vmpyewuh(Vt,Vs)) >> 16
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000997 SDValue T0 = getInstr(Hexagon::V6_vmpyewuh, dl, ResTy, {Vt, Vs}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000998 // Get Vs':
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000999 SDValue S0 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {Vs, S16}, DAG);
1000 SDValue T1 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1001 {T0, S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001002 // Shift by 16:
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001003 SDValue S2 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {T1, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001004 // Get Vs'*Hi(Vt):
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001005 SDValue T2 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001006 // Add:
1007 SDValue T3 = DAG.getNode(ISD::ADD, dl, ResTy, {S2, T2});
1008 return T3;
1009 }
1010
1011 // Unsigned mulhw. (Would expansion using signed mulhw be better?)
1012
1013 auto LoVec = [&DAG,ResTy,dl] (SDValue Pair) {
1014 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResTy, Pair);
1015 };
1016 auto HiVec = [&DAG,ResTy,dl] (SDValue Pair) {
1017 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResTy, Pair);
1018 };
1019
1020 MVT PairTy = typeJoin({ResTy, ResTy});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001021 SDValue P = getInstr(Hexagon::V6_lvsplatw, dl, ResTy,
1022 {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001023 // Multiply-unsigned halfwords:
1024 // LoVec = Vs.uh[2i] * Vt.uh[2i],
1025 // HiVec = Vs.uh[2i+1] * Vt.uh[2i+1]
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001026 SDValue T0 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001027 // The low halves in the LoVec of the pair can be discarded. They are
1028 // not added to anything (in the full-precision product), so they cannot
1029 // produce a carry into the higher bits.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001030 SDValue T1 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {LoVec(T0), S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001031 // Swap low and high halves in Vt, and do the halfword multiplication
1032 // to get products Vs.uh[2i] * Vt.uh[2i+1] and Vs.uh[2i+1] * Vt.uh[2i].
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001033 SDValue D0 = getInstr(Hexagon::V6_vdelta, dl, ResTy, {Vt, P}, DAG);
1034 SDValue T2 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, D0}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001035 // T2 has mixed products of halfwords: Lo(Vt)*Hi(Vs) and Hi(Vt)*Lo(Vs).
1036 // These products are words, but cannot be added directly because the
1037 // sums could overflow. Add these products, by halfwords, where each sum
1038 // of a pair of halfwords gives a word.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001039 SDValue T3 = getInstr(Hexagon::V6_vadduhw, dl, PairTy,
1040 {LoVec(T2), HiVec(T2)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001041 // Add the high halfwords from the products of the low halfwords.
1042 SDValue T4 = DAG.getNode(ISD::ADD, dl, ResTy, {T1, LoVec(T3)});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001043 SDValue T5 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {T4, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001044 SDValue T6 = DAG.getNode(ISD::ADD, dl, ResTy, {HiVec(T0), HiVec(T3)});
1045 SDValue T7 = DAG.getNode(ISD::ADD, dl, ResTy, {T5, T6});
1046 return T7;
1047}
1048
1049SDValue
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001050HexagonTargetLowering::LowerHvxSetCC(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001051 MVT ResTy = ty(Op);
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001052 MVT VecTy = ty(Op.getOperand(0));
1053 assert(VecTy == ty(Op.getOperand(1)));
Krzysztof Parzyszekb843f752018-01-31 20:46:55 +00001054 unsigned HwLen = Subtarget.getVectorLength();
1055 const SDLoc &dl(Op);
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001056
1057 SDValue Cmp = Op.getOperand(2);
1058 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
Krzysztof Parzyszekb843f752018-01-31 20:46:55 +00001059
1060 if (VecTy.getSizeInBits() == 16*HwLen) {
1061 VectorPair P0 = opSplit(Op.getOperand(0), dl, DAG);
1062 VectorPair P1 = opSplit(Op.getOperand(1), dl, DAG);
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001063 MVT HalfTy = typeSplit(ResTy).first;
Krzysztof Parzyszekb843f752018-01-31 20:46:55 +00001064
1065 SDValue V0 = DAG.getSetCC(dl, HalfTy, P0.first, P1.first, CC);
1066 SDValue V1 = DAG.getSetCC(dl, HalfTy, P0.second, P1.second, CC);
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001067 return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, V1, V0);
Krzysztof Parzyszekb843f752018-01-31 20:46:55 +00001068 }
1069
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001070 return SDValue();
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001071}
Krzysztof Parzyszek6b589e52017-12-18 18:32:27 +00001072
1073SDValue
1074HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
1075 // Sign- and zero-extends are legal.
1076 assert(Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG);
1077 return DAG.getZeroExtendVectorInReg(Op.getOperand(0), SDLoc(Op), ty(Op));
1078}
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001079
1080SDValue
1081HexagonTargetLowering::LowerHvxShift(SDValue Op, SelectionDAG &DAG) const {
1082 return Op;
1083}
1084