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