blob: 281cfcf9f89855fb66b64fc19222fa2008a4f5bd [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 Parzyszek8abaf892018-02-06 20:22:20 +000017static const MVT LegalV64[] = { MVT::v64i8, MVT::v32i16, MVT::v16i32 };
18static const MVT LegalW64[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
19static const MVT LegalV128[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
20static const MVT LegalW128[] = { MVT::v256i8, MVT::v128i16, MVT::v64i32 };
21
22
23void
24HexagonTargetLowering::initializeHVXLowering() {
25 if (Subtarget.useHVX64BOps()) {
26 addRegisterClass(MVT::v64i8, &Hexagon::HvxVRRegClass);
27 addRegisterClass(MVT::v32i16, &Hexagon::HvxVRRegClass);
28 addRegisterClass(MVT::v16i32, &Hexagon::HvxVRRegClass);
29 addRegisterClass(MVT::v128i8, &Hexagon::HvxWRRegClass);
30 addRegisterClass(MVT::v64i16, &Hexagon::HvxWRRegClass);
31 addRegisterClass(MVT::v32i32, &Hexagon::HvxWRRegClass);
32 // These "short" boolean vector types should be legal because
33 // they will appear as results of vector compares. If they were
34 // not legal, type legalization would try to make them legal
35 // and that would require using operations that do not use or
36 // produce such types. That, in turn, would imply using custom
37 // nodes, which would be unoptimizable by the DAG combiner.
38 // The idea is to rely on target-independent operations as much
39 // as possible.
40 addRegisterClass(MVT::v16i1, &Hexagon::HvxQRRegClass);
41 addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
42 addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
43 addRegisterClass(MVT::v512i1, &Hexagon::HvxQRRegClass);
44 } else if (Subtarget.useHVX128BOps()) {
45 addRegisterClass(MVT::v128i8, &Hexagon::HvxVRRegClass);
46 addRegisterClass(MVT::v64i16, &Hexagon::HvxVRRegClass);
47 addRegisterClass(MVT::v32i32, &Hexagon::HvxVRRegClass);
48 addRegisterClass(MVT::v256i8, &Hexagon::HvxWRRegClass);
49 addRegisterClass(MVT::v128i16, &Hexagon::HvxWRRegClass);
50 addRegisterClass(MVT::v64i32, &Hexagon::HvxWRRegClass);
51 addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
52 addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
53 addRegisterClass(MVT::v128i1, &Hexagon::HvxQRRegClass);
54 addRegisterClass(MVT::v1024i1, &Hexagon::HvxQRRegClass);
55 }
56
57 // Set up operation actions.
58
59 bool Use64b = Subtarget.useHVX64BOps();
60 ArrayRef<MVT> LegalV = Use64b ? LegalV64 : LegalV128;
61 ArrayRef<MVT> LegalW = Use64b ? LegalW64 : LegalW128;
62 MVT ByteV = Use64b ? MVT::v64i8 : MVT::v128i8;
63 MVT ByteW = Use64b ? MVT::v128i8 : MVT::v256i8;
64
65 auto setPromoteTo = [this] (unsigned Opc, MVT FromTy, MVT ToTy) {
66 setOperationAction(Opc, FromTy, Promote);
67 AddPromotedToType(Opc, FromTy, ToTy);
68 };
69
70 setOperationAction(ISD::VECTOR_SHUFFLE, ByteV, Legal);
71 setOperationAction(ISD::VECTOR_SHUFFLE, ByteW, Legal);
72 setOperationAction(ISD::AND, ByteV, Legal);
73 setOperationAction(ISD::OR, ByteV, Legal);
74 setOperationAction(ISD::XOR, ByteV, Legal);
75
76 for (MVT T : LegalV) {
77 setIndexedLoadAction(ISD::POST_INC, T, Legal);
78 setIndexedStoreAction(ISD::POST_INC, T, Legal);
79
80 setOperationAction(ISD::ADD, T, Legal);
81 setOperationAction(ISD::SUB, T, Legal);
82 if (T != ByteV) {
83 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, T, Legal);
84 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, T, Legal);
85 }
86
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +000087 setOperationAction(ISD::LOAD, T, Custom);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +000088 setOperationAction(ISD::MUL, T, Custom);
89 setOperationAction(ISD::MULHS, T, Custom);
90 setOperationAction(ISD::MULHU, T, Custom);
91 setOperationAction(ISD::BUILD_VECTOR, T, Custom);
92 // Make concat-vectors custom to handle concats of more than 2 vectors.
93 setOperationAction(ISD::CONCAT_VECTORS, T, Custom);
94 setOperationAction(ISD::INSERT_SUBVECTOR, T, Custom);
95 setOperationAction(ISD::INSERT_VECTOR_ELT, T, Custom);
96 setOperationAction(ISD::EXTRACT_SUBVECTOR, T, Custom);
97 setOperationAction(ISD::EXTRACT_VECTOR_ELT, T, Custom);
98 setOperationAction(ISD::ANY_EXTEND, T, Custom);
99 setOperationAction(ISD::SIGN_EXTEND, T, Custom);
100 setOperationAction(ISD::ZERO_EXTEND, T, Custom);
101 if (T != ByteV) {
102 setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, T, Custom);
103 // HVX only has shifts of words and halfwords.
104 setOperationAction(ISD::SRA, T, Custom);
105 setOperationAction(ISD::SHL, T, Custom);
106 setOperationAction(ISD::SRL, T, Custom);
107 }
108
109 setCondCodeAction(ISD::SETNE, T, Expand);
110 setCondCodeAction(ISD::SETLE, T, Expand);
111 setCondCodeAction(ISD::SETGE, T, Expand);
112 setCondCodeAction(ISD::SETLT, T, Expand);
113 setCondCodeAction(ISD::SETULE, T, Expand);
114 setCondCodeAction(ISD::SETUGE, T, Expand);
115 setCondCodeAction(ISD::SETULT, T, Expand);
116 }
117
118 for (MVT T : LegalV) {
119 MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
120 setOperationAction(ISD::BUILD_VECTOR, BoolV, Custom);
121 setOperationAction(ISD::CONCAT_VECTORS, BoolV, Custom);
122 setOperationAction(ISD::INSERT_SUBVECTOR, BoolV, Custom);
123 setOperationAction(ISD::INSERT_VECTOR_ELT, BoolV, Custom);
124 setOperationAction(ISD::EXTRACT_SUBVECTOR, BoolV, Custom);
125 setOperationAction(ISD::EXTRACT_VECTOR_ELT, BoolV, Custom);
126 }
127
128 for (MVT T : LegalV) {
129 if (T == ByteV)
130 continue;
131 // Promote all shuffles to operate on vectors of bytes.
132 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteV);
133 setPromoteTo(ISD::AND, T, ByteV);
134 setPromoteTo(ISD::OR, T, ByteV);
135 setPromoteTo(ISD::XOR, T, ByteV);
136 }
137
138 for (MVT T : LegalW) {
139 // Custom-lower BUILD_VECTOR for vector pairs. The standard (target-
140 // independent) handling of it would convert it to a load, which is
141 // not always the optimal choice.
142 setOperationAction(ISD::BUILD_VECTOR, T, Custom);
143 // Make concat-vectors custom to handle concats of more than 2 vectors.
144 setOperationAction(ISD::CONCAT_VECTORS, T, Custom);
145
146 // Custom-lower these operations for pairs. Expand them into a concat
147 // of the corresponding operations on individual vectors.
148 setOperationAction(ISD::ANY_EXTEND, T, Custom);
149 setOperationAction(ISD::SIGN_EXTEND, T, Custom);
150 setOperationAction(ISD::ZERO_EXTEND, T, Custom);
151 setOperationAction(ISD::SIGN_EXTEND_INREG, T, Custom);
152 setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, T, Custom);
153 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, T, Legal);
154 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, T, Legal);
155
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +0000156 setOperationAction(ISD::LOAD, T, Custom);
157 setOperationAction(ISD::STORE, T, Custom);
158
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +0000159 setOperationAction(ISD::ADD, T, Legal);
160 setOperationAction(ISD::SUB, T, Legal);
161 setOperationAction(ISD::MUL, T, Custom);
162 setOperationAction(ISD::MULHS, T, Custom);
163 setOperationAction(ISD::MULHU, T, Custom);
164 setOperationAction(ISD::AND, T, Custom);
165 setOperationAction(ISD::OR, T, Custom);
166 setOperationAction(ISD::XOR, T, Custom);
167 setOperationAction(ISD::SETCC, T, Custom);
168 setOperationAction(ISD::VSELECT, T, Custom);
169 if (T != ByteW) {
170 setOperationAction(ISD::SRA, T, Custom);
171 setOperationAction(ISD::SHL, T, Custom);
172 setOperationAction(ISD::SRL, T, Custom);
173
174 // Promote all shuffles to operate on vectors of bytes.
175 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteW);
176 }
177
178 MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
179 setOperationAction(ISD::SETCC, BoolV, Custom);
180 }
181}
182
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000183SDValue
184HexagonTargetLowering::getInt(unsigned IntId, MVT ResTy, ArrayRef<SDValue> Ops,
185 const SDLoc &dl, SelectionDAG &DAG) const {
186 SmallVector<SDValue,4> IntOps;
187 IntOps.push_back(DAG.getConstant(IntId, dl, MVT::i32));
188 for (const SDValue &Op : Ops)
189 IntOps.push_back(Op);
190 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, ResTy, IntOps);
191}
192
193MVT
194HexagonTargetLowering::typeJoin(const TypePair &Tys) const {
195 assert(Tys.first.getVectorElementType() == Tys.second.getVectorElementType());
196
197 MVT ElemTy = Tys.first.getVectorElementType();
198 return MVT::getVectorVT(ElemTy, Tys.first.getVectorNumElements() +
199 Tys.second.getVectorNumElements());
200}
201
202HexagonTargetLowering::TypePair
203HexagonTargetLowering::typeSplit(MVT VecTy) const {
204 assert(VecTy.isVector());
205 unsigned NumElem = VecTy.getVectorNumElements();
206 assert((NumElem % 2) == 0 && "Expecting even-sized vector type");
207 MVT HalfTy = MVT::getVectorVT(VecTy.getVectorElementType(), NumElem/2);
208 return { HalfTy, HalfTy };
209}
210
211MVT
212HexagonTargetLowering::typeExtElem(MVT VecTy, unsigned Factor) const {
213 MVT ElemTy = VecTy.getVectorElementType();
214 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() * Factor);
215 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
216}
217
218MVT
219HexagonTargetLowering::typeTruncElem(MVT VecTy, unsigned Factor) const {
220 MVT ElemTy = VecTy.getVectorElementType();
221 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() / Factor);
222 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
223}
224
225SDValue
226HexagonTargetLowering::opCastElem(SDValue Vec, MVT ElemTy,
227 SelectionDAG &DAG) const {
228 if (ty(Vec).getVectorElementType() == ElemTy)
229 return Vec;
230 MVT CastTy = tyVector(Vec.getValueType().getSimpleVT(), ElemTy);
231 return DAG.getBitcast(CastTy, Vec);
232}
233
234SDValue
235HexagonTargetLowering::opJoin(const VectorPair &Ops, const SDLoc &dl,
236 SelectionDAG &DAG) const {
237 return DAG.getNode(ISD::CONCAT_VECTORS, dl, typeJoin(ty(Ops)),
238 Ops.second, Ops.first);
239}
240
241HexagonTargetLowering::VectorPair
242HexagonTargetLowering::opSplit(SDValue Vec, const SDLoc &dl,
243 SelectionDAG &DAG) const {
244 TypePair Tys = typeSplit(ty(Vec));
Krzysztof Parzyszek1d52a852018-02-06 15:15:13 +0000245 if (Vec.getOpcode() == HexagonISD::QCAT)
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +0000246 return VectorPair(Vec.getOperand(0), Vec.getOperand(1));
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000247 return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
248}
249
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000250bool
251HexagonTargetLowering::isHvxSingleTy(MVT Ty) const {
252 return Subtarget.isHVXVectorType(Ty) &&
253 Ty.getSizeInBits() == 8 * Subtarget.getVectorLength();
254}
255
256bool
257HexagonTargetLowering::isHvxPairTy(MVT Ty) const {
258 return Subtarget.isHVXVectorType(Ty) &&
259 Ty.getSizeInBits() == 16 * Subtarget.getVectorLength();
260}
261
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000262SDValue
263HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
264 SelectionDAG &DAG) const {
265 if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
266 ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
267
268 unsigned ElemWidth = ElemTy.getSizeInBits();
269 if (ElemWidth == 8)
270 return ElemIdx;
271
272 unsigned L = Log2_32(ElemWidth/8);
273 const SDLoc &dl(ElemIdx);
274 return DAG.getNode(ISD::SHL, dl, MVT::i32,
275 {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
276}
277
278SDValue
279HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
280 SelectionDAG &DAG) const {
281 unsigned ElemWidth = ElemTy.getSizeInBits();
282 assert(ElemWidth >= 8 && ElemWidth <= 32);
283 if (ElemWidth == 32)
284 return Idx;
285
286 if (ty(Idx) != MVT::i32)
287 Idx = DAG.getBitcast(MVT::i32, Idx);
288 const SDLoc &dl(Idx);
289 SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
290 SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
291 return SubIdx;
292}
293
294SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000295HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
296 SDValue Op1, ArrayRef<int> Mask,
297 SelectionDAG &DAG) const {
298 MVT OpTy = ty(Op0);
299 assert(OpTy == ty(Op1));
300
301 MVT ElemTy = OpTy.getVectorElementType();
302 if (ElemTy == MVT::i8)
303 return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
304 assert(ElemTy.getSizeInBits() >= 8);
305
306 MVT ResTy = tyVector(OpTy, MVT::i8);
307 unsigned ElemSize = ElemTy.getSizeInBits() / 8;
308
309 SmallVector<int,128> ByteMask;
310 for (int M : Mask) {
311 if (M < 0) {
312 for (unsigned I = 0; I != ElemSize; ++I)
313 ByteMask.push_back(-1);
314 } else {
315 int NewM = M*ElemSize;
316 for (unsigned I = 0; I != ElemSize; ++I)
317 ByteMask.push_back(NewM+I);
318 }
319 }
320 assert(ResTy.getVectorNumElements() == ByteMask.size());
321 return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
322 opCastElem(Op1, MVT::i8, DAG), ByteMask);
323}
324
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000325SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000326HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
327 const SDLoc &dl, MVT VecTy,
328 SelectionDAG &DAG) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000329 unsigned VecLen = Values.size();
330 MachineFunction &MF = DAG.getMachineFunction();
331 MVT ElemTy = VecTy.getVectorElementType();
332 unsigned ElemWidth = ElemTy.getSizeInBits();
333 unsigned HwLen = Subtarget.getVectorLength();
334
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000335 unsigned ElemSize = ElemWidth / 8;
336 assert(ElemSize*VecLen == HwLen);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000337 SmallVector<SDValue,32> Words;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000338
339 if (VecTy.getVectorElementType() != MVT::i32) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000340 assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
341 unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
342 MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000343 for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
344 SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000345 Words.push_back(DAG.getBitcast(MVT::i32, W));
346 }
347 } else {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000348 Words.assign(Values.begin(), Values.end());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000349 }
350
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000351 unsigned NumWords = Words.size();
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000352 bool IsSplat = true, IsUndef = true;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000353 SDValue SplatV;
354 for (unsigned i = 0; i != NumWords && IsSplat; ++i) {
355 if (isUndef(Words[i]))
356 continue;
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000357 IsUndef = false;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000358 if (!SplatV.getNode())
359 SplatV = Words[i];
360 else if (SplatV != Words[i])
361 IsSplat = false;
362 }
Krzysztof Parzyszek82a83392018-01-31 16:52:15 +0000363 if (IsUndef)
364 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000365 if (IsSplat) {
366 assert(SplatV.getNode());
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000367 auto *IdxN = dyn_cast<ConstantSDNode>(SplatV.getNode());
368 if (IdxN && IdxN->isNullValue())
369 return getZero(dl, VecTy, DAG);
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +0000370 return DAG.getNode(HexagonISD::VSPLATW, dl, VecTy, SplatV);
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000371 }
372
373 // Delay recognizing constant vectors until here, so that we can generate
374 // a vsplat.
375 SmallVector<ConstantInt*, 128> Consts(VecLen);
376 bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
377 if (AllConst) {
378 ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
379 (Constant**)Consts.end());
380 Constant *CV = ConstantVector::get(Tmp);
381 unsigned Align = HwLen;
382 SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
383 return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
384 MachinePointerInfo::getConstantPool(MF), Align);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000385 }
386
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000387 // Construct two halves in parallel, then or them together.
388 assert(4*Words.size() == Subtarget.getVectorLength());
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000389 SDValue HalfV0 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
390 SDValue HalfV1 = getInstr(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000391 SDValue S = DAG.getConstant(4, dl, MVT::i32);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000392 for (unsigned i = 0; i != NumWords/2; ++i) {
393 SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
394 {HalfV0, Words[i]});
395 SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
396 {HalfV1, Words[i+NumWords/2]});
397 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
398 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
399 }
400
401 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
402 {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
403 SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
404 return DstV;
405}
406
407SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000408HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
409 unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
410 MVT PredTy = ty(PredV);
411 unsigned HwLen = Subtarget.getVectorLength();
412 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
413
414 if (Subtarget.isHVXVectorType(PredTy, true)) {
415 // Move the vector predicate SubV to a vector register, and scale it
416 // down to match the representation (bytes per type element) that VecV
417 // uses. The scaling down will pick every 2nd or 4th (every Scale-th
Hiroshi Inoue0909ca12018-01-26 08:15:29 +0000418 // in general) element and put them at the front of the resulting
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000419 // vector. This subvector will then be inserted into the Q2V of VecV.
420 // To avoid having an operation that generates an illegal type (short
421 // vector), generate a full size vector.
422 //
423 SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
424 SmallVector<int,128> Mask(HwLen);
425 // Scale = BitBytes(PredV) / Given BitBytes.
426 unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
427 unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
428
429 for (unsigned i = 0; i != HwLen; ++i) {
430 unsigned Num = i % Scale;
431 unsigned Off = i / Scale;
432 Mask[BlockLen*Num + Off] = i;
433 }
434 SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
435 if (!ZeroFill)
436 return S;
437 // Fill the bytes beyond BlockLen with 0s.
438 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000439 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
440 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000441 SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
442 return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
443 }
444
445 // Make sure that this is a valid scalar predicate.
446 assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
447
448 unsigned Bytes = 8 / PredTy.getVectorNumElements();
449 SmallVector<SDValue,4> Words[2];
450 unsigned IdxW = 0;
451
452 auto Lo32 = [&DAG, &dl] (SDValue P) {
453 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, P);
454 };
455 auto Hi32 = [&DAG, &dl] (SDValue P) {
456 return DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, P);
457 };
458
459 SDValue W0 = isUndef(PredV)
460 ? DAG.getUNDEF(MVT::i64)
461 : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
462 Words[IdxW].push_back(Hi32(W0));
463 Words[IdxW].push_back(Lo32(W0));
464
465 while (Bytes < BitBytes) {
466 IdxW ^= 1;
467 Words[IdxW].clear();
468
469 if (Bytes < 4) {
470 for (const SDValue &W : Words[IdxW ^ 1]) {
471 SDValue T = expandPredicate(W, dl, DAG);
472 Words[IdxW].push_back(Hi32(T));
473 Words[IdxW].push_back(Lo32(T));
474 }
475 } else {
476 for (const SDValue &W : Words[IdxW ^ 1]) {
477 Words[IdxW].push_back(W);
478 Words[IdxW].push_back(W);
479 }
480 }
481 Bytes *= 2;
482 }
483
484 assert(Bytes == BitBytes);
485
486 SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
487 SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
488 for (const SDValue &W : Words[IdxW]) {
489 Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
490 Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
491 }
492
493 return Vec;
494}
495
496SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000497HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
498 const SDLoc &dl, MVT VecTy,
499 SelectionDAG &DAG) const {
500 // Construct a vector V of bytes, such that a comparison V >u 0 would
501 // produce the required vector predicate.
502 unsigned VecLen = Values.size();
503 unsigned HwLen = Subtarget.getVectorLength();
504 assert(VecLen <= HwLen || VecLen == 8*HwLen);
505 SmallVector<SDValue,128> Bytes;
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000506 bool AllT = true, AllF = true;
507
508 auto IsTrue = [] (SDValue V) {
509 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
510 return !N->isNullValue();
511 return false;
512 };
513 auto IsFalse = [] (SDValue V) {
514 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
515 return N->isNullValue();
516 return false;
517 };
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000518
519 if (VecLen <= HwLen) {
520 // In the hardware, each bit of a vector predicate corresponds to a byte
521 // of a vector register. Calculate how many bytes does a bit of VecTy
522 // correspond to.
523 assert(HwLen % VecLen == 0);
524 unsigned BitBytes = HwLen / VecLen;
525 for (SDValue V : Values) {
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000526 AllT &= IsTrue(V);
527 AllF &= IsFalse(V);
528
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000529 SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000530 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000531 for (unsigned B = 0; B != BitBytes; ++B)
532 Bytes.push_back(Ext);
533 }
534 } else {
535 // There are as many i1 values, as there are bits in a vector register.
536 // Divide the values into groups of 8 and check that each group consists
537 // of the same value (ignoring undefs).
538 for (unsigned I = 0; I != VecLen; I += 8) {
539 unsigned B = 0;
540 // Find the first non-undef value in this group.
541 for (; B != 8; ++B) {
542 if (!Values[I+B].isUndef())
543 break;
544 }
545 SDValue F = Values[I+B];
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000546 AllT &= IsTrue(F);
547 AllF &= IsFalse(F);
548
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000549 SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000550 : DAG.getUNDEF(MVT::i8);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000551 Bytes.push_back(Ext);
552 // Verify that the rest of values in the group are the same as the
553 // first.
554 for (; B != 8; ++B)
555 assert(Values[I+B].isUndef() || Values[I+B] == F);
556 }
557 }
558
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +0000559 if (AllT)
560 return DAG.getNode(HexagonISD::QTRUE, dl, VecTy);
561 if (AllF)
562 return DAG.getNode(HexagonISD::QFALSE, dl, VecTy);
563
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000564 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000565 SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
566 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
567}
568
569SDValue
570HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
571 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
572 MVT ElemTy = ty(VecV).getVectorElementType();
573
574 unsigned ElemWidth = ElemTy.getSizeInBits();
575 assert(ElemWidth >= 8 && ElemWidth <= 32);
576 (void)ElemWidth;
577
578 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
579 SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
580 {VecV, ByteIdx});
581 if (ElemTy == MVT::i32)
582 return ExWord;
583
584 // Have an extracted word, need to extract the smaller element out of it.
585 // 1. Extract the bits of (the original) IdxV that correspond to the index
586 // of the desired element in the 32-bit word.
587 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
588 // 2. Extract the element from the word.
589 SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
590 return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
591}
592
593SDValue
594HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
595 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
596 // Implement other return types if necessary.
597 assert(ResTy == MVT::i1);
598
599 unsigned HwLen = Subtarget.getVectorLength();
600 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
601 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
602
603 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
604 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
605 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
606
607 SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
608 SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000609 return getInstr(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000610}
611
612SDValue
613HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
614 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
615 MVT ElemTy = ty(VecV).getVectorElementType();
616
617 unsigned ElemWidth = ElemTy.getSizeInBits();
618 assert(ElemWidth >= 8 && ElemWidth <= 32);
619 (void)ElemWidth;
620
621 auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
622 SDValue ByteIdxV) {
623 MVT VecTy = ty(VecV);
624 unsigned HwLen = Subtarget.getVectorLength();
625 SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
626 {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
627 SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
628 SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
629 SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
630 {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
631 SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
632 return TorV;
633 };
634
635 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
636 if (ElemTy == MVT::i32)
637 return InsertWord(VecV, ValV, ByteIdx);
638
639 // If this is not inserting a 32-bit word, convert it into such a thing.
640 // 1. Extract the existing word from the target vector.
641 SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
642 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
643 SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
644 dl, MVT::i32, DAG);
645
646 // 2. Treating the extracted word as a 32-bit vector, insert the given
647 // value into it.
648 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
649 MVT SubVecTy = tyVector(ty(Ext), ElemTy);
650 SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
651 ValV, SubIdx, dl, ElemTy, DAG);
652
653 // 3. Insert the 32-bit word back into the original vector.
654 return InsertWord(VecV, Ins, ByteIdx);
655}
656
657SDValue
658HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
659 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
660 unsigned HwLen = Subtarget.getVectorLength();
661 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
662 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
663
664 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
665 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
666 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
667 ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
668
669 SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
670 return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
671}
672
673SDValue
674HexagonTargetLowering::extractHvxSubvectorReg(SDValue VecV, SDValue IdxV,
675 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
676 MVT VecTy = ty(VecV);
677 unsigned HwLen = Subtarget.getVectorLength();
678 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
679 MVT ElemTy = VecTy.getVectorElementType();
680 unsigned ElemWidth = ElemTy.getSizeInBits();
681
682 // If the source vector is a vector pair, get the single vector containing
683 // the subvector of interest. The subvector will never overlap two single
684 // vectors.
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000685 if (isHvxPairTy(VecTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000686 unsigned SubIdx;
687 if (Idx * ElemWidth >= 8*HwLen) {
688 SubIdx = Hexagon::vsub_hi;
689 Idx -= VecTy.getVectorNumElements() / 2;
690 } else {
691 SubIdx = Hexagon::vsub_lo;
692 }
693 VecTy = typeSplit(VecTy).first;
694 VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
695 if (VecTy == ResTy)
696 return VecV;
697 }
698
699 // The only meaningful subvectors of a single HVX vector are those that
700 // fit in a scalar register.
701 assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
702
703 MVT WordTy = tyVector(VecTy, MVT::i32);
704 SDValue WordVec = DAG.getBitcast(WordTy, VecV);
705 unsigned WordIdx = (Idx*ElemWidth) / 32;
706
707 SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
708 SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
709 if (ResTy.getSizeInBits() == 32)
710 return DAG.getBitcast(ResTy, W0);
711
712 SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
713 SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
714 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64, {W1, W0});
715 return DAG.getBitcast(ResTy, WW);
716}
717
718SDValue
719HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
720 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
721 MVT VecTy = ty(VecV);
722 unsigned HwLen = Subtarget.getVectorLength();
723 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
724 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
725 // IdxV is required to be a constant.
726 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
727
728 unsigned ResLen = ResTy.getVectorNumElements();
729 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
730 unsigned Offset = Idx * BitBytes;
731 SDValue Undef = DAG.getUNDEF(ByteTy);
732 SmallVector<int,128> Mask;
733
734 if (Subtarget.isHVXVectorType(ResTy, true)) {
735 // Converting between two vector predicates. Since the result is shorter
736 // than the source, it will correspond to a vector predicate with the
737 // relevant bits replicated. The replication count is the ratio of the
738 // source and target vector lengths.
739 unsigned Rep = VecTy.getVectorNumElements() / ResLen;
740 assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
741 for (unsigned i = 0; i != HwLen/Rep; ++i) {
742 for (unsigned j = 0; j != Rep; ++j)
743 Mask.push_back(i + Offset);
744 }
745 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
746 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
747 }
748
749 // Converting between a vector predicate and a scalar predicate. In the
750 // vector predicate, a group of BitBytes bits will correspond to a single
751 // i1 element of the source vector type. Those bits will all have the same
752 // value. The same will be true for ByteVec, where each byte corresponds
753 // to a bit in the vector predicate.
754 // The algorithm is to traverse the ByteVec, going over the i1 values from
755 // the source vector, and generate the corresponding representation in an
756 // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
757 // elements so that the interesting 8 bytes will be in the low end of the
758 // vector.
759 unsigned Rep = 8 / ResLen;
760 // Make sure the output fill the entire vector register, so repeat the
761 // 8-byte groups as many times as necessary.
762 for (unsigned r = 0; r != HwLen/ResLen; ++r) {
763 // This will generate the indexes of the 8 interesting bytes.
764 for (unsigned i = 0; i != ResLen; ++i) {
765 for (unsigned j = 0; j != Rep; ++j)
766 Mask.push_back(Offset + i*BitBytes);
767 }
768 }
769
770 SDValue Zero = getZero(dl, MVT::i32, DAG);
771 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
772 // Combine the two low words from ShuffV into a v8i8, and byte-compare
773 // them against 0.
774 SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
775 SDValue W1 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
776 {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
777 SDValue Vec64 = DAG.getNode(HexagonISD::COMBINE, dl, MVT::v8i8, {W1, W0});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000778 return getInstr(Hexagon::A4_vcmpbgtui, dl, ResTy,
779 {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000780}
781
782SDValue
783HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
784 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
785 MVT VecTy = ty(VecV);
786 MVT SubTy = ty(SubV);
787 unsigned HwLen = Subtarget.getVectorLength();
788 MVT ElemTy = VecTy.getVectorElementType();
789 unsigned ElemWidth = ElemTy.getSizeInBits();
790
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000791 bool IsPair = isHvxPairTy(VecTy);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000792 MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
793 // The two single vectors that VecV consists of, if it's a pair.
794 SDValue V0, V1;
795 SDValue SingleV = VecV;
796 SDValue PickHi;
797
798 if (IsPair) {
799 V0 = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, VecV);
800 V1 = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, VecV);
801
802 SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
803 dl, MVT::i32);
804 PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
Krzysztof Parzyszek7b52cf12018-02-06 14:21:31 +0000805 if (isHvxSingleTy(SubTy)) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000806 if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
807 unsigned Idx = CN->getZExtValue();
808 assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
809 unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
810 return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
811 }
812 // If IdxV is not a constant, generate the two variants: with the
813 // SubV as the high and as the low subregister, and select the right
814 // pair based on the IdxV.
815 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
816 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
817 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
818 }
819 // The subvector being inserted must be entirely contained in one of
820 // the vectors V0 or V1. Set SingleV to the correct one, and update
821 // IdxV to be the index relative to the beginning of that vector.
822 SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
823 IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
824 SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
825 }
826
827 // The only meaningful subvectors of a single HVX vector are those that
828 // fit in a scalar register.
829 assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
830 // Convert IdxV to be index in bytes.
831 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
832 if (!IdxN || !IdxN->isNullValue()) {
833 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
834 DAG.getConstant(ElemWidth/8, dl, MVT::i32));
835 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
836 }
837 // When inserting a single word, the rotation back to the original position
838 // would be by HwLen-Idx, but if two words are inserted, it will need to be
839 // by (HwLen-4)-Idx.
840 unsigned RolBase = HwLen;
841 if (VecTy.getSizeInBits() == 32) {
842 SDValue V = DAG.getBitcast(MVT::i32, SubV);
843 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, V);
844 } else {
845 SDValue V = DAG.getBitcast(MVT::i64, SubV);
846 SDValue R0 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, V);
847 SDValue R1 = DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, V);
848 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
849 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
850 DAG.getConstant(4, dl, MVT::i32));
851 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
852 RolBase = HwLen-4;
853 }
854 // If the vector wasn't ror'ed, don't ror it back.
855 if (RolBase != 4 || !IdxN || !IdxN->isNullValue()) {
856 SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
857 DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
858 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
859 }
860
861 if (IsPair) {
862 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
863 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
864 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
865 }
866 return SingleV;
867}
868
869SDValue
870HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
871 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
872 MVT VecTy = ty(VecV);
873 MVT SubTy = ty(SubV);
874 assert(Subtarget.isHVXVectorType(VecTy, true));
875 // VecV is an HVX vector predicate. SubV may be either an HVX vector
876 // predicate as well, or it can be a scalar predicate.
877
878 unsigned VecLen = VecTy.getVectorNumElements();
879 unsigned HwLen = Subtarget.getVectorLength();
880 assert(HwLen % VecLen == 0 && "Unexpected vector type");
881
882 unsigned Scale = VecLen / SubTy.getVectorNumElements();
883 unsigned BitBytes = HwLen / VecLen;
884 unsigned BlockLen = HwLen / Scale;
885
886 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
887 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
888 SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
889 SDValue ByteIdx;
890
891 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
892 if (!IdxN || !IdxN->isNullValue()) {
893 ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
894 DAG.getConstant(BitBytes, dl, MVT::i32));
895 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
896 }
897
898 // ByteVec is the target vector VecV rotated in such a way that the
899 // subvector should be inserted at index 0. Generate a predicate mask
900 // and use vmux to do the insertion.
901 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +0000902 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
903 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
904 ByteVec = getInstr(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000905 // Rotate ByteVec back, and convert to a vector predicate.
906 if (!IdxN || !IdxN->isNullValue()) {
907 SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
908 SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
909 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
910 }
911 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
912}
913
914SDValue
915HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
916 MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
917 // Sign- and any-extending of a vector predicate to a vector register is
918 // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
919 // a vector of 1s (where the 1s are of type matching the vector type).
920 assert(Subtarget.isHVXVectorType(ResTy));
921 if (!ZeroExt)
922 return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
923
924 assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
925 SDValue True = DAG.getNode(HexagonISD::VSPLAT, dl, ResTy,
926 DAG.getConstant(1, dl, MVT::i32));
927 SDValue False = getZero(dl, ResTy, DAG);
928 return DAG.getSelect(dl, ResTy, VecV, True, False);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000929}
930
931SDValue
932HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
933 const {
934 const SDLoc &dl(Op);
935 MVT VecTy = ty(Op);
936
937 unsigned Size = Op.getNumOperands();
938 SmallVector<SDValue,128> Ops;
939 for (unsigned i = 0; i != Size; ++i)
940 Ops.push_back(Op.getOperand(i));
941
942 if (VecTy.getVectorElementType() == MVT::i1)
943 return buildHvxVectorPred(Ops, dl, VecTy, DAG);
944
945 if (VecTy.getSizeInBits() == 16*Subtarget.getVectorLength()) {
946 ArrayRef<SDValue> A(Ops);
947 MVT SingleTy = typeSplit(VecTy).first;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000948 SDValue V0 = buildHvxVectorReg(A.take_front(Size/2), dl, SingleTy, DAG);
949 SDValue V1 = buildHvxVectorReg(A.drop_front(Size/2), dl, SingleTy, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +0000950 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
951 }
952
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000953 return buildHvxVectorReg(Ops, dl, VecTy, DAG);
954}
955
956SDValue
957HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
958 const {
Krzysztof Parzyszek97a50952018-02-06 20:18:58 +0000959 // Vector concatenation of two integer (non-bool) vectors does not need
960 // special lowering. Custom-lower concats of bool vectors and expand
961 // concats of more than 2 vectors.
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000962 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000963 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +0000964 unsigned NumOp = Op.getNumOperands();
Krzysztof Parzyszek97a50952018-02-06 20:18:58 +0000965 if (VecTy.getVectorElementType() != MVT::i1) {
966 if (NumOp == 2)
967 return Op;
968 // Expand the other cases into a build-vector.
969 SmallVector<SDValue,8> Elems;
970 for (SDValue V : Op.getNode()->ops())
971 DAG.ExtractVectorElements(V, Elems);
Krzysztof Parzyszek2a9a83c2018-04-19 17:11:58 +0000972 // A vector of i16 will be broken up into a build_vector of i16's.
973 // This is a problem, since at the time of operation legalization,
974 // all operations are expected to be type-legalized, and i16 is not
975 // a legal type. If any of the extracted elements is not of a valid
976 // type, sign-extend it to a valid one.
977 for (unsigned i = 0, e = Elems.size(); i != e; ++i) {
978 SDValue V = Elems[i];
979 MVT Ty = ty(V);
980 if (!isTypeLegal(Ty)) {
981 EVT NTy = getTypeToTransformTo(*DAG.getContext(), Ty);
982 if (V.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
983 Elems[i] = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NTy,
984 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NTy,
985 V.getOperand(0), V.getOperand(1)),
986 DAG.getValueType(Ty));
987 continue;
988 }
989 // A few less complicated cases.
990 if (V.getOpcode() == ISD::Constant)
991 Elems[i] = DAG.getSExtOrTrunc(V, dl, NTy);
992 else if (V.isUndef())
993 Elems[i] = DAG.getUNDEF(NTy);
994 else
995 llvm_unreachable("Unexpected vector element");
996 }
997 }
Krzysztof Parzyszek97a50952018-02-06 20:18:58 +0000998 return DAG.getBuildVector(VecTy, dl, Elems);
999 }
1000
1001 assert(VecTy.getVectorElementType() == MVT::i1);
1002 unsigned HwLen = Subtarget.getVectorLength();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001003 assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001004
1005 SDValue Op0 = Op.getOperand(0);
1006
1007 // If the operands are HVX types (i.e. not scalar predicates), then
1008 // defer the concatenation, and create QCAT instead.
1009 if (Subtarget.isHVXVectorType(ty(Op0), true)) {
1010 if (NumOp == 2)
1011 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, Op0, Op.getOperand(1));
1012
1013 ArrayRef<SDUse> U(Op.getNode()->ops());
1014 SmallVector<SDValue,4> SV(U.begin(), U.end());
1015 ArrayRef<SDValue> Ops(SV);
1016
1017 MVT HalfTy = typeSplit(VecTy).first;
1018 SDValue V0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1019 Ops.take_front(NumOp/2));
1020 SDValue V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1021 Ops.take_back(NumOp/2));
1022 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, V0, V1);
1023 }
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001024
1025 // Count how many bytes (in a vector register) each bit in VecTy
1026 // corresponds to.
1027 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
1028
1029 SmallVector<SDValue,8> Prefixes;
1030 for (SDValue V : Op.getNode()->op_values()) {
1031 SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
1032 Prefixes.push_back(P);
1033 }
1034
1035 unsigned InpLen = ty(Op.getOperand(0)).getVectorNumElements();
1036 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1037 SDValue S = DAG.getConstant(InpLen*BitBytes, dl, MVT::i32);
1038 SDValue Res = getZero(dl, ByteTy, DAG);
1039 for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
1040 Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
1041 Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
1042 }
1043 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001044}
1045
1046SDValue
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001047HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
1048 const {
1049 // Change the type of the extracted element to i32.
1050 SDValue VecV = Op.getOperand(0);
1051 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001052 const SDLoc &dl(Op);
1053 SDValue IdxV = Op.getOperand(1);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001054 if (ElemTy == MVT::i1)
1055 return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001056
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001057 return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001058}
1059
1060SDValue
1061HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
1062 const {
1063 const SDLoc &dl(Op);
1064 SDValue VecV = Op.getOperand(0);
1065 SDValue ValV = Op.getOperand(1);
1066 SDValue IdxV = Op.getOperand(2);
1067 MVT ElemTy = ty(VecV).getVectorElementType();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001068 if (ElemTy == MVT::i1)
1069 return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001070
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001071 return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001072}
1073
1074SDValue
1075HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
1076 const {
1077 SDValue SrcV = Op.getOperand(0);
1078 MVT SrcTy = ty(SrcV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001079 MVT DstTy = ty(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001080 SDValue IdxV = Op.getOperand(1);
1081 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001082 assert(Idx % DstTy.getVectorNumElements() == 0);
1083 (void)Idx;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001084 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001085
1086 MVT ElemTy = SrcTy.getVectorElementType();
1087 if (ElemTy == MVT::i1)
1088 return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
1089
1090 return extractHvxSubvectorReg(SrcV, IdxV, dl, DstTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001091}
1092
1093SDValue
1094HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
1095 const {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001096 // Idx does not need to be a constant.
1097 SDValue VecV = Op.getOperand(0);
1098 SDValue ValV = Op.getOperand(1);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001099 SDValue IdxV = Op.getOperand(2);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001100
1101 const SDLoc &dl(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001102 MVT VecTy = ty(VecV);
1103 MVT ElemTy = VecTy.getVectorElementType();
1104 if (ElemTy == MVT::i1)
1105 return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
1106
1107 return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001108}
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001109
1110SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001111HexagonTargetLowering::LowerHvxAnyExt(SDValue Op, SelectionDAG &DAG) const {
1112 // Lower any-extends of boolean vectors to sign-extends, since they
1113 // translate directly to Q2V. Zero-extending could also be done equally
1114 // fast, but Q2V is used/recognized in more places.
1115 // For all other vectors, use zero-extend.
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 LowerHvxSignExt(Op, DAG);
1121 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(Op), ResTy, InpV);
1122}
1123
1124SDValue
1125HexagonTargetLowering::LowerHvxSignExt(SDValue Op, SelectionDAG &DAG) const {
1126 MVT ResTy = ty(Op);
1127 SDValue InpV = Op.getOperand(0);
1128 MVT ElemTy = ty(InpV).getVectorElementType();
1129 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1130 return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), false, DAG);
1131 return Op;
1132}
1133
1134SDValue
1135HexagonTargetLowering::LowerHvxZeroExt(SDValue Op, SelectionDAG &DAG) const {
1136 MVT ResTy = ty(Op);
1137 SDValue InpV = Op.getOperand(0);
1138 MVT ElemTy = ty(InpV).getVectorElementType();
1139 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
1140 return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), true, DAG);
1141 return Op;
1142}
1143
1144SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001145HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
1146 MVT ResTy = ty(Op);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001147 assert(ResTy.isVector() && isHvxSingleTy(ResTy));
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001148 const SDLoc &dl(Op);
1149 SmallVector<int,256> ShuffMask;
1150
1151 MVT ElemTy = ResTy.getVectorElementType();
1152 unsigned VecLen = ResTy.getVectorNumElements();
1153 SDValue Vs = Op.getOperand(0);
1154 SDValue Vt = Op.getOperand(1);
1155
1156 switch (ElemTy.SimpleTy) {
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +00001157 case MVT::i8: {
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001158 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1159 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1160 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001161 MVT ExtTy = typeExtElem(ResTy, 2);
1162 unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
1163 : Hexagon::V6_vmpyhv;
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001164 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001165
1166 // Discard high halves of the resulting values, collect the low halves.
1167 for (unsigned I = 0; I < VecLen; I += 2) {
1168 ShuffMask.push_back(I); // Pick even element.
1169 ShuffMask.push_back(I+VecLen); // Pick odd element.
1170 }
1171 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
Krzysztof Parzyszek0f5d9762018-01-05 20:45:34 +00001172 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1173 return DAG.getBitcast(ResTy, BS);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001174 }
Krzysztof Parzyszek02947b72018-02-05 15:40:06 +00001175 case MVT::i16:
1176 // For i16 there is V6_vmpyih, which acts exactly like the MUL opcode.
1177 // (There is also V6_vmpyhv, which behaves in an analogous way to
1178 // V6_vmpybv.)
1179 return getInstr(Hexagon::V6_vmpyih, dl, ResTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001180 case MVT::i32: {
1181 // Use the following sequence for signed word multiply:
1182 // T0 = V6_vmpyiowh Vs, Vt
1183 // T1 = V6_vaslw T0, 16
1184 // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
1185 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001186 SDValue T0 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
1187 SDValue T1 = getInstr(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
1188 SDValue T2 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1189 {T1, Vs, Vt}, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001190 return T2;
1191 }
1192 default:
1193 break;
1194 }
1195 return SDValue();
1196}
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001197
1198SDValue
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001199HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
1200 MVT ResTy = ty(Op);
1201 assert(ResTy.isVector());
1202 const SDLoc &dl(Op);
1203 SmallVector<int,256> ShuffMask;
1204
1205 MVT ElemTy = ResTy.getVectorElementType();
1206 unsigned VecLen = ResTy.getVectorNumElements();
1207 SDValue Vs = Op.getOperand(0);
1208 SDValue Vt = Op.getOperand(1);
1209 bool IsSigned = Op.getOpcode() == ISD::MULHS;
1210
1211 if (ElemTy == MVT::i8 || ElemTy == MVT::i16) {
1212 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
1213 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
1214 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
1215 // For i16, use V6_vmpyhv, which behaves in an analogous way to
1216 // V6_vmpybv: results Lo and Hi are products of even/odd elements
1217 // respectively.
1218 MVT ExtTy = typeExtElem(ResTy, 2);
1219 unsigned MpyOpc = ElemTy == MVT::i8
1220 ? (IsSigned ? Hexagon::V6_vmpybv : Hexagon::V6_vmpyubv)
1221 : (IsSigned ? Hexagon::V6_vmpyhv : Hexagon::V6_vmpyuhv);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001222 SDValue M = getInstr(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001223
1224 // Discard low halves of the resulting values, collect the high halves.
1225 for (unsigned I = 0; I < VecLen; I += 2) {
1226 ShuffMask.push_back(I+1); // Pick even element.
1227 ShuffMask.push_back(I+VecLen+1); // Pick odd element.
1228 }
1229 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
1230 SDValue BS = getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
1231 return DAG.getBitcast(ResTy, BS);
1232 }
1233
1234 assert(ElemTy == MVT::i32);
1235 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
1236
1237 if (IsSigned) {
1238 // mulhs(Vs,Vt) =
1239 // = [(Hi(Vs)*2^16 + Lo(Vs)) *s (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1240 // = [Hi(Vs)*2^16 *s Hi(Vt)*2^16 + Hi(Vs) *su Lo(Vt)*2^16
1241 // + Lo(Vs) *us (Hi(Vt)*2^16 + Lo(Vt))] >> 32
1242 // = [Hi(Vs) *s Hi(Vt)*2^32 + Hi(Vs) *su Lo(Vt)*2^16
1243 // + Lo(Vs) *us Vt] >> 32
1244 // The low half of Lo(Vs)*Lo(Vt) will be discarded (it's not added to
1245 // anything, so it cannot produce any carry over to higher bits),
1246 // so everything in [] can be shifted by 16 without loss of precision.
1247 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + Lo(Vs)*Vt >> 16] >> 16
1248 // = [Hi(Vs) *s Hi(Vt)*2^16 + Hi(Vs)*su Lo(Vt) + V6_vmpyewuh(Vs,Vt)] >> 16
1249 // Denote Hi(Vs) = Vs':
1250 // = [Vs'*s Hi(Vt)*2^16 + Vs' *su Lo(Vt) + V6_vmpyewuh(Vt,Vs)] >> 16
1251 // = Vs'*s Hi(Vt) + (V6_vmpyiewuh(Vs',Vt) + V6_vmpyewuh(Vt,Vs)) >> 16
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001252 SDValue T0 = getInstr(Hexagon::V6_vmpyewuh, dl, ResTy, {Vt, Vs}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001253 // Get Vs':
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001254 SDValue S0 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {Vs, S16}, DAG);
1255 SDValue T1 = getInstr(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
1256 {T0, S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001257 // Shift by 16:
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001258 SDValue S2 = getInstr(Hexagon::V6_vasrw, dl, ResTy, {T1, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001259 // Get Vs'*Hi(Vt):
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001260 SDValue T2 = getInstr(Hexagon::V6_vmpyiowh, dl, ResTy, {S0, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001261 // Add:
1262 SDValue T3 = DAG.getNode(ISD::ADD, dl, ResTy, {S2, T2});
1263 return T3;
1264 }
1265
1266 // Unsigned mulhw. (Would expansion using signed mulhw be better?)
1267
1268 auto LoVec = [&DAG,ResTy,dl] (SDValue Pair) {
1269 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResTy, Pair);
1270 };
1271 auto HiVec = [&DAG,ResTy,dl] (SDValue Pair) {
1272 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResTy, Pair);
1273 };
1274
1275 MVT PairTy = typeJoin({ResTy, ResTy});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001276 SDValue P = getInstr(Hexagon::V6_lvsplatw, dl, ResTy,
1277 {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001278 // Multiply-unsigned halfwords:
1279 // LoVec = Vs.uh[2i] * Vt.uh[2i],
1280 // HiVec = Vs.uh[2i+1] * Vt.uh[2i+1]
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001281 SDValue T0 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, Vt}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001282 // The low halves in the LoVec of the pair can be discarded. They are
1283 // not added to anything (in the full-precision product), so they cannot
1284 // produce a carry into the higher bits.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001285 SDValue T1 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {LoVec(T0), S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001286 // Swap low and high halves in Vt, and do the halfword multiplication
1287 // 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 +00001288 SDValue D0 = getInstr(Hexagon::V6_vdelta, dl, ResTy, {Vt, P}, DAG);
1289 SDValue T2 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {Vs, D0}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001290 // T2 has mixed products of halfwords: Lo(Vt)*Hi(Vs) and Hi(Vt)*Lo(Vs).
1291 // These products are words, but cannot be added directly because the
1292 // sums could overflow. Add these products, by halfwords, where each sum
1293 // of a pair of halfwords gives a word.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001294 SDValue T3 = getInstr(Hexagon::V6_vadduhw, dl, PairTy,
1295 {LoVec(T2), HiVec(T2)}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001296 // Add the high halfwords from the products of the low halfwords.
1297 SDValue T4 = DAG.getNode(ISD::ADD, dl, ResTy, {T1, LoVec(T3)});
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001298 SDValue T5 = getInstr(Hexagon::V6_vlsrw, dl, ResTy, {T4, S16}, DAG);
Krzysztof Parzyszek7fb738a2018-01-15 18:43:55 +00001299 SDValue T6 = DAG.getNode(ISD::ADD, dl, ResTy, {HiVec(T0), HiVec(T3)});
1300 SDValue T7 = DAG.getNode(ISD::ADD, dl, ResTy, {T5, T6});
1301 return T7;
1302}
1303
1304SDValue
Krzysztof Parzyszek6b589e52017-12-18 18:32:27 +00001305HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
1306 // Sign- and zero-extends are legal.
1307 assert(Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG);
1308 return DAG.getZeroExtendVectorInReg(Op.getOperand(0), SDLoc(Op), ty(Op));
1309}
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001310
1311SDValue
1312HexagonTargetLowering::LowerHvxShift(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001313 if (SDValue S = getVectorShiftByInt(Op, DAG))
1314 return S;
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001315 return Op;
1316}
1317
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001318SDValue
1319HexagonTargetLowering::SplitHvxPairOp(SDValue Op, SelectionDAG &DAG) const {
1320 assert(!Op.isMachineOpcode());
1321 SmallVector<SDValue,2> OpsL, OpsH;
1322 const SDLoc &dl(Op);
1323
1324 auto SplitVTNode = [&DAG,this] (const VTSDNode *N) {
1325 MVT Ty = typeSplit(N->getVT().getSimpleVT()).first;
1326 SDValue TV = DAG.getValueType(Ty);
1327 return std::make_pair(TV, TV);
1328 };
1329
1330 for (SDValue A : Op.getNode()->ops()) {
1331 VectorPair P = Subtarget.isHVXVectorType(ty(A), true)
1332 ? opSplit(A, dl, DAG)
1333 : std::make_pair(A, A);
1334 // Special case for type operand.
1335 if (Op.getOpcode() == ISD::SIGN_EXTEND_INREG) {
1336 if (const auto *N = dyn_cast<const VTSDNode>(A.getNode()))
1337 P = SplitVTNode(N);
1338 }
1339 OpsL.push_back(P.first);
1340 OpsH.push_back(P.second);
1341 }
1342
1343 MVT ResTy = ty(Op);
1344 MVT HalfTy = typeSplit(ResTy).first;
1345 SDValue L = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsL);
1346 SDValue H = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsH);
1347 SDValue S = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, L, H);
1348 return S;
1349}
1350
1351SDValue
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001352HexagonTargetLowering::SplitHvxMemOp(SDValue Op, SelectionDAG &DAG) const {
1353 LSBaseSDNode *BN = cast<LSBaseSDNode>(Op.getNode());
1354 assert(BN->isUnindexed());
1355 MVT MemTy = BN->getMemoryVT().getSimpleVT();
1356 if (!isHvxPairTy(MemTy))
1357 return Op;
1358
1359 const SDLoc &dl(Op);
1360 unsigned HwLen = Subtarget.getVectorLength();
1361 MVT SingleTy = typeSplit(MemTy).first;
1362 SDValue Chain = BN->getChain();
1363 SDValue Base0 = BN->getBasePtr();
1364 SDValue Base1 = DAG.getMemBasePlusOffset(Base0, HwLen, dl);
1365
1366 MachineMemOperand *MOp0 = nullptr, *MOp1 = nullptr;
1367 if (MachineMemOperand *MMO = BN->getMemOperand()) {
1368 MachineFunction &MF = DAG.getMachineFunction();
1369 MOp0 = MF.getMachineMemOperand(MMO, 0, HwLen);
1370 MOp1 = MF.getMachineMemOperand(MMO, HwLen, HwLen);
1371 }
1372
1373 unsigned MemOpc = BN->getOpcode();
1374 SDValue NewOp;
1375
1376 if (MemOpc == ISD::LOAD) {
1377 SDValue Load0 = DAG.getLoad(SingleTy, dl, Chain, Base0, MOp0);
1378 SDValue Load1 = DAG.getLoad(SingleTy, dl, Chain, Base1, MOp1);
1379 NewOp = DAG.getMergeValues(
1380 { DAG.getNode(ISD::CONCAT_VECTORS, dl, MemTy, Load0, Load1),
1381 DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1382 Load0.getValue(1), Load1.getValue(1)) }, dl);
1383 } else {
1384 assert(MemOpc == ISD::STORE);
1385 VectorPair Vals = opSplit(cast<StoreSDNode>(Op)->getValue(), dl, DAG);
1386 SDValue Store0 = DAG.getStore(Chain, dl, Vals.first, Base0, MOp0);
1387 SDValue Store1 = DAG.getStore(Chain, dl, Vals.second, Base1, MOp1);
1388 NewOp = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store0, Store1);
1389 }
1390
1391 return NewOp;
1392}
1393
1394SDValue
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001395HexagonTargetLowering::LowerHvxOperation(SDValue Op, SelectionDAG &DAG) const {
1396 unsigned Opc = Op.getOpcode();
1397 bool IsPairOp = isHvxPairTy(ty(Op)) ||
1398 llvm::any_of(Op.getNode()->ops(), [this] (SDValue V) {
1399 return isHvxPairTy(ty(V));
1400 });
1401
1402 if (IsPairOp) {
1403 switch (Opc) {
1404 default:
1405 break;
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001406 case ISD::LOAD:
1407 case ISD::STORE:
1408 return SplitHvxMemOp(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001409 case ISD::MUL:
1410 case ISD::MULHS:
1411 case ISD::MULHU:
1412 case ISD::AND:
1413 case ISD::OR:
1414 case ISD::XOR:
1415 case ISD::SRA:
1416 case ISD::SHL:
1417 case ISD::SRL:
1418 case ISD::SETCC:
1419 case ISD::VSELECT:
1420 case ISD::SIGN_EXTEND_INREG:
1421 return SplitHvxPairOp(Op, DAG);
1422 }
1423 }
1424
1425 switch (Opc) {
1426 default:
1427 break;
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001428 case ISD::BUILD_VECTOR: return LowerHvxBuildVector(Op, DAG);
1429 case ISD::CONCAT_VECTORS: return LowerHvxConcatVectors(Op, DAG);
1430 case ISD::INSERT_SUBVECTOR: return LowerHvxInsertSubvector(Op, DAG);
1431 case ISD::INSERT_VECTOR_ELT: return LowerHvxInsertElement(Op, DAG);
1432 case ISD::EXTRACT_SUBVECTOR: return LowerHvxExtractSubvector(Op, DAG);
1433 case ISD::EXTRACT_VECTOR_ELT: return LowerHvxExtractElement(Op, DAG);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001434
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001435 case ISD::ANY_EXTEND: return LowerHvxAnyExt(Op, DAG);
1436 case ISD::SIGN_EXTEND: return LowerHvxSignExt(Op, DAG);
1437 case ISD::ZERO_EXTEND: return LowerHvxZeroExt(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001438 case ISD::SRA:
1439 case ISD::SHL:
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001440 case ISD::SRL: return LowerHvxShift(Op, DAG);
1441 case ISD::MUL: return LowerHvxMul(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001442 case ISD::MULHS:
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001443 case ISD::MULHU: return LowerHvxMulh(Op, DAG);
1444 case ISD::ANY_EXTEND_VECTOR_INREG: return LowerHvxExtend(Op, DAG);
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001445 case ISD::SETCC:
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001446 case ISD::INTRINSIC_VOID: return Op;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001447 // Unaligned loads will be handled by the default lowering.
1448 case ISD::LOAD: return SDValue();
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001449 }
1450#ifndef NDEBUG
1451 Op.dumpr(&DAG);
1452#endif
1453 llvm_unreachable("Unhandled HVX operation");
1454}
1455
1456bool
1457HexagonTargetLowering::isHvxOperation(SDValue Op) const {
1458 // If the type of the result, or any operand type are HVX vector types,
1459 // this is an HVX operation.
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001460 return Subtarget.isHVXVectorType(ty(Op), true) ||
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001461 llvm::any_of(Op.getNode()->ops(),
1462 [this] (SDValue V) {
1463 return Subtarget.isHVXVectorType(ty(V), true);
1464 });
1465}