blob: 652dffce841246fa266cd05c830f0b5a9c49ec1e [file] [log] [blame]
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001//===-- HexagonISelLoweringHVX.cpp --- Lowering HVX operations ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "HexagonISelLowering.h"
11#include "HexagonRegisterInfo.h"
12#include "HexagonSubtarget.h"
13
14using namespace llvm;
15
16SDValue
17HexagonTargetLowering::getInt(unsigned IntId, MVT ResTy, ArrayRef<SDValue> Ops,
18 const SDLoc &dl, SelectionDAG &DAG) const {
19 SmallVector<SDValue,4> IntOps;
20 IntOps.push_back(DAG.getConstant(IntId, dl, MVT::i32));
21 for (const SDValue &Op : Ops)
22 IntOps.push_back(Op);
23 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, ResTy, IntOps);
24}
25
26MVT
27HexagonTargetLowering::typeJoin(const TypePair &Tys) const {
28 assert(Tys.first.getVectorElementType() == Tys.second.getVectorElementType());
29
30 MVT ElemTy = Tys.first.getVectorElementType();
31 return MVT::getVectorVT(ElemTy, Tys.first.getVectorNumElements() +
32 Tys.second.getVectorNumElements());
33}
34
35HexagonTargetLowering::TypePair
36HexagonTargetLowering::typeSplit(MVT VecTy) const {
37 assert(VecTy.isVector());
38 unsigned NumElem = VecTy.getVectorNumElements();
39 assert((NumElem % 2) == 0 && "Expecting even-sized vector type");
40 MVT HalfTy = MVT::getVectorVT(VecTy.getVectorElementType(), NumElem/2);
41 return { HalfTy, HalfTy };
42}
43
44MVT
45HexagonTargetLowering::typeExtElem(MVT VecTy, unsigned Factor) const {
46 MVT ElemTy = VecTy.getVectorElementType();
47 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() * Factor);
48 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
49}
50
51MVT
52HexagonTargetLowering::typeTruncElem(MVT VecTy, unsigned Factor) const {
53 MVT ElemTy = VecTy.getVectorElementType();
54 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() / Factor);
55 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
56}
57
58SDValue
59HexagonTargetLowering::opCastElem(SDValue Vec, MVT ElemTy,
60 SelectionDAG &DAG) const {
61 if (ty(Vec).getVectorElementType() == ElemTy)
62 return Vec;
63 MVT CastTy = tyVector(Vec.getValueType().getSimpleVT(), ElemTy);
64 return DAG.getBitcast(CastTy, Vec);
65}
66
67SDValue
68HexagonTargetLowering::opJoin(const VectorPair &Ops, const SDLoc &dl,
69 SelectionDAG &DAG) const {
70 return DAG.getNode(ISD::CONCAT_VECTORS, dl, typeJoin(ty(Ops)),
71 Ops.second, Ops.first);
72}
73
74HexagonTargetLowering::VectorPair
75HexagonTargetLowering::opSplit(SDValue Vec, const SDLoc &dl,
76 SelectionDAG &DAG) const {
77 TypePair Tys = typeSplit(ty(Vec));
78 return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
79}
80
81SDValue
82HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
83 SelectionDAG &DAG) const {
84 if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
85 ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
86
87 unsigned ElemWidth = ElemTy.getSizeInBits();
88 if (ElemWidth == 8)
89 return ElemIdx;
90
91 unsigned L = Log2_32(ElemWidth/8);
92 const SDLoc &dl(ElemIdx);
93 return DAG.getNode(ISD::SHL, dl, MVT::i32,
94 {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
95}
96
97SDValue
98HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
99 SelectionDAG &DAG) const {
100 unsigned ElemWidth = ElemTy.getSizeInBits();
101 assert(ElemWidth >= 8 && ElemWidth <= 32);
102 if (ElemWidth == 32)
103 return Idx;
104
105 if (ty(Idx) != MVT::i32)
106 Idx = DAG.getBitcast(MVT::i32, Idx);
107 const SDLoc &dl(Idx);
108 SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
109 SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
110 return SubIdx;
111}
112
113SDValue
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000114HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
115 SDValue Op1, ArrayRef<int> Mask,
116 SelectionDAG &DAG) const {
117 MVT OpTy = ty(Op0);
118 assert(OpTy == ty(Op1));
119
120 MVT ElemTy = OpTy.getVectorElementType();
121 if (ElemTy == MVT::i8)
122 return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
123 assert(ElemTy.getSizeInBits() >= 8);
124
125 MVT ResTy = tyVector(OpTy, MVT::i8);
126 unsigned ElemSize = ElemTy.getSizeInBits() / 8;
127
128 SmallVector<int,128> ByteMask;
129 for (int M : Mask) {
130 if (M < 0) {
131 for (unsigned I = 0; I != ElemSize; ++I)
132 ByteMask.push_back(-1);
133 } else {
134 int NewM = M*ElemSize;
135 for (unsigned I = 0; I != ElemSize; ++I)
136 ByteMask.push_back(NewM+I);
137 }
138 }
139 assert(ResTy.getVectorNumElements() == ByteMask.size());
140 return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
141 opCastElem(Op1, MVT::i8, DAG), ByteMask);
142}
143
144SDValue
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000145HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
146 const {
147 const SDLoc &dl(Op);
148 BuildVectorSDNode *BN = cast<BuildVectorSDNode>(Op.getNode());
149 bool IsConst = BN->isConstant();
150 MachineFunction &MF = DAG.getMachineFunction();
151 MVT VecTy = ty(Op);
152
153 if (IsConst) {
154 SmallVector<Constant*, 128> Elems;
155 for (SDValue V : BN->op_values()) {
156 if (auto *C = dyn_cast<ConstantSDNode>(V.getNode()))
157 Elems.push_back(const_cast<ConstantInt*>(C->getConstantIntValue()));
158 }
159 Constant *CV = ConstantVector::get(Elems);
160 unsigned Align = VecTy.getSizeInBits() / 8;
161 SDValue CP = LowerConstantPool(DAG.getConstantPool(CV, VecTy, Align), DAG);
162 return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
163 MachinePointerInfo::getConstantPool(MF), Align);
164 }
165
166 unsigned NumOps = Op.getNumOperands();
167 unsigned HwLen = Subtarget.getVectorLength();
168 unsigned ElemSize = VecTy.getVectorElementType().getSizeInBits() / 8;
169 assert(ElemSize*NumOps == HwLen);
170
171 SmallVector<SDValue,32> Words;
172 SmallVector<SDValue,32> Ops;
173 for (unsigned i = 0; i != NumOps; ++i)
174 Ops.push_back(Op.getOperand(i));
175
176 if (VecTy.getVectorElementType() != MVT::i32) {
177 assert(ElemSize < 4 && "vNi64 should have been promoted to vNi32");
178 assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
179 unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
180 MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
181 for (unsigned i = 0; i != NumOps; i += OpsPerWord) {
182 SDValue W = buildVector32({&Ops[i], OpsPerWord}, dl, PartVT, DAG);
183 Words.push_back(DAG.getBitcast(MVT::i32, W));
184 }
185 } else {
186 Words.assign(Ops.begin(), Ops.end());
187 }
188
189 // Construct two halves in parallel, then or them together.
190 assert(4*Words.size() == Subtarget.getVectorLength());
191 SDValue HalfV0 = getNode(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
192 SDValue HalfV1 = getNode(Hexagon::V6_vd0, dl, VecTy, {}, DAG);
193 SDValue S = DAG.getConstant(4, dl, MVT::i32);
194 unsigned NumWords = Words.size();
195 for (unsigned i = 0; i != NumWords/2; ++i) {
196 SDValue N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
197 {HalfV0, Words[i]});
198 SDValue M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
199 {HalfV1, Words[i+NumWords/2]});
200 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, S});
201 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, S});
202 }
203
204 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy,
205 {HalfV0, DAG.getConstant(HwLen/2, dl, MVT::i32)});
206 SDValue DstV = DAG.getNode(ISD::OR, dl, VecTy, {HalfV0, HalfV1});
207 return DstV;
208}
209
210SDValue
211HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
212 const {
213 // Change the type of the extracted element to i32.
214 SDValue VecV = Op.getOperand(0);
215 MVT ElemTy = ty(VecV).getVectorElementType();
216 unsigned ElemWidth = ElemTy.getSizeInBits();
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000217 assert(ElemWidth >= 8 && ElemWidth <= 32);
Tim Shen7654ed032017-12-06 19:22:19 +0000218 (void)ElemWidth;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000219
220 const SDLoc &dl(Op);
221 SDValue IdxV = Op.getOperand(1);
222 if (ty(IdxV) != MVT::i32)
223 IdxV = DAG.getBitcast(MVT::i32, IdxV);
224
225 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
226 SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
227 {VecV, ByteIdx});
228 if (ElemTy == MVT::i32)
229 return ExWord;
230
231 // Have an extracted word, need to extract the smaller element out of it.
232 // 1. Extract the bits of (the original) IdxV that correspond to the index
233 // of the desired element in the 32-bit word.
234 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
235 // 2. Extract the element from the word.
236 SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
237 return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
238}
239
240SDValue
241HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
242 const {
243 const SDLoc &dl(Op);
244 SDValue VecV = Op.getOperand(0);
245 SDValue ValV = Op.getOperand(1);
246 SDValue IdxV = Op.getOperand(2);
247 MVT ElemTy = ty(VecV).getVectorElementType();
248 unsigned ElemWidth = ElemTy.getSizeInBits();
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000249 assert(ElemWidth >= 8 && ElemWidth <= 32);
Tim Shen7654ed032017-12-06 19:22:19 +0000250 (void)ElemWidth;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000251
252 auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
253 SDValue ByteIdxV) {
254 MVT VecTy = ty(VecV);
255 unsigned HwLen = Subtarget.getVectorLength();
256 SDValue MaskV = DAG.getNode(ISD::AND, dl, MVT::i32,
257 {ByteIdxV, DAG.getConstant(-4, dl, MVT::i32)});
258 SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
259 SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
260 SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
261 {DAG.getConstant(HwLen/4, dl, MVT::i32), MaskV});
262 SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
263 return TorV;
264 };
265
266 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
267 if (ElemTy == MVT::i32)
268 return InsertWord(VecV, ValV, ByteIdx);
269
270 // If this is not inserting a 32-bit word, convert it into such a thing.
271 // 1. Extract the existing word from the target vector.
272 SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
273 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
274 SDValue Ex0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
275 {opCastElem(VecV, MVT::i32, DAG), WordIdx});
276 SDValue Ext = LowerHvxExtractElement(Ex0, DAG);
277
278 // 2. Treating the extracted word as a 32-bit vector, insert the given
279 // value into it.
280 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
281 MVT SubVecTy = tyVector(ty(Ext), ElemTy);
282 SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
Krzysztof Parzyszek15241452017-12-11 14:46:06 +0000283 ValV, SubIdx, dl, ElemTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000284
285 // 3. Insert the 32-bit word back into the original vector.
286 return InsertWord(VecV, Ins, ByteIdx);
287}
288
289SDValue
290HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
291 const {
292 SDValue SrcV = Op.getOperand(0);
293 MVT SrcTy = ty(SrcV);
294 unsigned SrcElems = SrcTy.getVectorNumElements();
295 SDValue IdxV = Op.getOperand(1);
296 unsigned Idx = cast<ConstantSDNode>(IdxV.getNode())->getZExtValue();
297 MVT DstTy = ty(Op);
298 assert(Idx == 0 || DstTy.getVectorNumElements() % Idx == 0);
299 const SDLoc &dl(Op);
300 if (Idx == 0)
301 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, DstTy, SrcV);
302 if (Idx == SrcElems/2)
303 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, DstTy, SrcV);
304 return SDValue();
305}
306
307SDValue
308HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
309 const {
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000310 // Idx may be variable.
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000311 SDValue IdxV = Op.getOperand(2);
312 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
313 if (!IdxN)
314 return SDValue();
315 unsigned Idx = IdxN->getZExtValue();
316
317 SDValue DstV = Op.getOperand(0);
318 SDValue SrcV = Op.getOperand(1);
319 MVT DstTy = ty(DstV);
320 MVT SrcTy = ty(SrcV);
321 unsigned DstElems = DstTy.getVectorNumElements();
322 unsigned SrcElems = SrcTy.getVectorNumElements();
323 if (2*SrcElems != DstElems)
324 return SDValue();
325
326 const SDLoc &dl(Op);
327 if (Idx == 0)
328 return DAG.getTargetInsertSubreg(Hexagon::vsub_lo, dl, DstTy, DstV, SrcV);
329 if (Idx == SrcElems)
330 return DAG.getTargetInsertSubreg(Hexagon::vsub_hi, dl, DstTy, DstV, SrcV);
331 return SDValue();
332}
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +0000333
334SDValue
335HexagonTargetLowering::LowerHvxMul(SDValue Op, SelectionDAG &DAG) const {
336 MVT ResTy = ty(Op);
337 if (!ResTy.isVector())
338 return SDValue();
339 const SDLoc &dl(Op);
340 SmallVector<int,256> ShuffMask;
341
342 MVT ElemTy = ResTy.getVectorElementType();
343 unsigned VecLen = ResTy.getVectorNumElements();
344 SDValue Vs = Op.getOperand(0);
345 SDValue Vt = Op.getOperand(1);
346
347 switch (ElemTy.SimpleTy) {
348 case MVT::i8:
349 case MVT::i16: {
350 // For i8 vectors Vs = (a0, a1, ...), Vt = (b0, b1, ...),
351 // V6_vmpybv Vs, Vt produces a pair of i16 vectors Hi:Lo,
352 // where Lo = (a0*b0, a2*b2, ...), Hi = (a1*b1, a3*b3, ...).
353 // For i16, use V6_vmpyhv, which behaves in an analogous way to
354 // V6_vmpybv: results Lo and Hi are products of even/odd elements
355 // respectively.
356 MVT ExtTy = typeExtElem(ResTy, 2);
357 unsigned MpyOpc = ElemTy == MVT::i8 ? Hexagon::V6_vmpybv
358 : Hexagon::V6_vmpyhv;
359 SDValue M = getNode(MpyOpc, dl, ExtTy, {Vs, Vt}, DAG);
360
361 // Discard high halves of the resulting values, collect the low halves.
362 for (unsigned I = 0; I < VecLen; I += 2) {
363 ShuffMask.push_back(I); // Pick even element.
364 ShuffMask.push_back(I+VecLen); // Pick odd element.
365 }
366 VectorPair P = opSplit(opCastElem(M, ElemTy, DAG), dl, DAG);
367 return getByteShuffle(dl, P.first, P.second, ShuffMask, DAG);
368 }
369 case MVT::i32: {
370 // Use the following sequence for signed word multiply:
371 // T0 = V6_vmpyiowh Vs, Vt
372 // T1 = V6_vaslw T0, 16
373 // T2 = V6_vmpyiewuh_acc T1, Vs, Vt
374 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
375 SDValue T0 = getNode(Hexagon::V6_vmpyiowh, dl, ResTy, {Vs, Vt}, DAG);
376 SDValue T1 = getNode(Hexagon::V6_vaslw, dl, ResTy, {T0, S16}, DAG);
377 SDValue T2 = getNode(Hexagon::V6_vmpyiewuh_acc, dl, ResTy,
378 {T1, Vs, Vt}, DAG);
379 return T2;
380 }
381 default:
382 break;
383 }
384 return SDValue();
385}
Krzysztof Parzyszek47076052017-12-14 21:28:48 +0000386
387SDValue
388HexagonTargetLowering::LowerHvxSetCC(SDValue Op, SelectionDAG &DAG) const {
389 MVT VecTy = ty(Op.getOperand(0));
390 assert(VecTy == ty(Op.getOperand(1)));
391
392 SDValue Cmp = Op.getOperand(2);
393 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
394 bool Negate = false, Swap = false;
395
396 // HVX has instructions for SETEQ, SETGT, SETUGT. The other comparisons
397 // can be arranged as operand-swapped/negated versions of these. Since
398 // the generated code will have the original CC expressed as
399 // (negate (swap-op NewCmp)),
400 // the condition code for the NewCmp should be calculated from the original
401 // CC by applying these operations in the reverse order.
402
403 switch (CC) {
404 case ISD::SETNE: // !eq
405 case ISD::SETLE: // !gt
406 case ISD::SETGE: // !lt
407 case ISD::SETULE: // !ugt
408 case ISD::SETUGE: // !ult
409 CC = ISD::getSetCCInverse(CC, true);
410 Negate = true;
411 break;
412 default:
413 break;
414 }
415
416 switch (CC) {
417 case ISD::SETLT: // swap gt
418 case ISD::SETULT: // swap ugt
419 CC = ISD::getSetCCSwappedOperands(CC);
420 Swap = true;
421 break;
422 default:
423 break;
424 }
425
426 assert(CC == ISD::SETEQ || CC == ISD::SETGT || CC == ISD::SETUGT);
427
428 MVT ElemTy = VecTy.getVectorElementType();
429 unsigned ElemWidth = ElemTy.getSizeInBits();
430 assert(isPowerOf2_32(ElemWidth));
431
432 auto getIdx = [] (unsigned Code) {
433 static const unsigned Idx[] = { ISD::SETEQ, ISD::SETGT, ISD::SETUGT };
434 for (unsigned I = 0, E = array_lengthof(Idx); I != E; ++I)
435 if (Code == Idx[I])
436 return I;
437 llvm_unreachable("Unhandled CondCode");
438 };
439
440 static unsigned OpcTable[3][3] = {
441 // SETEQ SETGT, SETUGT
442 /* Byte */ { Hexagon::V6_veqb, Hexagon::V6_vgtb, Hexagon::V6_vgtub },
443 /* Half */ { Hexagon::V6_veqh, Hexagon::V6_vgth, Hexagon::V6_vgtuh },
444 /* Word */ { Hexagon::V6_veqw, Hexagon::V6_vgtw, Hexagon::V6_vgtuw }
445 };
446
447 unsigned CmpOpc = OpcTable[Log2_32(ElemWidth)-3][getIdx(CC)];
448
449 MVT ResTy = ty(Op);
450 const SDLoc &dl(Op);
451 SDValue OpL = Swap ? Op.getOperand(1) : Op.getOperand(0);
452 SDValue OpR = Swap ? Op.getOperand(0) : Op.getOperand(1);
453 SDValue CmpV = getNode(CmpOpc, dl, ResTy, {OpL, OpR}, DAG);
454 return Negate ? getNode(Hexagon::V6_pred_not, dl, ResTy, {CmpV}, DAG)
455 : CmpV;
456}