blob: b9e285131b35b5353e1015c0ba160d4a804ea935 [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"
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +000013#include "llvm/Support/CommandLine.h"
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +000014
15using namespace llvm;
16
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +000017static cl::opt<bool> ExpandUnalignedLoads("hvx-expand-unaligned-loads",
18 cl::Hidden, cl::init(true),
19 cl::desc("Expand unaligned HVX loads into a pair of aligned loads"));
20
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +000021static const MVT LegalV64[] = { MVT::v64i8, MVT::v32i16, MVT::v16i32 };
22static const MVT LegalW64[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
23static const MVT LegalV128[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
24static const MVT LegalW128[] = { MVT::v256i8, MVT::v128i16, MVT::v64i32 };
25
26
27void
28HexagonTargetLowering::initializeHVXLowering() {
29 if (Subtarget.useHVX64BOps()) {
30 addRegisterClass(MVT::v64i8, &Hexagon::HvxVRRegClass);
31 addRegisterClass(MVT::v32i16, &Hexagon::HvxVRRegClass);
32 addRegisterClass(MVT::v16i32, &Hexagon::HvxVRRegClass);
33 addRegisterClass(MVT::v128i8, &Hexagon::HvxWRRegClass);
34 addRegisterClass(MVT::v64i16, &Hexagon::HvxWRRegClass);
35 addRegisterClass(MVT::v32i32, &Hexagon::HvxWRRegClass);
36 // These "short" boolean vector types should be legal because
37 // they will appear as results of vector compares. If they were
38 // not legal, type legalization would try to make them legal
39 // and that would require using operations that do not use or
40 // produce such types. That, in turn, would imply using custom
41 // nodes, which would be unoptimizable by the DAG combiner.
42 // The idea is to rely on target-independent operations as much
43 // as possible.
44 addRegisterClass(MVT::v16i1, &Hexagon::HvxQRRegClass);
45 addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
46 addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
47 addRegisterClass(MVT::v512i1, &Hexagon::HvxQRRegClass);
48 } else if (Subtarget.useHVX128BOps()) {
49 addRegisterClass(MVT::v128i8, &Hexagon::HvxVRRegClass);
50 addRegisterClass(MVT::v64i16, &Hexagon::HvxVRRegClass);
51 addRegisterClass(MVT::v32i32, &Hexagon::HvxVRRegClass);
52 addRegisterClass(MVT::v256i8, &Hexagon::HvxWRRegClass);
53 addRegisterClass(MVT::v128i16, &Hexagon::HvxWRRegClass);
54 addRegisterClass(MVT::v64i32, &Hexagon::HvxWRRegClass);
55 addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
56 addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
57 addRegisterClass(MVT::v128i1, &Hexagon::HvxQRRegClass);
58 addRegisterClass(MVT::v1024i1, &Hexagon::HvxQRRegClass);
59 }
60
61 // Set up operation actions.
62
63 bool Use64b = Subtarget.useHVX64BOps();
64 ArrayRef<MVT> LegalV = Use64b ? LegalV64 : LegalV128;
65 ArrayRef<MVT> LegalW = Use64b ? LegalW64 : LegalW128;
66 MVT ByteV = Use64b ? MVT::v64i8 : MVT::v128i8;
67 MVT ByteW = Use64b ? MVT::v128i8 : MVT::v256i8;
68
69 auto setPromoteTo = [this] (unsigned Opc, MVT FromTy, MVT ToTy) {
70 setOperationAction(Opc, FromTy, Promote);
71 AddPromotedToType(Opc, FromTy, ToTy);
72 };
73
74 setOperationAction(ISD::VECTOR_SHUFFLE, ByteV, Legal);
75 setOperationAction(ISD::VECTOR_SHUFFLE, ByteW, Legal);
76 setOperationAction(ISD::AND, ByteV, Legal);
77 setOperationAction(ISD::OR, ByteV, Legal);
78 setOperationAction(ISD::XOR, ByteV, Legal);
79
80 for (MVT T : LegalV) {
81 setIndexedLoadAction(ISD::POST_INC, T, Legal);
82 setIndexedStoreAction(ISD::POST_INC, T, Legal);
83
84 setOperationAction(ISD::ADD, T, Legal);
85 setOperationAction(ISD::SUB, T, Legal);
86 if (T != ByteV) {
87 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, T, Legal);
88 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, T, Legal);
89 }
90
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +000091 setOperationAction(ISD::LOAD, T, Custom);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +000092 setOperationAction(ISD::MUL, T, Custom);
93 setOperationAction(ISD::MULHS, T, Custom);
94 setOperationAction(ISD::MULHU, T, Custom);
95 setOperationAction(ISD::BUILD_VECTOR, T, Custom);
96 // Make concat-vectors custom to handle concats of more than 2 vectors.
97 setOperationAction(ISD::CONCAT_VECTORS, T, Custom);
98 setOperationAction(ISD::INSERT_SUBVECTOR, T, Custom);
99 setOperationAction(ISD::INSERT_VECTOR_ELT, T, Custom);
100 setOperationAction(ISD::EXTRACT_SUBVECTOR, T, Custom);
101 setOperationAction(ISD::EXTRACT_VECTOR_ELT, T, Custom);
102 setOperationAction(ISD::ANY_EXTEND, T, Custom);
103 setOperationAction(ISD::SIGN_EXTEND, T, Custom);
104 setOperationAction(ISD::ZERO_EXTEND, T, Custom);
105 if (T != ByteV) {
106 setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, T, Custom);
107 // HVX only has shifts of words and halfwords.
108 setOperationAction(ISD::SRA, T, Custom);
109 setOperationAction(ISD::SHL, T, Custom);
110 setOperationAction(ISD::SRL, T, Custom);
111 }
112
113 setCondCodeAction(ISD::SETNE, T, Expand);
114 setCondCodeAction(ISD::SETLE, T, Expand);
115 setCondCodeAction(ISD::SETGE, T, Expand);
116 setCondCodeAction(ISD::SETLT, T, Expand);
117 setCondCodeAction(ISD::SETULE, T, Expand);
118 setCondCodeAction(ISD::SETUGE, T, Expand);
119 setCondCodeAction(ISD::SETULT, T, Expand);
120 }
121
122 for (MVT T : LegalV) {
123 MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
124 setOperationAction(ISD::BUILD_VECTOR, BoolV, Custom);
125 setOperationAction(ISD::CONCAT_VECTORS, BoolV, Custom);
126 setOperationAction(ISD::INSERT_SUBVECTOR, BoolV, Custom);
127 setOperationAction(ISD::INSERT_VECTOR_ELT, BoolV, Custom);
128 setOperationAction(ISD::EXTRACT_SUBVECTOR, BoolV, Custom);
129 setOperationAction(ISD::EXTRACT_VECTOR_ELT, BoolV, Custom);
130 }
131
132 for (MVT T : LegalV) {
133 if (T == ByteV)
134 continue;
135 // Promote all shuffles to operate on vectors of bytes.
136 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteV);
137 setPromoteTo(ISD::AND, T, ByteV);
138 setPromoteTo(ISD::OR, T, ByteV);
139 setPromoteTo(ISD::XOR, T, ByteV);
140 }
141
142 for (MVT T : LegalW) {
143 // Custom-lower BUILD_VECTOR for vector pairs. The standard (target-
144 // independent) handling of it would convert it to a load, which is
145 // not always the optimal choice.
146 setOperationAction(ISD::BUILD_VECTOR, T, Custom);
147 // Make concat-vectors custom to handle concats of more than 2 vectors.
148 setOperationAction(ISD::CONCAT_VECTORS, T, Custom);
149
150 // Custom-lower these operations for pairs. Expand them into a concat
151 // of the corresponding operations on individual vectors.
152 setOperationAction(ISD::ANY_EXTEND, T, Custom);
153 setOperationAction(ISD::SIGN_EXTEND, T, Custom);
154 setOperationAction(ISD::ZERO_EXTEND, T, Custom);
155 setOperationAction(ISD::SIGN_EXTEND_INREG, T, Custom);
156 setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, T, Custom);
157 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, T, Legal);
158 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, T, Legal);
159
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +0000160 setOperationAction(ISD::LOAD, T, Custom);
161 setOperationAction(ISD::STORE, T, Custom);
162
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +0000163 setOperationAction(ISD::ADD, T, Legal);
164 setOperationAction(ISD::SUB, T, Legal);
165 setOperationAction(ISD::MUL, T, Custom);
166 setOperationAction(ISD::MULHS, T, Custom);
167 setOperationAction(ISD::MULHU, T, Custom);
168 setOperationAction(ISD::AND, T, Custom);
169 setOperationAction(ISD::OR, T, Custom);
170 setOperationAction(ISD::XOR, T, Custom);
171 setOperationAction(ISD::SETCC, T, Custom);
172 setOperationAction(ISD::VSELECT, T, Custom);
173 if (T != ByteW) {
174 setOperationAction(ISD::SRA, T, Custom);
175 setOperationAction(ISD::SHL, T, Custom);
176 setOperationAction(ISD::SRL, T, Custom);
177
178 // Promote all shuffles to operate on vectors of bytes.
179 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteW);
180 }
181
182 MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
183 setOperationAction(ISD::SETCC, BoolV, Custom);
184 }
185}
186
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000187SDValue
188HexagonTargetLowering::getInt(unsigned IntId, MVT ResTy, ArrayRef<SDValue> Ops,
189 const SDLoc &dl, SelectionDAG &DAG) const {
190 SmallVector<SDValue,4> IntOps;
191 IntOps.push_back(DAG.getConstant(IntId, dl, MVT::i32));
192 for (const SDValue &Op : Ops)
193 IntOps.push_back(Op);
194 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, ResTy, IntOps);
195}
196
197MVT
198HexagonTargetLowering::typeJoin(const TypePair &Tys) const {
199 assert(Tys.first.getVectorElementType() == Tys.second.getVectorElementType());
200
201 MVT ElemTy = Tys.first.getVectorElementType();
202 return MVT::getVectorVT(ElemTy, Tys.first.getVectorNumElements() +
203 Tys.second.getVectorNumElements());
204}
205
206HexagonTargetLowering::TypePair
207HexagonTargetLowering::typeSplit(MVT VecTy) const {
208 assert(VecTy.isVector());
209 unsigned NumElem = VecTy.getVectorNumElements();
210 assert((NumElem % 2) == 0 && "Expecting even-sized vector type");
211 MVT HalfTy = MVT::getVectorVT(VecTy.getVectorElementType(), NumElem/2);
212 return { HalfTy, HalfTy };
213}
214
215MVT
216HexagonTargetLowering::typeExtElem(MVT VecTy, unsigned Factor) const {
217 MVT ElemTy = VecTy.getVectorElementType();
218 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() * Factor);
219 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
220}
221
222MVT
223HexagonTargetLowering::typeTruncElem(MVT VecTy, unsigned Factor) const {
224 MVT ElemTy = VecTy.getVectorElementType();
225 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() / Factor);
226 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
227}
228
229SDValue
230HexagonTargetLowering::opCastElem(SDValue Vec, MVT ElemTy,
231 SelectionDAG &DAG) const {
232 if (ty(Vec).getVectorElementType() == ElemTy)
233 return Vec;
234 MVT CastTy = tyVector(Vec.getValueType().getSimpleVT(), ElemTy);
235 return DAG.getBitcast(CastTy, Vec);
236}
237
238SDValue
239HexagonTargetLowering::opJoin(const VectorPair &Ops, const SDLoc &dl,
240 SelectionDAG &DAG) const {
241 return DAG.getNode(ISD::CONCAT_VECTORS, dl, typeJoin(ty(Ops)),
242 Ops.second, Ops.first);
243}
244
245HexagonTargetLowering::VectorPair
246HexagonTargetLowering::opSplit(SDValue Vec, const SDLoc &dl,
247 SelectionDAG &DAG) const {
248 TypePair Tys = typeSplit(ty(Vec));
Krzysztof Parzyszek1d52a852018-02-06 15:15:13 +0000249 if (Vec.getOpcode() == HexagonISD::QCAT)
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +0000250 return VectorPair(Vec.getOperand(0), Vec.getOperand(1));
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000251 return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
252}
253
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000254bool
255HexagonTargetLowering::isHvxSingleTy(MVT Ty) const {
256 return Subtarget.isHVXVectorType(Ty) &&
257 Ty.getSizeInBits() == 8 * Subtarget.getVectorLength();
258}
259
260bool
261HexagonTargetLowering::isHvxPairTy(MVT Ty) const {
262 return Subtarget.isHVXVectorType(Ty) &&
263 Ty.getSizeInBits() == 16 * Subtarget.getVectorLength();
264}
265
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000266SDValue
267HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
268 SelectionDAG &DAG) const {
269 if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
270 ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
271
272 unsigned ElemWidth = ElemTy.getSizeInBits();
273 if (ElemWidth == 8)
274 return ElemIdx;
275
276 unsigned L = Log2_32(ElemWidth/8);
277 const SDLoc &dl(ElemIdx);
278 return DAG.getNode(ISD::SHL, dl, MVT::i32,
279 {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
280}
281
282SDValue
283HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
284 SelectionDAG &DAG) const {
285 unsigned ElemWidth = ElemTy.getSizeInBits();
286 assert(ElemWidth >= 8 && ElemWidth <= 32);
287 if (ElemWidth == 32)
288 return Idx;
289
290 if (ty(Idx) != MVT::i32)
291 Idx = DAG.getBitcast(MVT::i32, Idx);
292 const SDLoc &dl(Idx);
293 SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
294 SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
295 return SubIdx;
296}
297
298SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000299HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
300 SDValue Op1, ArrayRef<int> Mask,
301 SelectionDAG &DAG) const {
302 MVT OpTy = ty(Op0);
303 assert(OpTy == ty(Op1));
304
305 MVT ElemTy = OpTy.getVectorElementType();
306 if (ElemTy == MVT::i8)
307 return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
308 assert(ElemTy.getSizeInBits() >= 8);
309
310 MVT ResTy = tyVector(OpTy, MVT::i8);
311 unsigned ElemSize = ElemTy.getSizeInBits() / 8;
312
313 SmallVector<int,128> ByteMask;
314 for (int M : Mask) {
315 if (M < 0) {
316 for (unsigned I = 0; I != ElemSize; ++I)
317 ByteMask.push_back(-1);
318 } else {
319 int NewM = M*ElemSize;
320 for (unsigned I = 0; I != ElemSize; ++I)
321 ByteMask.push_back(NewM+I);
322 }
323 }
324 assert(ResTy.getVectorNumElements() == ByteMask.size());
325 return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
326 opCastElem(Op1, MVT::i8, DAG), ByteMask);
327}
328
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000329SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000330HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
331 const SDLoc &dl, MVT VecTy,
332 SelectionDAG &DAG) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000333 unsigned VecLen = Values.size();
334 MachineFunction &MF = DAG.getMachineFunction();
335 MVT ElemTy = VecTy.getVectorElementType();
336 unsigned ElemWidth = ElemTy.getSizeInBits();
337 unsigned HwLen = Subtarget.getVectorLength();
338
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000339 unsigned ElemSize = ElemWidth / 8;
340 assert(ElemSize*VecLen == HwLen);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000341 SmallVector<SDValue,32> Words;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000342
343 if (VecTy.getVectorElementType() != MVT::i32) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000344 assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
345 unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
346 MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000347 for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
348 SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000349 Words.push_back(DAG.getBitcast(MVT::i32, W));
350 }
351 } else {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000352 Words.assign(Values.begin(), Values.end());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000353 }
354
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000355 unsigned NumWords = Words.size();
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000356 bool IsSplat = true, IsUndef = true;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000357 SDValue SplatV;
358 for (unsigned i = 0; i != NumWords && IsSplat; ++i) {
359 if (isUndef(Words[i]))
360 continue;
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000361 IsUndef = false;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000362 if (!SplatV.getNode())
363 SplatV = Words[i];
364 else if (SplatV != Words[i])
365 IsSplat = false;
366 }
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000367 if (IsUndef)
368 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000369 if (IsSplat) {
370 assert(SplatV.getNode());
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000371 auto *IdxN = dyn_cast<ConstantSDNode>(SplatV.getNode());
372 if (IdxN && IdxN->isNullValue())
373 return getZero(dl, VecTy, DAG);
374 MVT WordTy = MVT::getVectorVT(MVT::i32, HwLen/4);
375 SDValue SV = DAG.getNode(HexagonISD::VSPLAT, dl, WordTy, SplatV);
376 return DAG.getBitcast(VecTy, SV);
377 }
378
379 // Delay recognizing constant vectors until here, so that we can generate
380 // a vsplat.
381 SmallVector<ConstantInt*, 128> Consts(VecLen);
382 bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
383 if (AllConst) {
384 ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
385 (Constant**)Consts.end());
386 Constant *CV = ConstantVector::get(Tmp);
387 unsigned Align = HwLen;
388 SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
389 return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
390 MachinePointerInfo::getConstantPool(MF), Align);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000391 }
392
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000393 // Construct two halves in parallel, then or them together.
394 assert(4*Words.size() == Subtarget.getVectorLength());
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000395 SDValue HalfV0 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
396 SDValue HalfV1 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000397 SDValue S = DAG.getConstant(4, dl, MVT::i32);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000398 for (unsigned i = 0; i != NumWords/2; ++i) {
399 SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
400 {HalfV0, Words[i]});
401 SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
402 {HalfV1, Words[i+NumWords/2]});
403 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
404 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
405 }
406
407 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
408 {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
409 SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
410 return DstV;
411}
412
413SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000414HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
415 unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
416 MVT PredTy = ty(PredV);
417 unsigned HwLen = Subtarget.getVectorLength();
418 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
419
420 if (Subtarget.isHVXVectorType(PredTy, true)) {
421 // Move the vector predicate SubV to a vector register, and scale it
422 // down to match the representation (bytes per type element) that VecV
423 // uses. The scaling down will pick every 2nd or 4th (every Scale-th
Hiroshi Inoue0909ca12018-01-26 08:15:29 +0000424 // in general) element and put them at the front of the resulting
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000425 // vector. This subvector will then be inserted into the Q2V of VecV.
426 // To avoid having an operation that generates an illegal type (short
427 // vector), generate a full size vector.
428 //
429 SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
430 SmallVector<int,128> Mask(HwLen);
431 // Scale = BitBytes(PredV) / Given BitBytes.
432 unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
433 unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
434
435 for (unsigned i = 0; i != HwLen; ++i) {
436 unsigned Num = i % Scale;
437 unsigned Off = i / Scale;
438 Mask[BlockLen*Num + Off] = i;
439 }
440 SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
441 if (!ZeroFill)
442 return S;
443 // Fill the bytes beyond BlockLen with 0s.
444 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000445 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
446 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000447 SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
448 return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
449 }
450
451 // Make sure that this is a valid scalar predicate.
452 assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
453
454 unsigned Bytes = 8 / PredTy.getVectorNumElements();
455 SmallVector<SDValue,4> Words[2];
456 unsigned IdxW = 0;
457
458 auto Lo32 = [&DAG, &dl] (SDValue P) {
459 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, P);
460 };
461 auto Hi32 = [&DAG, &dl] (SDValue P) {
462 return DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, P);
463 };
464
465 SDValue W0 = isUndef(PredV)
466 ? DAG.getUNDEF(MVT::i64)
467 : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
468 Words[IdxW].push_back(Hi32(W0));
469 Words[IdxW].push_back(Lo32(W0));
470
471 while (Bytes < BitBytes) {
472 IdxW ^= 1;
473 Words[IdxW].clear();
474
475 if (Bytes < 4) {
476 for (const SDValue &W : Words[IdxW ^ 1]) {
477 SDValue T = expandPredicate(W, dl, DAG);
478 Words[IdxW].push_back(Hi32(T));
479 Words[IdxW].push_back(Lo32(T));
480 }
481 } else {
482 for (const SDValue &W : Words[IdxW ^ 1]) {
483 Words[IdxW].push_back(W);
484 Words[IdxW].push_back(W);
485 }
486 }
487 Bytes *= 2;
488 }
489
490 assert(Bytes == BitBytes);
491
492 SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
493 SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
494 for (const SDValue &W : Words[IdxW]) {
495 Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
496 Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
497 }
498
499 return Vec;
500}
501
502SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000503HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
504 const SDLoc &dl, MVT VecTy,
505 SelectionDAG &DAG) const {
506 // Construct a vector V of bytes, such that a comparison V >u 0 would
507 // produce the required vector predicate.
508 unsigned VecLen = Values.size();
509 unsigned HwLen = Subtarget.getVectorLength();
510 assert(VecLen <= HwLen || VecLen == 8*HwLen);
511 SmallVector<SDValue,128> Bytes;
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000512 bool AllT = true, AllF = true;
513
514 auto IsTrue = [] (SDValue V) {
515 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
516 return !N->isNullValue();
517 return false;
518 };
519 auto IsFalse = [] (SDValue V) {
520 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
521 return N->isNullValue();
522 return false;
523 };
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000524
525 if (VecLen <= HwLen) {
526 // In the hardware, each bit of a vector predicate corresponds to a byte
527 // of a vector register. Calculate how many bytes does a bit of VecTy
528 // correspond to.
529 assert(HwLen % VecLen == 0);
530 unsigned BitBytes = HwLen / VecLen;
531 for (SDValue V : Values) {
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000532 AllT &= IsTrue(V);
533 AllF &= IsFalse(V);
534
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000535 SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000536 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000537 for (unsigned B = 0; B != BitBytes; ++B)
538 Bytes.push_back(Ext);
539 }
540 } else {
541 // There are as many i1 values, as there are bits in a vector register.
542 // Divide the values into groups of 8 and check that each group consists
543 // of the same value (ignoring undefs).
544 for (unsigned I = 0; I != VecLen; I += 8) {
545 unsigned B = 0;
546 // Find the first non-undef value in this group.
547 for (; B != 8; ++B) {
548 if (!Values[I+B].isUndef())
549 break;
550 }
551 SDValue F = Values[I+B];
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000552 AllT &= IsTrue(F);
553 AllF &= IsFalse(F);
554
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000555 SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000556 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000557 Bytes.push_back(Ext);
558 // Verify that the rest of values in the group are the same as the
559 // first.
560 for (; B != 8; ++B)
561 assert(Values[I+B].isUndef() || Values[I+B] == F);
562 }
563 }
564
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000565 if (AllT)
566 return DAG.getNode(HexagonISD::QTRUE, dl, VecTy);
567 if (AllF)
568 return DAG.getNode(HexagonISD::QFALSE, dl, VecTy);
569
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000570 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000571 SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
572 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
573}
574
575SDValue
576HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
577 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
578 MVT ElemTy = ty(VecV).getVectorElementType();
579
580 unsigned ElemWidth = ElemTy.getSizeInBits();
581 assert(ElemWidth >= 8 && ElemWidth <= 32);
582 (void)ElemWidth;
583
584 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
585 SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
586 {VecV, ByteIdx});
587 if (ElemTy == MVT::i32)
588 return ExWord;
589
590 // Have an extracted word, need to extract the smaller element out of it.
591 // 1. Extract the bits of (the original) IdxV that correspond to the index
592 // of the desired element in the 32-bit word.
593 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
594 // 2. Extract the element from the word.
595 SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
596 return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
597}
598
599SDValue
600HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
601 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
602 // Implement other return types if necessary.
603 assert(ResTy == MVT::i1);
604
605 unsigned HwLen = Subtarget.getVectorLength();
606 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
607 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
608
609 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
610 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
611 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
612
613 SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
614 SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000615 return getInstr(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000616}
617
618SDValue
619HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
620 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
621 MVT ElemTy = ty(VecV).getVectorElementType();
622
623 unsigned ElemWidth = ElemTy.getSizeInBits();
624 assert(ElemWidth >= 8 && ElemWidth <= 32);
625 (void)ElemWidth;
626
627 auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
628 SDValue ByteIdxV) {
629 MVT VecTy = ty(VecV);
630 unsigned HwLen = Subtarget.getVectorLength();
631 SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
632 {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
633 SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
634 SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
635 SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
636 {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
637 SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
638 return TorV;
639 };
640
641 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
642 if (ElemTy == MVT::i32)
643 return InsertWord(VecV, ValV, ByteIdx);
644
645 // If this is not inserting a 32-bit word, convert it into such a thing.
646 // 1. Extract the existing word from the target vector.
647 SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
648 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
649 SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
650 dl, MVT::i32, DAG);
651
652 // 2. Treating the extracted word as a 32-bit vector, insert the given
653 // value into it.
654 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
655 MVT SubVecTy = tyVector(ty(Ext), ElemTy);
656 SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
657 ValV, SubIdx, dl, ElemTy, DAG);
658
659 // 3. Insert the 32-bit word back into the original vector.
660 return InsertWord(VecV, Ins, ByteIdx);
661}
662
663SDValue
664HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
665 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
666 unsigned HwLen = Subtarget.getVectorLength();
667 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
668 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
669
670 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
671 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
672 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
673 ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
674
675 SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
676 return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
677}
678
679SDValue
680HexagonTargetLowering::extractHvxSubvectorReg(SDValue VecV, SDValue IdxV,
681 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
682 MVT VecTy = ty(VecV);
683 unsigned HwLen = Subtarget.getVectorLength();
684 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
685 MVT ElemTy = VecTy.getVectorElementType();
686 unsigned ElemWidth = ElemTy.getSizeInBits();
687
688 // If the source vector is a vector pair, get the single vector containing
689 // the subvector of interest. The subvector will never overlap two single
690 // vectors.
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000691 if (isHvxPairTy(VecTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000692 unsigned SubIdx;
693 if (Idx * ElemWidth >= 8*HwLen) {
694 SubIdx = Hexagon::vsub_hi;
695 Idx -= VecTy.getVectorNumElements() / 2;
696 } else {
697 SubIdx = Hexagon::vsub_lo;
698 }
699 VecTy = typeSplit(VecTy).first;
700 VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
701 if (VecTy == ResTy)
702 return VecV;
703 }
704
705 // The only meaningful subvectors of a single HVX vector are those that
706 // fit in a scalar register.
707 assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
708
709 MVT WordTy = tyVector(VecTy, MVT::i32);
710 SDValue WordVec = DAG.getBitcast(WordTy, VecV);
711 unsigned WordIdx = (Idx*ElemWidth) / 32;
712
713 SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
714 SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
715 if (ResTy.getSizeInBits() == 32)
716 return DAG.getBitcast(ResTy, W0);
717
718 SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
719 SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
720 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64, {W1, W0});
721 return DAG.getBitcast(ResTy, WW);
722}
723
724SDValue
725HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
726 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
727 MVT VecTy = ty(VecV);
728 unsigned HwLen = Subtarget.getVectorLength();
729 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
730 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
731 // IdxV is required to be a constant.
732 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
733
734 unsigned ResLen = ResTy.getVectorNumElements();
735 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
736 unsigned Offset = Idx * BitBytes;
737 SDValue Undef = DAG.getUNDEF(ByteTy);
738 SmallVector<int,128> Mask;
739
740 if (Subtarget.isHVXVectorType(ResTy, true)) {
741 // Converting between two vector predicates. Since the result is shorter
742 // than the source, it will correspond to a vector predicate with the
743 // relevant bits replicated. The replication count is the ratio of the
744 // source and target vector lengths.
745 unsigned Rep = VecTy.getVectorNumElements() / ResLen;
746 assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
747 for (unsigned i = 0; i != HwLen/Rep; ++i) {
748 for (unsigned j = 0; j != Rep; ++j)
749 Mask.push_back(i + Offset);
750 }
751 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
752 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
753 }
754
755 // Converting between a vector predicate and a scalar predicate. In the
756 // vector predicate, a group of BitBytes bits will correspond to a single
757 // i1 element of the source vector type. Those bits will all have the same
758 // value. The same will be true for ByteVec, where each byte corresponds
759 // to a bit in the vector predicate.
760 // The algorithm is to traverse the ByteVec, going over the i1 values from
761 // the source vector, and generate the corresponding representation in an
762 // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
763 // elements so that the interesting 8 bytes will be in the low end of the
764 // vector.
765 unsigned Rep = 8 / ResLen;
766 // Make sure the output fill the entire vector register, so repeat the
767 // 8-byte groups as many times as necessary.
768 for (unsigned r = 0; r != HwLen/ResLen; ++r) {
769 // This will generate the indexes of the 8 interesting bytes.
770 for (unsigned i = 0; i != ResLen; ++i) {
771 for (unsigned j = 0; j != Rep; ++j)
772 Mask.push_back(Offset + i*BitBytes);
773 }
774 }
775
776 SDValue Zero = getZero(dl, MVT::i32, DAG);
777 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
778 // Combine the two low words from ShuffV into a v8i8, and byte-compare
779 // them against 0.
780 SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
781 SDValue W1 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
782 {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
783 SDValue Vec64 = DAG.getNode(HexagonISD::COMBINE, dl, MVT::v8i8, {W1, W0});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000784 return getInstr(Hexagon::A4_vcmpbgtui, dl, ResTy,
785 {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000786}
787
788SDValue
789HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
790 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
791 MVT VecTy = ty(VecV);
792 MVT SubTy = ty(SubV);
793 unsigned HwLen = Subtarget.getVectorLength();
794 MVT ElemTy = VecTy.getVectorElementType();
795 unsigned ElemWidth = ElemTy.getSizeInBits();
796
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000797 bool IsPair = isHvxPairTy(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000798 MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
799 // The two single vectors that VecV consists of, if it's a pair.
800 SDValue V0, V1;
801 SDValue SingleV = VecV;
802 SDValue PickHi;
803
804 if (IsPair) {
805 V0 = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, VecV);
806 V1 = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, VecV);
807
808 SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
809 dl, MVT::i32);
810 PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000811 if (isHvxSingleTy(SubTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000812 if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
813 unsigned Idx = CN->getZExtValue();
814 assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
815 unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
816 return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
817 }
818 // If IdxV is not a constant, generate the two variants: with the
819 // SubV as the high and as the low subregister, and select the right
820 // pair based on the IdxV.
821 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
822 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
823 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
824 }
825 // The subvector being inserted must be entirely contained in one of
826 // the vectors V0 or V1. Set SingleV to the correct one, and update
827 // IdxV to be the index relative to the beginning of that vector.
828 SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
829 IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
830 SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
831 }
832
833 // The only meaningful subvectors of a single HVX vector are those that
834 // fit in a scalar register.
835 assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
836 // Convert IdxV to be index in bytes.
837 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
838 if (!IdxN || !IdxN->isNullValue()) {
839 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
840 DAG.getConstant(ElemWidth/8, dl, MVT::i32));
841 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
842 }
843 // When inserting a single word, the rotation back to the original position
844 // would be by HwLen-Idx, but if two words are inserted, it will need to be
845 // by (HwLen-4)-Idx.
846 unsigned RolBase = HwLen;
847 if (VecTy.getSizeInBits() == 32) {
848 SDValue V = DAG.getBitcast(MVT::i32, SubV);
849 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, V);
850 } else {
851 SDValue V = DAG.getBitcast(MVT::i64, SubV);
852 SDValue R0 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, V);
853 SDValue R1 = DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, V);
854 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
855 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
856 DAG.getConstant(4, dl, MVT::i32));
857 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
858 RolBase = HwLen-4;
859 }
860 // If the vector wasn't ror'ed, don't ror it back.
861 if (RolBase != 4 || !IdxN || !IdxN->isNullValue()) {
862 SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
863 DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
864 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
865 }
866
867 if (IsPair) {
868 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
869 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
870 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
871 }
872 return SingleV;
873}
874
875SDValue
876HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
877 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
878 MVT VecTy = ty(VecV);
879 MVT SubTy = ty(SubV);
880 assert(Subtarget.isHVXVectorType(VecTy, true));
881 // VecV is an HVX vector predicate. SubV may be either an HVX vector
882 // predicate as well, or it can be a scalar predicate.
883
884 unsigned VecLen = VecTy.getVectorNumElements();
885 unsigned HwLen = Subtarget.getVectorLength();
886 assert(HwLen % VecLen == 0 && "Unexpected vector type");
887
888 unsigned Scale = VecLen / SubTy.getVectorNumElements();
889 unsigned BitBytes = HwLen / VecLen;
890 unsigned BlockLen = HwLen / Scale;
891
892 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
893 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
894 SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
895 SDValue ByteIdx;
896
897 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
898 if (!IdxN || !IdxN->isNullValue()) {
899 ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
900 DAG.getConstant(BitBytes, dl, MVT::i32));
901 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
902 }
903
904 // ByteVec is the target vector VecV rotated in such a way that the
905 // subvector should be inserted at index 0. Generate a predicate mask
906 // and use vmux to do the insertion.
907 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000908 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
909 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
910 ByteVec = getInstr(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000911 // Rotate ByteVec back, and convert to a vector predicate.
912 if (!IdxN || !IdxN->isNullValue()) {
913 SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
914 SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
915 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
916 }
917 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
918}
919
920SDValue
921HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
922 MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
923 // Sign- and any-extending of a vector predicate to a vector register is
924 // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
925 // a vector of 1s (where the 1s are of type matching the vector type).
926 assert(Subtarget.isHVXVectorType(ResTy));
927 if (!ZeroExt)
928 return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
929
930 assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
931 SDValue True = DAG.getNode(HexagonISD::VSPLAT, dl, ResTy,
932 DAG.getConstant(1, dl, MVT::i32));
933 SDValue False = getZero(dl, ResTy, DAG);
934 return DAG.getSelect(dl, ResTy, VecV, True, False);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000935}
936
937SDValue
938HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
939 const {
940 const SDLoc &dl(Op);
941 MVT VecTy = ty(Op);
942
943 unsigned Size = Op.getNumOperands();
944 SmallVector<SDValue,128> Ops;
945 for (unsigned i = 0; i != Size; ++i)
946 Ops.push_back(Op.getOperand(i));
947
948 if (VecTy.getVectorElementType() == MVT::i1)
949 return buildHvxVectorPred(Ops, dl, VecTy, DAG);
950
951 if (VecTy.getSizeInBits() == 16*Subtarget.getVectorLength()) {
952 ArrayRef<SDValue> A(Ops);
953 MVT SingleTy = typeSplit(VecTy).first;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000954 SDValue V0 = buildHvxVectorReg(A.take_front(Size/2), dl, SingleTy, DAG);
955 SDValue V1 = buildHvxVectorReg(A.drop_front(Size/2), dl, SingleTy, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000956 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
957 }
958
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000959 return buildHvxVectorReg(Ops, dl, VecTy, DAG);
960}
961
962SDValue
963HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
964 const {
Krzysztof Parzyszek97a50952018-02-06 20:18:58 +0000965 // Vector concatenation of two integer (non-bool) vectors does not need
966 // special lowering. Custom-lower concats of bool vectors and expand
967 // concats of more than 2 vectors.
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000968 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000969 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000970 unsigned NumOp = Op.getNumOperands();
Krzysztof Parzyszek97a50952018-02-06 20:18:58 +0000971 if (VecTy.getVectorElementType() != MVT::i1) {
972 if (NumOp == 2)
973 return Op;
974 // Expand the other cases into a build-vector.
975 SmallVector<SDValue,8> Elems;
976 for (SDValue V : Op.getNode()->ops())
977 DAG.ExtractVectorElements(V, Elems);
978 return DAG.getBuildVector(VecTy, dl, Elems);
979 }
980
981 assert(VecTy.getVectorElementType() == MVT::i1);
982 unsigned HwLen = Subtarget.getVectorLength();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000983 assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +0000984
985 SDValue Op0 = Op.getOperand(0);
986
987 // If the operands are HVX types (i.e. not scalar predicates), then
988 // defer the concatenation, and create QCAT instead.
989 if (Subtarget.isHVXVectorType(ty(Op0), true)) {
990 if (NumOp == 2)
991 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, Op0, Op.getOperand(1));
992
993 ArrayRef<SDUse> U(Op.getNode()->ops());
994 SmallVector<SDValue,4> SV(U.begin(), U.end());
995 ArrayRef<SDValue> Ops(SV);
996
997 MVT HalfTy = typeSplit(VecTy).first;
998 SDValue V0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
999 Ops.take_front(NumOp/2));
1000 SDValue V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1001 Ops.take_back(NumOp/2));
1002 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, V0, V1);
1003 }
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001004
1005 // Count how many bytes (in a vector register) each bit in VecTy
1006 // corresponds to.
1007 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
1008
1009 SmallVector<SDValue,8> Prefixes;
1010 for (SDValue V : Op.getNode()->op_values()) {
1011 SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
1012 Prefixes.push_back(P);
1013 }
1014
1015 unsigned InpLen = ty(Op.getOperand(0)).getVectorNumElements();
1016 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1017 SDValue S = DAG.getConstant(InpLen*BitBytes, dl, MVT::i32);
1018 SDValue Res = getZero(dl, ByteTy, DAG);
1019 for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
1020 Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
1021 Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
1022 }
1023 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001024}
1025
1026SDValue
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001027HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
1028 const {
1029 // Change the type of the extracted element to i32.
1030 SDValue VecV = Op.getOperand(0);
1031 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001032 const SDLoc &dl(Op);
1033 SDValue IdxV = Op.getOperand(1);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001034 if (ElemTy == MVT::i1)
1035 return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001036
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001037 return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001038}
1039
1040SDValue
1041HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
1042 const {
1043 const SDLoc &dl(Op);
1044 SDValue VecV = Op.getOperand(0);
1045 SDValue ValV = Op.getOperand(1);
1046 SDValue IdxV = Op.getOperand(2);
1047 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001048 if (ElemTy == MVT::i1)
1049 return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001050
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001051 return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001052}
1053
1054SDValue
1055HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
1056 const {
1057 SDValue SrcV = Op.getOperand(0);
1058 MVT SrcTy = ty(SrcV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001059 MVT DstTy = ty(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001060 SDValue IdxV = Op.getOperand(1);
1061 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001062 assert(Idx % DstTy.getVectorNumElements() == 0);
1063 (void)Idx;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001064 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001065
1066 MVT ElemTy = SrcTy.getVectorElementType();
1067 if (ElemTy == MVT::i1)
1068 return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
1069
1070 return extractHvxSubvectorReg(SrcV, IdxV, dl, DstTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001071}
1072
1073SDValue
1074HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
1075 const {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001076 // Idx does not need to be a constant.
1077 SDValue VecV = Op.getOperand(0);
1078 SDValue ValV = Op.getOperand(1);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001079 SDValue IdxV = Op.getOperand(2);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001080
1081 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001082 MVT VecTy = ty(VecV);
1083 MVT ElemTy = VecTy.getVectorElementType();
1084 if (ElemTy == MVT::i1)
1085 return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
1086
1087 return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001088}
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001089
1090SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001091HexagonTargetLowering::LowerHvxAnyExt(SDValue Op, SelectionDAG &DAG) const {
1092 // Lower any-extends of boolean vectors to sign-extends, since they
1093 // translate directly to Q2V. Zero-extending could also be done equally
1094 // fast, but Q2V is used/recognized in more places.
1095 // For all other vectors, use zero-extend.
1096 MVT ResTy = ty(Op);
1097 SDValue InpV = Op.getOperand(0);
1098 MVT ElemTy = ty(InpV).getVectorElementType();
1099 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1100 return LowerHvxSignExt(Op, DAG);
1101 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(Op), ResTy, InpV);
1102}
1103
1104SDValue
1105HexagonTargetLowering::LowerHvxSignExt(SDValue Op, SelectionDAG &DAG) const {
1106 MVT ResTy = ty(Op);
1107 SDValue InpV = Op.getOperand(0);
1108 MVT ElemTy = ty(InpV).getVectorElementType();
1109 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1110 return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), false, DAG);
1111 return Op;
1112}
1113
1114SDValue
1115HexagonTargetLowering::LowerHvxZeroExt(SDValue Op, SelectionDAG &DAG) const {
1116 MVT ResTy = ty(Op);
1117 SDValue InpV = Op.getOperand(0);
1118 MVT ElemTy = ty(InpV).getVectorElementType();
1119 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1120 return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), true, DAG);
1121 return Op;
1122}
1123
1124SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001125HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
1126 MVT ResTy = ty(Op);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001127 assert(ResTy.isVector() && isHvxSingleTy(ResTy));
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001128 const SDLoc &dl(Op);
1129 SmallVector<int,256> ShuffMask;
1130
1131 MVT ElemTy = ResTy.getVectorElementType();
1132 unsigned VecLen = ResTy.getVectorNumElements();
1133 SDValue Vs = Op.getOperand(0);
1134 SDValue Vt = Op.getOperand(1);
1135
1136 switch (ElemTy.SimpleTy) {
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +00001137 case MVT::i8: {
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001138 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1139 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1140 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001141 MVT ExtTy = typeExtElem(ResTy, 2);
1142 unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
1143 : Hexagon::V6_vmpyhv;
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001144 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001145
1146 // Discard high halves of the resulting values, collect the low halves.
1147 for (unsigned I = 0; I < VecLen; I += 2) {
1148 ShuffMask.push_back(I); // Pick even element.
1149 ShuffMask.push_back(I+VecLen); // Pick odd element.
1150 }
1151 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
Krzysztof Parzyszek0f5d9762018-01-05 20:45:34 +00001152 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1153 return DAG.getBitcast(ResTy, BS);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001154 }
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +00001155 case MVT::i16:
1156 // For i16 there is V6_vmpyih, which acts exactly like the MUL opcode.
1157 // (There is also V6_vmpyhv, which behaves in an analogous way to
1158 // V6_vmpybv.)
1159 return getInstr(Hexagon::V6_vmpyih, dl, ResTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001160 case MVT::i32: {
1161 // Use the following sequence for signed word multiply:
1162 // T0 = V6_vmpyiowh Vs, Vt
1163 // T1 = V6_vaslw T0, 16
1164 // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
1165 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001166 SDValue T0 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
1167 SDValue T1 = getInstr(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
1168 SDValue T2 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1169 {T1, Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001170 return T2;
1171 }
1172 default:
1173 break;
1174 }
1175 return SDValue();
1176}
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001177
1178SDValue
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001179HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
1180 MVT ResTy = ty(Op);
1181 assert(ResTy.isVector());
1182 const SDLoc &dl(Op);
1183 SmallVector<int,256> ShuffMask;
1184
1185 MVT ElemTy = ResTy.getVectorElementType();
1186 unsigned VecLen = ResTy.getVectorNumElements();
1187 SDValue Vs = Op.getOperand(0);
1188 SDValue Vt = Op.getOperand(1);
1189 bool IsSigned = Op.getOpcode() == ISD::MULHS;
1190
1191 if (ElemTy == MVT::i8 || ElemTy == MVT::i16) {
1192 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1193 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1194 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
1195 // For i16, use V6_vmpyhv, which behaves in an analogous way to
1196 // V6_vmpybv: results Lo and Hi are products of even/odd elements
1197 // respectively.
1198 MVT ExtTy = typeExtElem(ResTy, 2);
1199 unsigned MpyOpc = ElemTy == MVT::i8
1200 ? (IsSigned ? Hexagon::V6_vmpybv : Hexagon::V6_vmpyubv)
1201 : (IsSigned ? Hexagon::V6_vmpyhv : Hexagon::V6_vmpyuhv);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001202 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001203
1204 // Discard low halves of the resulting values, collect the high halves.
1205 for (unsigned I = 0; I < VecLen; I += 2) {
1206 ShuffMask.push_back(I+1); // Pick even element.
1207 ShuffMask.push_back(I+VecLen+1); // Pick odd element.
1208 }
1209 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
1210 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1211 return DAG.getBitcast(ResTy, BS);
1212 }
1213
1214 assert(ElemTy == MVT::i32);
1215 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
1216
1217 if (IsSigned) {
1218 // mulhs(Vs,Vt) =
1219 // = [(Hi(Vs)*2^16 + Lo(Vs)) *s (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1220 // = [Hi(Vs)*2^16 *s Hi(Vt)*2^16 + Hi(Vs) *su Lo(Vt)*2^16
1221 // + Lo(Vs) *us (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1222 // = [Hi(Vs) *s Hi(Vt)*2^32 + Hi(Vs) *su Lo(Vt)*2^16
1223 // + Lo(Vs) *us Vt] >> 32
1224 // The low half of Lo(Vs)*Lo(Vt) will be discarded (it's not added to
1225 // anything, so it cannot produce any carry over to higher bits),
1226 // so everything in [] can be shifted by 16 without loss of precision.
1227 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + Lo(Vs)*Vt >> 16] >> 16
1228 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + V6_vmpyewuh(Vs,Vt)] >> 16
1229 // Denote Hi(Vs) = Vs':
1230 // = [Vs'*s Hi(Vt)*2^16 + Vs' *su Lo(Vt) + V6_vmpyewuh(Vt,Vs)] >> 16
1231 // = Vs'*s Hi(Vt) + (V6_vmpyiewuh(Vs',Vt) + V6_vmpyewuh(Vt,Vs)) >> 16
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001232 SDValue T0 = getInstr(Hexagon::V6_vmpyewuh, dl, ResTy, {Vt, Vs}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001233 // Get Vs':
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001234 SDValue S0 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {Vs, S16}, DAG);
1235 SDValue T1 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1236 {T0, S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001237 // Shift by 16:
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001238 SDValue S2 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {T1, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001239 // Get Vs'*Hi(Vt):
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001240 SDValue T2 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001241 // Add:
1242 SDValue T3 = DAG.getNode(ISD::ADD, dl, ResTy, {S2, T2});
1243 return T3;
1244 }
1245
1246 // Unsigned mulhw. (Would expansion using signed mulhw be better?)
1247
1248 auto LoVec = [&DAG,ResTy,dl] (SDValue Pair) {
1249 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResTy, Pair);
1250 };
1251 auto HiVec = [&DAG,ResTy,dl] (SDValue Pair) {
1252 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResTy, Pair);
1253 };
1254
1255 MVT PairTy = typeJoin({ResTy, ResTy});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001256 SDValue P = getInstr(Hexagon::V6_lvsplatw, dl, ResTy,
1257 {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001258 // Multiply-unsigned halfwords:
1259 // LoVec = Vs.uh[2i] * Vt.uh[2i],
1260 // HiVec = Vs.uh[2i+1] * Vt.uh[2i+1]
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001261 SDValue T0 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001262 // The low halves in the LoVec of the pair can be discarded. They are
1263 // not added to anything (in the full-precision product), so they cannot
1264 // produce a carry into the higher bits.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001265 SDValue T1 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {LoVec(T0), S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001266 // Swap low and high halves in Vt, and do the halfword multiplication
1267 // 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 +00001268 SDValue D0 = getInstr(Hexagon::V6_vdelta, dl, ResTy, {Vt, P}, DAG);
1269 SDValue T2 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, D0}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001270 // T2 has mixed products of halfwords: Lo(Vt)*Hi(Vs) and Hi(Vt)*Lo(Vs).
1271 // These products are words, but cannot be added directly because the
1272 // sums could overflow. Add these products, by halfwords, where each sum
1273 // of a pair of halfwords gives a word.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001274 SDValue T3 = getInstr(Hexagon::V6_vadduhw, dl, PairTy,
1275 {LoVec(T2), HiVec(T2)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001276 // Add the high halfwords from the products of the low halfwords.
1277 SDValue T4 = DAG.getNode(ISD::ADD, dl, ResTy, {T1, LoVec(T3)});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001278 SDValue T5 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {T4, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001279 SDValue T6 = DAG.getNode(ISD::ADD, dl, ResTy, {HiVec(T0), HiVec(T3)});
1280 SDValue T7 = DAG.getNode(ISD::ADD, dl, ResTy, {T5, T6});
1281 return T7;
1282}
1283
1284SDValue
Krzysztof Parzyszek6b589e52017-12-18 18:32:27 +00001285HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
1286 // Sign- and zero-extends are legal.
1287 assert(Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG);
1288 return DAG.getZeroExtendVectorInReg(Op.getOperand(0), SDLoc(Op), ty(Op));
1289}
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001290
1291SDValue
1292HexagonTargetLowering::LowerHvxShift(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001293 if (SDValue S = getVectorShiftByInt(Op, DAG))
1294 return S;
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001295 return Op;
1296}
1297
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001298SDValue
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001299HexagonTargetLowering::LowerHvxUnalignedLoad(SDValue Op, SelectionDAG &DAG)
1300 const {
1301 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
1302 unsigned HaveAlign = LN->getAlignment();
1303 MVT VecTy = ty(Op);
1304 Type *Ty = EVT(VecTy).getTypeForEVT(*DAG.getContext());
1305 const DataLayout &DL = DAG.getDataLayout();
1306 unsigned NeedAlign = DL.getABITypeAlignment(Ty);
1307 if (HaveAlign >= NeedAlign || !ExpandUnalignedLoads)
1308 return Op;
1309
1310 unsigned HwLen = Subtarget.getVectorLength();
1311
1312 SDValue Base = LN->getBasePtr();
1313 SDValue Chain = LN->getChain();
1314 auto BO = getBaseAndOffset(Base);
1315 unsigned BaseOpc = BO.first.getOpcode();
1316 if (BaseOpc == HexagonISD::VALIGNADDR && BO.second % HwLen == 0)
1317 return Op;
1318
1319 const SDLoc &dl(Op);
1320 if (BO.second % HwLen != 0) {
1321 BO.first = DAG.getNode(ISD::ADD, dl, MVT::i32, BO.first,
1322 DAG.getConstant(BO.second % HwLen, dl, MVT::i32));
1323 BO.second -= BO.second % HwLen;
1324 }
1325 SDValue BaseNoOff = (BaseOpc != HexagonISD::VALIGNADDR)
1326 ? DAG.getNode(HexagonISD::VALIGNADDR, dl, MVT::i32, BO.first)
1327 : BO.first;
1328 SDValue Base0 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second, dl);
1329 SDValue Base1 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second+HwLen, dl);
1330
1331 MachineMemOperand *WideMMO = nullptr;
1332 if (MachineMemOperand *MMO = LN->getMemOperand()) {
1333 MachineFunction &MF = DAG.getMachineFunction();
1334 WideMMO = MF.getMachineMemOperand(MMO->getPointerInfo(), MMO->getFlags(),
1335 2*HwLen, HwLen, MMO->getAAInfo(), MMO->getRanges(),
1336 MMO->getSyncScopeID(), MMO->getOrdering(),
1337 MMO->getFailureOrdering());
1338 }
1339
1340 SDValue Load0 = DAG.getLoad(VecTy, dl, Chain, Base0, WideMMO);
1341 SDValue Load1 = DAG.getLoad(VecTy, dl, Chain, Base1, WideMMO);
1342
1343 SDValue Aligned = getInstr(Hexagon::V6_valignb, dl, VecTy,
1344 {Load1, Load0, BaseNoOff.getOperand(0)}, DAG);
1345 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1346 Load0.getValue(1), Load1.getValue(1));
1347 SDValue M = DAG.getMergeValues({Aligned, NewChain}, dl);
1348 return M;
1349}
1350
1351SDValue
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001352HexagonTargetLowering::SplitHvxPairOp(SDValue Op, SelectionDAG &DAG) const {
1353 assert(!Op.isMachineOpcode());
1354 SmallVector<SDValue,2> OpsL, OpsH;
1355 const SDLoc &dl(Op);
1356
1357 auto SplitVTNode = [&DAG,this] (const VTSDNode *N) {
1358 MVT Ty = typeSplit(N->getVT().getSimpleVT()).first;
1359 SDValue TV = DAG.getValueType(Ty);
1360 return std::make_pair(TV, TV);
1361 };
1362
1363 for (SDValue A : Op.getNode()->ops()) {
1364 VectorPair P = Subtarget.isHVXVectorType(ty(A), true)
1365 ? opSplit(A, dl, DAG)
1366 : std::make_pair(A, A);
1367 // Special case for type operand.
1368 if (Op.getOpcode() == ISD::SIGN_EXTEND_INREG) {
1369 if (const auto *N = dyn_cast<const VTSDNode>(A.getNode()))
1370 P = SplitVTNode(N);
1371 }
1372 OpsL.push_back(P.first);
1373 OpsH.push_back(P.second);
1374 }
1375
1376 MVT ResTy = ty(Op);
1377 MVT HalfTy = typeSplit(ResTy).first;
1378 SDValue L = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsL);
1379 SDValue H = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsH);
1380 SDValue S = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, L, H);
1381 return S;
1382}
1383
1384SDValue
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001385HexagonTargetLowering::SplitHvxMemOp(SDValue Op, SelectionDAG &DAG) const {
1386 LSBaseSDNode *BN = cast<LSBaseSDNode>(Op.getNode());
1387 assert(BN->isUnindexed());
1388 MVT MemTy = BN->getMemoryVT().getSimpleVT();
1389 if (!isHvxPairTy(MemTy))
1390 return Op;
1391
1392 const SDLoc &dl(Op);
1393 unsigned HwLen = Subtarget.getVectorLength();
1394 MVT SingleTy = typeSplit(MemTy).first;
1395 SDValue Chain = BN->getChain();
1396 SDValue Base0 = BN->getBasePtr();
1397 SDValue Base1 = DAG.getMemBasePlusOffset(Base0, HwLen, dl);
1398
1399 MachineMemOperand *MOp0 = nullptr, *MOp1 = nullptr;
1400 if (MachineMemOperand *MMO = BN->getMemOperand()) {
1401 MachineFunction &MF = DAG.getMachineFunction();
1402 MOp0 = MF.getMachineMemOperand(MMO, 0, HwLen);
1403 MOp1 = MF.getMachineMemOperand(MMO, HwLen, HwLen);
1404 }
1405
1406 unsigned MemOpc = BN->getOpcode();
1407 SDValue NewOp;
1408
1409 if (MemOpc == ISD::LOAD) {
1410 SDValue Load0 = DAG.getLoad(SingleTy, dl, Chain, Base0, MOp0);
1411 SDValue Load1 = DAG.getLoad(SingleTy, dl, Chain, Base1, MOp1);
1412 NewOp = DAG.getMergeValues(
1413 { DAG.getNode(ISD::CONCAT_VECTORS, dl, MemTy, Load0, Load1),
1414 DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1415 Load0.getValue(1), Load1.getValue(1)) }, dl);
1416 } else {
1417 assert(MemOpc == ISD::STORE);
1418 VectorPair Vals = opSplit(cast<StoreSDNode>(Op)->getValue(), dl, DAG);
1419 SDValue Store0 = DAG.getStore(Chain, dl, Vals.first, Base0, MOp0);
1420 SDValue Store1 = DAG.getStore(Chain, dl, Vals.second, Base1, MOp1);
1421 NewOp = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store0, Store1);
1422 }
1423
1424 return NewOp;
1425}
1426
1427SDValue
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001428HexagonTargetLowering::LowerHvxOperation(SDValue Op, SelectionDAG &DAG) const {
1429 unsigned Opc = Op.getOpcode();
1430 bool IsPairOp = isHvxPairTy(ty(Op)) ||
1431 llvm::any_of(Op.getNode()->ops(), [this] (SDValue V) {
1432 return isHvxPairTy(ty(V));
1433 });
1434
1435 if (IsPairOp) {
1436 switch (Opc) {
1437 default:
1438 break;
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001439 case ISD::LOAD:
1440 case ISD::STORE:
1441 return SplitHvxMemOp(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001442 case ISD::MUL:
1443 case ISD::MULHS:
1444 case ISD::MULHU:
1445 case ISD::AND:
1446 case ISD::OR:
1447 case ISD::XOR:
1448 case ISD::SRA:
1449 case ISD::SHL:
1450 case ISD::SRL:
1451 case ISD::SETCC:
1452 case ISD::VSELECT:
1453 case ISD::SIGN_EXTEND_INREG:
1454 return SplitHvxPairOp(Op, DAG);
1455 }
1456 }
1457
1458 switch (Opc) {
1459 default:
1460 break;
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001461 case ISD::BUILD_VECTOR: return LowerHvxBuildVector(Op, DAG);
1462 case ISD::CONCAT_VECTORS: return LowerHvxConcatVectors(Op, DAG);
1463 case ISD::INSERT_SUBVECTOR: return LowerHvxInsertSubvector(Op, DAG);
1464 case ISD::INSERT_VECTOR_ELT: return LowerHvxInsertElement(Op, DAG);
1465 case ISD::EXTRACT_SUBVECTOR: return LowerHvxExtractSubvector(Op, DAG);
1466 case ISD::EXTRACT_VECTOR_ELT: return LowerHvxExtractElement(Op, DAG);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001467
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001468 case ISD::LOAD: return LowerHvxUnalignedLoad(Op, DAG);
1469 case ISD::ANY_EXTEND: return LowerHvxAnyExt(Op, DAG);
1470 case ISD::SIGN_EXTEND: return LowerHvxSignExt(Op, DAG);
1471 case ISD::ZERO_EXTEND: return LowerHvxZeroExt(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001472 case ISD::SRA:
1473 case ISD::SHL:
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001474 case ISD::SRL: return LowerHvxShift(Op, DAG);
1475 case ISD::MUL: return LowerHvxMul(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001476 case ISD::MULHS:
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001477 case ISD::MULHU: return LowerHvxMulh(Op, DAG);
1478 case ISD::ANY_EXTEND_VECTOR_INREG: return LowerHvxExtend(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001479 case ISD::SETCC:
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001480 case ISD::INTRINSIC_VOID: return Op;
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001481 }
1482#ifndef NDEBUG
1483 Op.dumpr(&DAG);
1484#endif
1485 llvm_unreachable("Unhandled HVX operation");
1486}
1487
1488bool
1489HexagonTargetLowering::isHvxOperation(SDValue Op) const {
1490 // If the type of the result, or any operand type are HVX vector types,
1491 // this is an HVX operation.
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001492 return Subtarget.isHVXVectorType(ty(Op), true) ||
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001493 llvm::any_of(Op.getNode()->ops(),
1494 [this] (SDValue V) {
1495 return Subtarget.isHVXVectorType(ty(V), true);
1496 });
1497}