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