blob: 291e3858012b6c4ec14a1cb307c248726b350c44 [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));
Krzysztof Parzyszek1d52a852018-02-06 15:15:13 +000078 if (Vec.getOpcode() == HexagonISD::QCAT)
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +000079 return VectorPair(Vec.getOperand(0), Vec.getOperand(1));
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +000080 return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
81}
82
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +000083bool
84HexagonTargetLowering::isHvxSingleTy(MVT Ty) const {
85 return Subtarget.isHVXVectorType(Ty) &&
86 Ty.getSizeInBits() == 8 * Subtarget.getVectorLength();
87}
88
89bool
90HexagonTargetLowering::isHvxPairTy(MVT Ty) const {
91 return Subtarget.isHVXVectorType(Ty) &&
92 Ty.getSizeInBits() == 16 * Subtarget.getVectorLength();
93}
94
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +000095SDValue
96HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
97 SelectionDAG &DAG) const {
98 if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
99 ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
100
101 unsigned ElemWidth = ElemTy.getSizeInBits();
102 if (ElemWidth == 8)
103 return ElemIdx;
104
105 unsigned L = Log2_32(ElemWidth/8);
106 const SDLoc &dl(ElemIdx);
107 return DAG.getNode(ISD::SHL, dl, MVT::i32,
108 {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
109}
110
111SDValue
112HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
113 SelectionDAG &DAG) const {
114 unsigned ElemWidth = ElemTy.getSizeInBits();
115 assert(ElemWidth >= 8 && ElemWidth <= 32);
116 if (ElemWidth == 32)
117 return Idx;
118
119 if (ty(Idx) != MVT::i32)
120 Idx = DAG.getBitcast(MVT::i32, Idx);
121 const SDLoc &dl(Idx);
122 SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
123 SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
124 return SubIdx;
125}
126
127SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000128HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
129 SDValue Op1, ArrayRef<int> Mask,
130 SelectionDAG &DAG) const {
131 MVT OpTy = ty(Op0);
132 assert(OpTy == ty(Op1));
133
134 MVT ElemTy = OpTy.getVectorElementType();
135 if (ElemTy == MVT::i8)
136 return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
137 assert(ElemTy.getSizeInBits() >= 8);
138
139 MVT ResTy = tyVector(OpTy, MVT::i8);
140 unsigned ElemSize = ElemTy.getSizeInBits() / 8;
141
142 SmallVector<int,128> ByteMask;
143 for (int M : Mask) {
144 if (M < 0) {
145 for (unsigned I = 0; I != ElemSize; ++I)
146 ByteMask.push_back(-1);
147 } else {
148 int NewM = M*ElemSize;
149 for (unsigned I = 0; I != ElemSize; ++I)
150 ByteMask.push_back(NewM+I);
151 }
152 }
153 assert(ResTy.getVectorNumElements() == ByteMask.size());
154 return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
155 opCastElem(Op1, MVT::i8, DAG), ByteMask);
156}
157
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000158SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000159HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
160 const SDLoc &dl, MVT VecTy,
161 SelectionDAG &DAG) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000162 unsigned VecLen = Values.size();
163 MachineFunction &MF = DAG.getMachineFunction();
164 MVT ElemTy = VecTy.getVectorElementType();
165 unsigned ElemWidth = ElemTy.getSizeInBits();
166 unsigned HwLen = Subtarget.getVectorLength();
167
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000168 unsigned ElemSize = ElemWidth / 8;
169 assert(ElemSize*VecLen == HwLen);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000170 SmallVector<SDValue,32> Words;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000171
172 if (VecTy.getVectorElementType() != MVT::i32) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000173 assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
174 unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
175 MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000176 for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
177 SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000178 Words.push_back(DAG.getBitcast(MVT::i32, W));
179 }
180 } else {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000181 Words.assign(Values.begin(), Values.end());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000182 }
183
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000184 unsigned NumWords = Words.size();
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000185 bool IsSplat = true, IsUndef = true;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000186 SDValue SplatV;
187 for (unsigned i = 0; i != NumWords && IsSplat; ++i) {
188 if (isUndef(Words[i]))
189 continue;
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000190 IsUndef = false;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000191 if (!SplatV.getNode())
192 SplatV = Words[i];
193 else if (SplatV != Words[i])
194 IsSplat = false;
195 }
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000196 if (IsUndef)
197 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000198 if (IsSplat) {
199 assert(SplatV.getNode());
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000200 auto *IdxN = dyn_cast<ConstantSDNode>(SplatV.getNode());
201 if (IdxN && IdxN->isNullValue())
202 return getZero(dl, VecTy, DAG);
203 MVT WordTy = MVT::getVectorVT(MVT::i32, HwLen/4);
204 SDValue SV = DAG.getNode(HexagonISD::VSPLAT, dl, WordTy, SplatV);
205 return DAG.getBitcast(VecTy, SV);
206 }
207
208 // Delay recognizing constant vectors until here, so that we can generate
209 // a vsplat.
210 SmallVector<ConstantInt*, 128> Consts(VecLen);
211 bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
212 if (AllConst) {
213 ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
214 (Constant**)Consts.end());
215 Constant *CV = ConstantVector::get(Tmp);
216 unsigned Align = HwLen;
217 SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
218 return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
219 MachinePointerInfo::getConstantPool(MF), Align);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000220 }
221
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000222 // Construct two halves in parallel, then or them together.
223 assert(4*Words.size() == Subtarget.getVectorLength());
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000224 SDValue HalfV0 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
225 SDValue HalfV1 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000226 SDValue S = DAG.getConstant(4, dl, MVT::i32);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000227 for (unsigned i = 0; i != NumWords/2; ++i) {
228 SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
229 {HalfV0, Words[i]});
230 SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
231 {HalfV1, Words[i+NumWords/2]});
232 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
233 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
234 }
235
236 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
237 {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
238 SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
239 return DstV;
240}
241
242SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000243HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
244 unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
245 MVT PredTy = ty(PredV);
246 unsigned HwLen = Subtarget.getVectorLength();
247 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
248
249 if (Subtarget.isHVXVectorType(PredTy, true)) {
250 // Move the vector predicate SubV to a vector register, and scale it
251 // down to match the representation (bytes per type element) that VecV
252 // uses. The scaling down will pick every 2nd or 4th (every Scale-th
Hiroshi Inoue0909ca12018-01-26 08:15:29 +0000253 // in general) element and put them at the front of the resulting
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000254 // vector. This subvector will then be inserted into the Q2V of VecV.
255 // To avoid having an operation that generates an illegal type (short
256 // vector), generate a full size vector.
257 //
258 SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
259 SmallVector<int,128> Mask(HwLen);
260 // Scale = BitBytes(PredV) / Given BitBytes.
261 unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
262 unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
263
264 for (unsigned i = 0; i != HwLen; ++i) {
265 unsigned Num = i % Scale;
266 unsigned Off = i / Scale;
267 Mask[BlockLen*Num + Off] = i;
268 }
269 SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
270 if (!ZeroFill)
271 return S;
272 // Fill the bytes beyond BlockLen with 0s.
273 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000274 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
275 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000276 SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
277 return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
278 }
279
280 // Make sure that this is a valid scalar predicate.
281 assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
282
283 unsigned Bytes = 8 / PredTy.getVectorNumElements();
284 SmallVector<SDValue,4> Words[2];
285 unsigned IdxW = 0;
286
287 auto Lo32 = [&DAG, &dl] (SDValue P) {
288 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, P);
289 };
290 auto Hi32 = [&DAG, &dl] (SDValue P) {
291 return DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, P);
292 };
293
294 SDValue W0 = isUndef(PredV)
295 ? DAG.getUNDEF(MVT::i64)
296 : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
297 Words[IdxW].push_back(Hi32(W0));
298 Words[IdxW].push_back(Lo32(W0));
299
300 while (Bytes < BitBytes) {
301 IdxW ^= 1;
302 Words[IdxW].clear();
303
304 if (Bytes < 4) {
305 for (const SDValue &W : Words[IdxW ^ 1]) {
306 SDValue T = expandPredicate(W, dl, DAG);
307 Words[IdxW].push_back(Hi32(T));
308 Words[IdxW].push_back(Lo32(T));
309 }
310 } else {
311 for (const SDValue &W : Words[IdxW ^ 1]) {
312 Words[IdxW].push_back(W);
313 Words[IdxW].push_back(W);
314 }
315 }
316 Bytes *= 2;
317 }
318
319 assert(Bytes == BitBytes);
320
321 SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
322 SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
323 for (const SDValue &W : Words[IdxW]) {
324 Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
325 Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
326 }
327
328 return Vec;
329}
330
331SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000332HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
333 const SDLoc &dl, MVT VecTy,
334 SelectionDAG &DAG) const {
335 // Construct a vector V of bytes, such that a comparison V >u 0 would
336 // produce the required vector predicate.
337 unsigned VecLen = Values.size();
338 unsigned HwLen = Subtarget.getVectorLength();
339 assert(VecLen <= HwLen || VecLen == 8*HwLen);
340 SmallVector<SDValue,128> Bytes;
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000341 bool AllT = true, AllF = true;
342
343 auto IsTrue = [] (SDValue V) {
344 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
345 return !N->isNullValue();
346 return false;
347 };
348 auto IsFalse = [] (SDValue V) {
349 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
350 return N->isNullValue();
351 return false;
352 };
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000353
354 if (VecLen <= HwLen) {
355 // In the hardware, each bit of a vector predicate corresponds to a byte
356 // of a vector register. Calculate how many bytes does a bit of VecTy
357 // correspond to.
358 assert(HwLen % VecLen == 0);
359 unsigned BitBytes = HwLen / VecLen;
360 for (SDValue V : Values) {
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000361 AllT &= IsTrue(V);
362 AllF &= IsFalse(V);
363
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000364 SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000365 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000366 for (unsigned B = 0; B != BitBytes; ++B)
367 Bytes.push_back(Ext);
368 }
369 } else {
370 // There are as many i1 values, as there are bits in a vector register.
371 // Divide the values into groups of 8 and check that each group consists
372 // of the same value (ignoring undefs).
373 for (unsigned I = 0; I != VecLen; I += 8) {
374 unsigned B = 0;
375 // Find the first non-undef value in this group.
376 for (; B != 8; ++B) {
377 if (!Values[I+B].isUndef())
378 break;
379 }
380 SDValue F = Values[I+B];
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000381 AllT &= IsTrue(F);
382 AllF &= IsFalse(F);
383
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000384 SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000385 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000386 Bytes.push_back(Ext);
387 // Verify that the rest of values in the group are the same as the
388 // first.
389 for (; B != 8; ++B)
390 assert(Values[I+B].isUndef() || Values[I+B] == F);
391 }
392 }
393
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000394 if (AllT)
395 return DAG.getNode(HexagonISD::QTRUE, dl, VecTy);
396 if (AllF)
397 return DAG.getNode(HexagonISD::QFALSE, dl, VecTy);
398
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000399 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000400 SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
401 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
402}
403
404SDValue
405HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
406 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
407 MVT ElemTy = ty(VecV).getVectorElementType();
408
409 unsigned ElemWidth = ElemTy.getSizeInBits();
410 assert(ElemWidth >= 8 && ElemWidth <= 32);
411 (void)ElemWidth;
412
413 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
414 SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
415 {VecV, ByteIdx});
416 if (ElemTy == MVT::i32)
417 return ExWord;
418
419 // Have an extracted word, need to extract the smaller element out of it.
420 // 1. Extract the bits of (the original) IdxV that correspond to the index
421 // of the desired element in the 32-bit word.
422 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
423 // 2. Extract the element from the word.
424 SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
425 return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
426}
427
428SDValue
429HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
430 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
431 // Implement other return types if necessary.
432 assert(ResTy == MVT::i1);
433
434 unsigned HwLen = Subtarget.getVectorLength();
435 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
436 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
437
438 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
439 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
440 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
441
442 SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
443 SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000444 return getInstr(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000445}
446
447SDValue
448HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
449 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
450 MVT ElemTy = ty(VecV).getVectorElementType();
451
452 unsigned ElemWidth = ElemTy.getSizeInBits();
453 assert(ElemWidth >= 8 && ElemWidth <= 32);
454 (void)ElemWidth;
455
456 auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
457 SDValue ByteIdxV) {
458 MVT VecTy = ty(VecV);
459 unsigned HwLen = Subtarget.getVectorLength();
460 SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
461 {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
462 SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
463 SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
464 SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
465 {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
466 SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
467 return TorV;
468 };
469
470 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
471 if (ElemTy == MVT::i32)
472 return InsertWord(VecV, ValV, ByteIdx);
473
474 // If this is not inserting a 32-bit word, convert it into such a thing.
475 // 1. Extract the existing word from the target vector.
476 SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
477 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
478 SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
479 dl, MVT::i32, DAG);
480
481 // 2. Treating the extracted word as a 32-bit vector, insert the given
482 // value into it.
483 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
484 MVT SubVecTy = tyVector(ty(Ext), ElemTy);
485 SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
486 ValV, SubIdx, dl, ElemTy, DAG);
487
488 // 3. Insert the 32-bit word back into the original vector.
489 return InsertWord(VecV, Ins, ByteIdx);
490}
491
492SDValue
493HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
494 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
495 unsigned HwLen = Subtarget.getVectorLength();
496 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
497 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
498
499 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
500 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
501 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
502 ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
503
504 SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
505 return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
506}
507
508SDValue
509HexagonTargetLowering::extractHvxSubvectorReg(SDValue VecV, SDValue IdxV,
510 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
511 MVT VecTy = ty(VecV);
512 unsigned HwLen = Subtarget.getVectorLength();
513 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
514 MVT ElemTy = VecTy.getVectorElementType();
515 unsigned ElemWidth = ElemTy.getSizeInBits();
516
517 // If the source vector is a vector pair, get the single vector containing
518 // the subvector of interest. The subvector will never overlap two single
519 // vectors.
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000520 if (isHvxPairTy(VecTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000521 unsigned SubIdx;
522 if (Idx * ElemWidth >= 8*HwLen) {
523 SubIdx = Hexagon::vsub_hi;
524 Idx -= VecTy.getVectorNumElements() / 2;
525 } else {
526 SubIdx = Hexagon::vsub_lo;
527 }
528 VecTy = typeSplit(VecTy).first;
529 VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
530 if (VecTy == ResTy)
531 return VecV;
532 }
533
534 // The only meaningful subvectors of a single HVX vector are those that
535 // fit in a scalar register.
536 assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
537
538 MVT WordTy = tyVector(VecTy, MVT::i32);
539 SDValue WordVec = DAG.getBitcast(WordTy, VecV);
540 unsigned WordIdx = (Idx*ElemWidth) / 32;
541
542 SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
543 SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
544 if (ResTy.getSizeInBits() == 32)
545 return DAG.getBitcast(ResTy, W0);
546
547 SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
548 SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
549 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64, {W1, W0});
550 return DAG.getBitcast(ResTy, WW);
551}
552
553SDValue
554HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
555 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
556 MVT VecTy = ty(VecV);
557 unsigned HwLen = Subtarget.getVectorLength();
558 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
559 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
560 // IdxV is required to be a constant.
561 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
562
563 unsigned ResLen = ResTy.getVectorNumElements();
564 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
565 unsigned Offset = Idx * BitBytes;
566 SDValue Undef = DAG.getUNDEF(ByteTy);
567 SmallVector<int,128> Mask;
568
569 if (Subtarget.isHVXVectorType(ResTy, true)) {
570 // Converting between two vector predicates. Since the result is shorter
571 // than the source, it will correspond to a vector predicate with the
572 // relevant bits replicated. The replication count is the ratio of the
573 // source and target vector lengths.
574 unsigned Rep = VecTy.getVectorNumElements() / ResLen;
575 assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
576 for (unsigned i = 0; i != HwLen/Rep; ++i) {
577 for (unsigned j = 0; j != Rep; ++j)
578 Mask.push_back(i + Offset);
579 }
580 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
581 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
582 }
583
584 // Converting between a vector predicate and a scalar predicate. In the
585 // vector predicate, a group of BitBytes bits will correspond to a single
586 // i1 element of the source vector type. Those bits will all have the same
587 // value. The same will be true for ByteVec, where each byte corresponds
588 // to a bit in the vector predicate.
589 // The algorithm is to traverse the ByteVec, going over the i1 values from
590 // the source vector, and generate the corresponding representation in an
591 // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
592 // elements so that the interesting 8 bytes will be in the low end of the
593 // vector.
594 unsigned Rep = 8 / ResLen;
595 // Make sure the output fill the entire vector register, so repeat the
596 // 8-byte groups as many times as necessary.
597 for (unsigned r = 0; r != HwLen/ResLen; ++r) {
598 // This will generate the indexes of the 8 interesting bytes.
599 for (unsigned i = 0; i != ResLen; ++i) {
600 for (unsigned j = 0; j != Rep; ++j)
601 Mask.push_back(Offset + i*BitBytes);
602 }
603 }
604
605 SDValue Zero = getZero(dl, MVT::i32, DAG);
606 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
607 // Combine the two low words from ShuffV into a v8i8, and byte-compare
608 // them against 0.
609 SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
610 SDValue W1 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
611 {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
612 SDValue Vec64 = DAG.getNode(HexagonISD::COMBINE, dl, MVT::v8i8, {W1, W0});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000613 return getInstr(Hexagon::A4_vcmpbgtui, dl, ResTy,
614 {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000615}
616
617SDValue
618HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
619 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
620 MVT VecTy = ty(VecV);
621 MVT SubTy = ty(SubV);
622 unsigned HwLen = Subtarget.getVectorLength();
623 MVT ElemTy = VecTy.getVectorElementType();
624 unsigned ElemWidth = ElemTy.getSizeInBits();
625
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000626 bool IsPair = isHvxPairTy(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000627 MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
628 // The two single vectors that VecV consists of, if it's a pair.
629 SDValue V0, V1;
630 SDValue SingleV = VecV;
631 SDValue PickHi;
632
633 if (IsPair) {
634 V0 = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, VecV);
635 V1 = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, VecV);
636
637 SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
638 dl, MVT::i32);
639 PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000640 if (isHvxSingleTy(SubTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000641 if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
642 unsigned Idx = CN->getZExtValue();
643 assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
644 unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
645 return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
646 }
647 // If IdxV is not a constant, generate the two variants: with the
648 // SubV as the high and as the low subregister, and select the right
649 // pair based on the IdxV.
650 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
651 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
652 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
653 }
654 // The subvector being inserted must be entirely contained in one of
655 // the vectors V0 or V1. Set SingleV to the correct one, and update
656 // IdxV to be the index relative to the beginning of that vector.
657 SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
658 IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
659 SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
660 }
661
662 // The only meaningful subvectors of a single HVX vector are those that
663 // fit in a scalar register.
664 assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
665 // Convert IdxV to be index in bytes.
666 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
667 if (!IdxN || !IdxN->isNullValue()) {
668 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
669 DAG.getConstant(ElemWidth/8, dl, MVT::i32));
670 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
671 }
672 // When inserting a single word, the rotation back to the original position
673 // would be by HwLen-Idx, but if two words are inserted, it will need to be
674 // by (HwLen-4)-Idx.
675 unsigned RolBase = HwLen;
676 if (VecTy.getSizeInBits() == 32) {
677 SDValue V = DAG.getBitcast(MVT::i32, SubV);
678 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, V);
679 } else {
680 SDValue V = DAG.getBitcast(MVT::i64, SubV);
681 SDValue R0 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, V);
682 SDValue R1 = DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, V);
683 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
684 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
685 DAG.getConstant(4, dl, MVT::i32));
686 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
687 RolBase = HwLen-4;
688 }
689 // If the vector wasn't ror'ed, don't ror it back.
690 if (RolBase != 4 || !IdxN || !IdxN->isNullValue()) {
691 SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
692 DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
693 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
694 }
695
696 if (IsPair) {
697 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
698 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
699 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
700 }
701 return SingleV;
702}
703
704SDValue
705HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
706 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
707 MVT VecTy = ty(VecV);
708 MVT SubTy = ty(SubV);
709 assert(Subtarget.isHVXVectorType(VecTy, true));
710 // VecV is an HVX vector predicate. SubV may be either an HVX vector
711 // predicate as well, or it can be a scalar predicate.
712
713 unsigned VecLen = VecTy.getVectorNumElements();
714 unsigned HwLen = Subtarget.getVectorLength();
715 assert(HwLen % VecLen == 0 && "Unexpected vector type");
716
717 unsigned Scale = VecLen / SubTy.getVectorNumElements();
718 unsigned BitBytes = HwLen / VecLen;
719 unsigned BlockLen = HwLen / Scale;
720
721 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
722 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
723 SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
724 SDValue ByteIdx;
725
726 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
727 if (!IdxN || !IdxN->isNullValue()) {
728 ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
729 DAG.getConstant(BitBytes, dl, MVT::i32));
730 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
731 }
732
733 // ByteVec is the target vector VecV rotated in such a way that the
734 // subvector should be inserted at index 0. Generate a predicate mask
735 // and use vmux to do the insertion.
736 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000737 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
738 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
739 ByteVec = getInstr(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000740 // Rotate ByteVec back, and convert to a vector predicate.
741 if (!IdxN || !IdxN->isNullValue()) {
742 SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
743 SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
744 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
745 }
746 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
747}
748
749SDValue
750HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
751 MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
752 // Sign- and any-extending of a vector predicate to a vector register is
753 // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
754 // a vector of 1s (where the 1s are of type matching the vector type).
755 assert(Subtarget.isHVXVectorType(ResTy));
756 if (!ZeroExt)
757 return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
758
759 assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
760 SDValue True = DAG.getNode(HexagonISD::VSPLAT, dl, ResTy,
761 DAG.getConstant(1, dl, MVT::i32));
762 SDValue False = getZero(dl, ResTy, DAG);
763 return DAG.getSelect(dl, ResTy, VecV, True, False);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000764}
765
766SDValue
767HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
768 const {
769 const SDLoc &dl(Op);
770 MVT VecTy = ty(Op);
771
772 unsigned Size = Op.getNumOperands();
773 SmallVector<SDValue,128> Ops;
774 for (unsigned i = 0; i != Size; ++i)
775 Ops.push_back(Op.getOperand(i));
776
777 if (VecTy.getVectorElementType() == MVT::i1)
778 return buildHvxVectorPred(Ops, dl, VecTy, DAG);
779
780 if (VecTy.getSizeInBits() == 16*Subtarget.getVectorLength()) {
781 ArrayRef<SDValue> A(Ops);
782 MVT SingleTy = typeSplit(VecTy).first;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000783 SDValue V0 = buildHvxVectorReg(A.take_front(Size/2), dl, SingleTy, DAG);
784 SDValue V1 = buildHvxVectorReg(A.drop_front(Size/2), dl, SingleTy, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000785 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
786 }
787
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000788 return buildHvxVectorReg(Ops, dl, VecTy, DAG);
789}
790
791SDValue
792HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
793 const {
794 // This should only be called for vectors of i1. The "scalar" vector
795 // concatenation does not need special lowering (assuming that only
796 // two vectors are concatenated at a time).
797 MVT VecTy = ty(Op);
798 assert(VecTy.getVectorElementType() == MVT::i1);
799
800 const SDLoc &dl(Op);
801 unsigned HwLen = Subtarget.getVectorLength();
802 unsigned NumOp = Op.getNumOperands();
803 assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +0000804
805 SDValue Op0 = Op.getOperand(0);
806
807 // If the operands are HVX types (i.e. not scalar predicates), then
808 // defer the concatenation, and create QCAT instead.
809 if (Subtarget.isHVXVectorType(ty(Op0), true)) {
810 if (NumOp == 2)
811 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, Op0, Op.getOperand(1));
812
813 ArrayRef<SDUse> U(Op.getNode()->ops());
814 SmallVector<SDValue,4> SV(U.begin(), U.end());
815 ArrayRef<SDValue> Ops(SV);
816
817 MVT HalfTy = typeSplit(VecTy).first;
818 SDValue V0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
819 Ops.take_front(NumOp/2));
820 SDValue V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
821 Ops.take_back(NumOp/2));
822 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, V0, V1);
823 }
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000824
825 // Count how many bytes (in a vector register) each bit in VecTy
826 // corresponds to.
827 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
828
829 SmallVector<SDValue,8> Prefixes;
830 for (SDValue V : Op.getNode()->op_values()) {
831 SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
832 Prefixes.push_back(P);
833 }
834
835 unsigned InpLen = ty(Op.getOperand(0)).getVectorNumElements();
836 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
837 SDValue S = DAG.getConstant(InpLen*BitBytes, dl, MVT::i32);
838 SDValue Res = getZero(dl, ByteTy, DAG);
839 for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
840 Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
841 Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
842 }
843 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000844}
845
846SDValue
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000847HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
848 const {
849 // Change the type of the extracted element to i32.
850 SDValue VecV = Op.getOperand(0);
851 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000852 const SDLoc &dl(Op);
853 SDValue IdxV = Op.getOperand(1);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000854 if (ElemTy == MVT::i1)
855 return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000856
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000857 return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000858}
859
860SDValue
861HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
862 const {
863 const SDLoc &dl(Op);
864 SDValue VecV = Op.getOperand(0);
865 SDValue ValV = Op.getOperand(1);
866 SDValue IdxV = Op.getOperand(2);
867 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000868 if (ElemTy == MVT::i1)
869 return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000870
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000871 return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000872}
873
874SDValue
875HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
876 const {
877 SDValue SrcV = Op.getOperand(0);
878 MVT SrcTy = ty(SrcV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000879 MVT DstTy = ty(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000880 SDValue IdxV = Op.getOperand(1);
881 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000882 assert(Idx % DstTy.getVectorNumElements() == 0);
883 (void)Idx;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000884 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000885
886 MVT ElemTy = SrcTy.getVectorElementType();
887 if (ElemTy == MVT::i1)
888 return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
889
890 return extractHvxSubvectorReg(SrcV, IdxV, dl, DstTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000891}
892
893SDValue
894HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
895 const {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000896 // Idx does not need to be a constant.
897 SDValue VecV = Op.getOperand(0);
898 SDValue ValV = Op.getOperand(1);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000899 SDValue IdxV = Op.getOperand(2);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000900
901 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000902 MVT VecTy = ty(VecV);
903 MVT ElemTy = VecTy.getVectorElementType();
904 if (ElemTy == MVT::i1)
905 return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
906
907 return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000908}
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000909
910SDValue
911HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
912 MVT ResTy = ty(Op);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +0000913 assert(ResTy.isVector() && isHvxSingleTy(ResTy));
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000914 const SDLoc &dl(Op);
915 SmallVector<int,256> ShuffMask;
916
917 MVT ElemTy = ResTy.getVectorElementType();
918 unsigned VecLen = ResTy.getVectorNumElements();
919 SDValue Vs = Op.getOperand(0);
920 SDValue Vt = Op.getOperand(1);
921
922 switch (ElemTy.SimpleTy) {
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +0000923 case MVT::i8: {
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000924 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
925 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
926 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000927 MVT ExtTy = typeExtElem(ResTy, 2);
928 unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
929 : Hexagon::V6_vmpyhv;
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000930 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000931
932 // Discard high halves of the resulting values, collect the low halves.
933 for (unsigned I = 0; I < VecLen; I += 2) {
934 ShuffMask.push_back(I); // Pick even element.
935 ShuffMask.push_back(I+VecLen); // Pick odd element.
936 }
937 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
Krzysztof Parzyszek0f5d9762018-01-05 20:45:34 +0000938 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
939 return DAG.getBitcast(ResTy, BS);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000940 }
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +0000941 case MVT::i16:
942 // For i16 there is V6_vmpyih, which acts exactly like the MUL opcode.
943 // (There is also V6_vmpyhv, which behaves in an analogous way to
944 // V6_vmpybv.)
945 return getInstr(Hexagon::V6_vmpyih, dl, ResTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000946 case MVT::i32: {
947 // Use the following sequence for signed word multiply:
948 // T0 = V6_vmpyiowh Vs, Vt
949 // T1 = V6_vaslw T0, 16
950 // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
951 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000952 SDValue T0 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
953 SDValue T1 = getInstr(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
954 SDValue T2 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
955 {T1, Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000956 return T2;
957 }
958 default:
959 break;
960 }
961 return SDValue();
962}
Krzysztof Parzyszek47076052017-12-14 21:28:48 +0000963
964SDValue
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000965HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
966 MVT ResTy = ty(Op);
967 assert(ResTy.isVector());
968 const SDLoc &dl(Op);
969 SmallVector<int,256> ShuffMask;
970
971 MVT ElemTy = ResTy.getVectorElementType();
972 unsigned VecLen = ResTy.getVectorNumElements();
973 SDValue Vs = Op.getOperand(0);
974 SDValue Vt = Op.getOperand(1);
975 bool IsSigned = Op.getOpcode() == ISD::MULHS;
976
977 if (ElemTy == MVT::i8 || ElemTy == MVT::i16) {
978 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
979 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
980 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
981 // For i16, use V6_vmpyhv, which behaves in an analogous way to
982 // V6_vmpybv: results Lo and Hi are products of even/odd elements
983 // respectively.
984 MVT ExtTy = typeExtElem(ResTy, 2);
985 unsigned MpyOpc = ElemTy == MVT::i8
986 ? (IsSigned ? Hexagon::V6_vmpybv : Hexagon::V6_vmpyubv)
987 : (IsSigned ? Hexagon::V6_vmpyhv : Hexagon::V6_vmpyuhv);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000988 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +0000989
990 // Discard low halves of the resulting values, collect the high halves.
991 for (unsigned I = 0; I < VecLen; I += 2) {
992 ShuffMask.push_back(I+1); // Pick even element.
993 ShuffMask.push_back(I+VecLen+1); // Pick odd element.
994 }
995 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
996 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
997 return DAG.getBitcast(ResTy, BS);
998 }
999
1000 assert(ElemTy == MVT::i32);
1001 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
1002
1003 if (IsSigned) {
1004 // mulhs(Vs,Vt) =
1005 // = [(Hi(Vs)*2^16 + Lo(Vs)) *s (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1006 // = [Hi(Vs)*2^16 *s Hi(Vt)*2^16 + Hi(Vs) *su Lo(Vt)*2^16
1007 // + Lo(Vs) *us (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1008 // = [Hi(Vs) *s Hi(Vt)*2^32 + Hi(Vs) *su Lo(Vt)*2^16
1009 // + Lo(Vs) *us Vt] >> 32
1010 // The low half of Lo(Vs)*Lo(Vt) will be discarded (it's not added to
1011 // anything, so it cannot produce any carry over to higher bits),
1012 // so everything in [] can be shifted by 16 without loss of precision.
1013 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + Lo(Vs)*Vt >> 16] >> 16
1014 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + V6_vmpyewuh(Vs,Vt)] >> 16
1015 // Denote Hi(Vs) = Vs':
1016 // = [Vs'*s Hi(Vt)*2^16 + Vs' *su Lo(Vt) + V6_vmpyewuh(Vt,Vs)] >> 16
1017 // = Vs'*s Hi(Vt) + (V6_vmpyiewuh(Vs',Vt) + V6_vmpyewuh(Vt,Vs)) >> 16
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001018 SDValue T0 = getInstr(Hexagon::V6_vmpyewuh, dl, ResTy, {Vt, Vs}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001019 // Get Vs':
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001020 SDValue S0 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {Vs, S16}, DAG);
1021 SDValue T1 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1022 {T0, S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001023 // Shift by 16:
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001024 SDValue S2 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {T1, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001025 // Get Vs'*Hi(Vt):
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001026 SDValue T2 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001027 // Add:
1028 SDValue T3 = DAG.getNode(ISD::ADD, dl, ResTy, {S2, T2});
1029 return T3;
1030 }
1031
1032 // Unsigned mulhw. (Would expansion using signed mulhw be better?)
1033
1034 auto LoVec = [&DAG,ResTy,dl] (SDValue Pair) {
1035 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResTy, Pair);
1036 };
1037 auto HiVec = [&DAG,ResTy,dl] (SDValue Pair) {
1038 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResTy, Pair);
1039 };
1040
1041 MVT PairTy = typeJoin({ResTy, ResTy});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001042 SDValue P = getInstr(Hexagon::V6_lvsplatw, dl, ResTy,
1043 {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001044 // Multiply-unsigned halfwords:
1045 // LoVec = Vs.uh[2i] * Vt.uh[2i],
1046 // HiVec = Vs.uh[2i+1] * Vt.uh[2i+1]
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001047 SDValue T0 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001048 // The low halves in the LoVec of the pair can be discarded. They are
1049 // not added to anything (in the full-precision product), so they cannot
1050 // produce a carry into the higher bits.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001051 SDValue T1 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {LoVec(T0), S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001052 // Swap low and high halves in Vt, and do the halfword multiplication
1053 // 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 +00001054 SDValue D0 = getInstr(Hexagon::V6_vdelta, dl, ResTy, {Vt, P}, DAG);
1055 SDValue T2 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, D0}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001056 // T2 has mixed products of halfwords: Lo(Vt)*Hi(Vs) and Hi(Vt)*Lo(Vs).
1057 // These products are words, but cannot be added directly because the
1058 // sums could overflow. Add these products, by halfwords, where each sum
1059 // of a pair of halfwords gives a word.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001060 SDValue T3 = getInstr(Hexagon::V6_vadduhw, dl, PairTy,
1061 {LoVec(T2), HiVec(T2)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001062 // Add the high halfwords from the products of the low halfwords.
1063 SDValue T4 = DAG.getNode(ISD::ADD, dl, ResTy, {T1, LoVec(T3)});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001064 SDValue T5 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {T4, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001065 SDValue T6 = DAG.getNode(ISD::ADD, dl, ResTy, {HiVec(T0), HiVec(T3)});
1066 SDValue T7 = DAG.getNode(ISD::ADD, dl, ResTy, {T5, T6});
1067 return T7;
1068}
1069
1070SDValue
Krzysztof Parzyszek6b589e52017-12-18 18:32:27 +00001071HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
1072 // Sign- and zero-extends are legal.
1073 assert(Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG);
1074 return DAG.getZeroExtendVectorInReg(Op.getOperand(0), SDLoc(Op), ty(Op));
1075}
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001076
1077SDValue
1078HexagonTargetLowering::LowerHvxShift(SDValue Op, SelectionDAG &DAG) const {
1079 return Op;
1080}
1081
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001082SDValue
1083HexagonTargetLowering::SplitHvxPairOp(SDValue Op, SelectionDAG &DAG) const {
1084 assert(!Op.isMachineOpcode());
1085 SmallVector<SDValue,2> OpsL, OpsH;
1086 const SDLoc &dl(Op);
1087
1088 auto SplitVTNode = [&DAG,this] (const VTSDNode *N) {
1089 MVT Ty = typeSplit(N->getVT().getSimpleVT()).first;
1090 SDValue TV = DAG.getValueType(Ty);
1091 return std::make_pair(TV, TV);
1092 };
1093
1094 for (SDValue A : Op.getNode()->ops()) {
1095 VectorPair P = Subtarget.isHVXVectorType(ty(A), true)
1096 ? opSplit(A, dl, DAG)
1097 : std::make_pair(A, A);
1098 // Special case for type operand.
1099 if (Op.getOpcode() == ISD::SIGN_EXTEND_INREG) {
1100 if (const auto *N = dyn_cast<const VTSDNode>(A.getNode()))
1101 P = SplitVTNode(N);
1102 }
1103 OpsL.push_back(P.first);
1104 OpsH.push_back(P.second);
1105 }
1106
1107 MVT ResTy = ty(Op);
1108 MVT HalfTy = typeSplit(ResTy).first;
1109 SDValue L = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsL);
1110 SDValue H = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsH);
1111 SDValue S = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, L, H);
1112 return S;
1113}
1114
1115SDValue
1116HexagonTargetLowering::LowerHvxOperation(SDValue Op, SelectionDAG &DAG) const {
1117 unsigned Opc = Op.getOpcode();
1118 bool IsPairOp = isHvxPairTy(ty(Op)) ||
1119 llvm::any_of(Op.getNode()->ops(), [this] (SDValue V) {
1120 return isHvxPairTy(ty(V));
1121 });
1122
1123 if (IsPairOp) {
1124 switch (Opc) {
1125 default:
1126 break;
1127 case ISD::MUL:
1128 case ISD::MULHS:
1129 case ISD::MULHU:
1130 case ISD::AND:
1131 case ISD::OR:
1132 case ISD::XOR:
1133 case ISD::SRA:
1134 case ISD::SHL:
1135 case ISD::SRL:
1136 case ISD::SETCC:
1137 case ISD::VSELECT:
1138 case ISD::SIGN_EXTEND_INREG:
1139 return SplitHvxPairOp(Op, DAG);
1140 }
1141 }
1142
1143 switch (Opc) {
1144 default:
1145 break;
1146 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
1147 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
1148 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
1149 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
1150 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
1151 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
1152 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
1153 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
1154 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
1155 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
1156 case ISD::SRA:
1157 case ISD::SHL:
1158 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
1159 case ISD::MUL: return LowerHvxMul(Op, DAG);
1160 case ISD::MULHS:
1161 case ISD::MULHU: return LowerHvxMulh(Op, DAG);
1162 case ISD::ANY_EXTEND_VECTOR_INREG: return LowerHvxExtend(Op, DAG);
1163 case ISD::SETCC:
1164 case ISD::INTRINSIC_VOID: return Op;
1165 }
1166#ifndef NDEBUG
1167 Op.dumpr(&DAG);
1168#endif
1169 llvm_unreachable("Unhandled HVX operation");
1170}
1171
1172bool
1173HexagonTargetLowering::isHvxOperation(SDValue Op) const {
1174 // If the type of the result, or any operand type are HVX vector types,
1175 // this is an HVX operation.
1176 return Subtarget.isHVXVectorType(ty(Op)) ||
1177 llvm::any_of(Op.getNode()->ops(),
1178 [this] (SDValue V) {
1179 return Subtarget.isHVXVectorType(ty(V), true);
1180 });
1181}