llvm.org GIT mirror llvm / 8cb3fe2
[X86] Move shuffle decoding for constant pool into the X86CodeGen library to remove a layering violation in the Util library. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256680 91177308-0d34-0410-b5e6-96231b3b80d8 Craig Topper 4 years ago
7 changed file(s) with 238 addition(s) and 181 deletion(s). Raw diff Collapse all Expand all
2626 X86PadShortFunction.cpp
2727 X86RegisterInfo.cpp
2828 X86SelectionDAGInfo.cpp
29 X86ShuffleDecodeConstantPool.cpp
2930 X86Subtarget.cpp
3031 X86TargetMachine.cpp
3132 X86TargetObjectFile.cpp
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "X86ShuffleDecode.h"
15 #include "llvm/IR/Constants.h"
1615 #include "llvm/CodeGen/MachineValueType.h"
1716
1817 //===----------------------------------------------------------------------===//
293292 for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)
294293 ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i);
295294 }
296 }
297
298 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl &ShuffleMask) {
299 Type *MaskTy = C->getType();
300 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
301 // constant pool uniques constants by their bit representation.
302 // e.g. the following take up the same space in the constant pool:
303 // i128 -170141183420855150465331762880109871104
304 //
305 // <2 x i64>
306 //
307 // <4 x i32>
308 // i32 -2147483648, i32 -2147483648>
309
310 #ifndef NDEBUG
311 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
312 assert(MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512);
313 #endif
314
315 // This is a straightforward byte vector.
316 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) {
317 int NumElements = MaskTy->getVectorNumElements();
318 ShuffleMask.reserve(NumElements);
319
320 for (int i = 0; i < NumElements; ++i) {
321 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
322 // lane of the vector we're inside.
323 int Base = i & ~0xf;
324 Constant *COp = C->getAggregateElement(i);
325 if (!COp) {
326 ShuffleMask.clear();
327 return;
328 } else if (isa(COp)) {
329 ShuffleMask.push_back(SM_SentinelUndef);
330 continue;
331 }
332 uint64_t Element = cast(COp)->getZExtValue();
333 // If the high bit (7) of the byte is set, the element is zeroed.
334 if (Element & (1 << 7))
335 ShuffleMask.push_back(SM_SentinelZero);
336 else {
337 // Only the least significant 4 bits of the byte are used.
338 int Index = Base + (Element & 0xf);
339 ShuffleMask.push_back(Index);
340 }
341 }
342 }
343 // TODO: Handle funny-looking vectors too.
344295 }
345296
346297 void DecodePSHUFBMask(ArrayRef RawMask,
387338 }
388339 }
389340
390 void DecodeVPERMILPMask(const Constant *C, unsigned ElSize,
391 SmallVectorImpl &ShuffleMask) {
392 Type *MaskTy = C->getType();
393 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
394 // constant pool uniques constants by their bit representation.
395 // e.g. the following take up the same space in the constant pool:
396 // i128 -170141183420855150465331762880109871104
397 //
398 // <2 x i64>
399 //
400 // <4 x i32>
401 // i32 -2147483648, i32 -2147483648>
402
403 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
404
405 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512.
406 return;
407
408 // Only support vector types.
409 if (!MaskTy->isVectorTy())
410 return;
411
412 // Make sure its an integer type.
413 Type *VecEltTy = MaskTy->getVectorElementType();
414 if (!VecEltTy->isIntegerTy())
415 return;
416
417 // Support any element type from byte up to element size.
418 // This is necesary primarily because 64-bit elements get split to 32-bit
419 // in the constant pool on 32-bit target.
420 unsigned EltTySize = VecEltTy->getIntegerBitWidth();
421 if (EltTySize < 8 || EltTySize > ElSize)
422 return;
423
424 unsigned NumElements = MaskTySize / ElSize;
425 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
426 "Unexpected number of vector elements.");
427 ShuffleMask.reserve(NumElements);
428 unsigned NumElementsPerLane = 128 / ElSize;
429 unsigned Factor = ElSize / EltTySize;
430
431 for (unsigned i = 0; i < NumElements; ++i) {
432 Constant *COp = C->getAggregateElement(i * Factor);
433 if (!COp) {
434 ShuffleMask.clear();
435 return;
436 } else if (isa(COp)) {
437 ShuffleMask.push_back(SM_SentinelUndef);
438 continue;
439 }
440 int Index = i & ~(NumElementsPerLane - 1);
441 uint64_t Element = cast(COp)->getZExtValue();
442 if (ElSize == 64)
443 Index += (Element >> 1) & 0x1;
444 else
445 Index += Element & 0x3;
446 ShuffleMask.push_back(Index);
447 }
448
449 // TODO: Handle funny-looking vectors too.
450 }
451
452341 void DecodeZeroExtendMask(MVT SrcVT, MVT DstVT, SmallVectorImpl &Mask) {
453342 unsigned NumDstElts = DstVT.getVectorNumElements();
454343 unsigned SrcScalarBits = SrcVT.getScalarSizeInBits();
571460 }
572461 }
573462
574 void DecodeVPERMVMask(const Constant *C, MVT VT,
575 SmallVectorImpl &ShuffleMask) {
576 Type *MaskTy = C->getType();
577 if (MaskTy->isVectorTy()) {
578 unsigned NumElements = MaskTy->getVectorNumElements();
579 if (NumElements == VT.getVectorNumElements()) {
580 for (unsigned i = 0; i < NumElements; ++i) {
581 Constant *COp = C->getAggregateElement(i);
582 if (!COp || (!isa(COp) && !isa(COp))) {
583 ShuffleMask.clear();
584 return;
585 }
586 if (isa(COp))
587 ShuffleMask.push_back(SM_SentinelUndef);
588 else {
589 uint64_t Element = cast(COp)->getZExtValue();
590 Element &= (1 << NumElements) - 1;
591 ShuffleMask.push_back(Element);
592 }
593 }
594 }
595 return;
596 }
597 // Scalar value; just broadcast it
598 if (!isa(C))
599 return;
600 uint64_t Element = cast(C)->getZExtValue();
601 int NumElements = VT.getVectorNumElements();
602 Element &= (1 << NumElements) - 1;
603 for (int i = 0; i < NumElements; ++i)
604 ShuffleMask.push_back(Element);
605 }
606
607 void DecodeVPERMV3Mask(const Constant *C, MVT VT,
608 SmallVectorImpl &ShuffleMask) {
609 Type *MaskTy = C->getType();
610 unsigned NumElements = MaskTy->getVectorNumElements();
611 if (NumElements == VT.getVectorNumElements()) {
612 for (unsigned i = 0; i < NumElements; ++i) {
613 Constant *COp = C->getAggregateElement(i);
614 if (!COp) {
615 ShuffleMask.clear();
616 return;
617 }
618 if (isa(COp))
619 ShuffleMask.push_back(SM_SentinelUndef);
620 else {
621 uint64_t Element = cast(COp)->getZExtValue();
622 Element &= (1 << NumElements*2) - 1;
623 ShuffleMask.push_back(Element);
624 }
625 }
626 }
627 }
628463 } // llvm namespace
2222 //===----------------------------------------------------------------------===//
2323
2424 namespace llvm {
25 class Constant;
2625 class MVT;
2726
2827 enum { SM_SentinelUndef = -1, SM_SentinelZero = -2 };
7170 /// different datatypes and vector widths.
7271 void DecodeUNPCKLMask(MVT VT, SmallVectorImpl &ShuffleMask);
7372
74 /// \brief Decode a PSHUFB mask from an IR-level vector constant.
75 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl &ShuffleMask);
76
7773 /// \brief Decode a PSHUFB mask from a raw array of constants such as from
7874 /// BUILD_VECTOR.
7975 void DecodePSHUFBMask(ArrayRef RawMask,
9490 /// No VT provided since it only works on 256-bit, 4 element vectors.
9591 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl &ShuffleMask);
9692
97 /// \brief Decode a VPERMILP variable mask from an IR-level vector constant.
98 void DecodeVPERMILPMask(const Constant *C, unsigned ElSize,
99 SmallVectorImpl &ShuffleMask);
100
10193 /// \brief Decode a zero extension instruction as a shuffle mask.
10294 void DecodeZeroExtendMask(MVT SrcVT, MVT DstVT,
10395 SmallVectorImpl &ShuffleMask);
117109 void DecodeINSERTQIMask(int Len, int Idx,
118110 SmallVectorImpl &ShuffleMask);
119111
120 /// \brief Decode a VPERM W/D/Q/PS/PD mask from an IR-level vector constant.
121 void DecodeVPERMVMask(const Constant *C, MVT VT,
122 SmallVectorImpl &ShuffleMask);
123
124112 /// \brief Decode a VPERM W/D/Q/PS/PD mask from a raw array of constants.
125113 void DecodeVPERMVMask(ArrayRef RawMask,
126 SmallVectorImpl &ShuffleMask);
127
128 /// \brief Decode a VPERMT2 W/D/Q/PS/PD mask from an IR-level vector constant.
129 void DecodeVPERMV3Mask(const Constant *C, MVT VT,
130114 SmallVectorImpl &ShuffleMask);
131115
132116 /// \brief Decode a VPERMT2 W/D/Q/PS/PD mask from a raw array of constants.
1717 #include "X86FrameLowering.h"
1818 #include "X86InstrBuilder.h"
1919 #include "X86MachineFunctionInfo.h"
20 #include "X86ShuffleDecodeConstantPool.h"
2021 #include "X86TargetMachine.h"
2122 #include "X86TargetObjectFile.h"
2223 #include "llvm/ADT/SmallBitVector.h"
1313
1414 #include "X86AsmPrinter.h"
1515 #include "X86RegisterInfo.h"
16 #include "X86ShuffleDecodeConstantPool.h"
1617 #include "InstPrinter/X86ATTInstPrinter.h"
1718 #include "MCTargetDesc/X86BaseInfo.h"
1819 #include "Utils/X86ShuffleDecode.h"
0 //===-- X86ShuffleDecodeConstantPool.cpp - X86 shuffle decode -------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Define several functions to decode x86 specific shuffle semantics using
10 // constants from the constant pool.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86ShuffleDecodeConstantPool.h"
15 #include "Utils/X86ShuffleDecode.h"
16 #include "llvm/CodeGen/MachineValueType.h"
17 #include "llvm/IR/Constants.h"
18
19 //===----------------------------------------------------------------------===//
20 // Vector Mask Decoding
21 //===----------------------------------------------------------------------===//
22
23 namespace llvm {
24
25 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl &ShuffleMask) {
26 Type *MaskTy = C->getType();
27 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
28 // constant pool uniques constants by their bit representation.
29 // e.g. the following take up the same space in the constant pool:
30 // i128 -170141183420855150465331762880109871104
31 //
32 // <2 x i64>
33 //
34 // <4 x i32>
35 // i32 -2147483648, i32 -2147483648>
36
37 #ifndef NDEBUG
38 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
39 assert(MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512);
40 #endif
41
42 // This is a straightforward byte vector.
43 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) {
44 int NumElements = MaskTy->getVectorNumElements();
45 ShuffleMask.reserve(NumElements);
46
47 for (int i = 0; i < NumElements; ++i) {
48 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
49 // lane of the vector we're inside.
50 int Base = i & ~0xf;
51 Constant *COp = C->getAggregateElement(i);
52 if (!COp) {
53 ShuffleMask.clear();
54 return;
55 } else if (isa(COp)) {
56 ShuffleMask.push_back(SM_SentinelUndef);
57 continue;
58 }
59 uint64_t Element = cast(COp)->getZExtValue();
60 // If the high bit (7) of the byte is set, the element is zeroed.
61 if (Element & (1 << 7))
62 ShuffleMask.push_back(SM_SentinelZero);
63 else {
64 // Only the least significant 4 bits of the byte are used.
65 int Index = Base + (Element & 0xf);
66 ShuffleMask.push_back(Index);
67 }
68 }
69 }
70 // TODO: Handle funny-looking vectors too.
71 }
72
73 void DecodeVPERMILPMask(const Constant *C, unsigned ElSize,
74 SmallVectorImpl &ShuffleMask) {
75 Type *MaskTy = C->getType();
76 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
77 // constant pool uniques constants by their bit representation.
78 // e.g. the following take up the same space in the constant pool:
79 // i128 -170141183420855150465331762880109871104
80 //
81 // <2 x i64>
82 //
83 // <4 x i32>
84 // i32 -2147483648, i32 -2147483648>
85
86 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
87
88 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512.
89 return;
90
91 // Only support vector types.
92 if (!MaskTy->isVectorTy())
93 return;
94
95 // Make sure its an integer type.
96 Type *VecEltTy = MaskTy->getVectorElementType();
97 if (!VecEltTy->isIntegerTy())
98 return;
99
100 // Support any element type from byte up to element size.
101 // This is necesary primarily because 64-bit elements get split to 32-bit
102 // in the constant pool on 32-bit target.
103 unsigned EltTySize = VecEltTy->getIntegerBitWidth();
104 if (EltTySize < 8 || EltTySize > ElSize)
105 return;
106
107 unsigned NumElements = MaskTySize / ElSize;
108 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
109 "Unexpected number of vector elements.");
110 ShuffleMask.reserve(NumElements);
111 unsigned NumElementsPerLane = 128 / ElSize;
112 unsigned Factor = ElSize / EltTySize;
113
114 for (unsigned i = 0; i < NumElements; ++i) {
115 Constant *COp = C->getAggregateElement(i * Factor);
116 if (!COp) {
117 ShuffleMask.clear();
118 return;
119 } else if (isa(COp)) {
120 ShuffleMask.push_back(SM_SentinelUndef);
121 continue;
122 }
123 int Index = i & ~(NumElementsPerLane - 1);
124 uint64_t Element = cast(COp)->getZExtValue();
125 if (ElSize == 64)
126 Index += (Element >> 1) & 0x1;
127 else
128 Index += Element & 0x3;
129 ShuffleMask.push_back(Index);
130 }
131
132 // TODO: Handle funny-looking vectors too.
133 }
134
135 void DecodeVPERMVMask(const Constant *C, MVT VT,
136 SmallVectorImpl &ShuffleMask) {
137 Type *MaskTy = C->getType();
138 if (MaskTy->isVectorTy()) {
139 unsigned NumElements = MaskTy->getVectorNumElements();
140 if (NumElements == VT.getVectorNumElements()) {
141 for (unsigned i = 0; i < NumElements; ++i) {
142 Constant *COp = C->getAggregateElement(i);
143 if (!COp || (!isa(COp) && !isa(COp))) {
144 ShuffleMask.clear();
145 return;
146 }
147 if (isa(COp))
148 ShuffleMask.push_back(SM_SentinelUndef);
149 else {
150 uint64_t Element = cast(COp)->getZExtValue();
151 Element &= (1 << NumElements) - 1;
152 ShuffleMask.push_back(Element);
153 }
154 }
155 }
156 return;
157 }
158 // Scalar value; just broadcast it
159 if (!isa(C))
160 return;
161 uint64_t Element = cast(C)->getZExtValue();
162 int NumElements = VT.getVectorNumElements();
163 Element &= (1 << NumElements) - 1;
164 for (int i = 0; i < NumElements; ++i)
165 ShuffleMask.push_back(Element);
166 }
167
168 void DecodeVPERMV3Mask(const Constant *C, MVT VT,
169 SmallVectorImpl &ShuffleMask) {
170 Type *MaskTy = C->getType();
171 unsigned NumElements = MaskTy->getVectorNumElements();
172 if (NumElements == VT.getVectorNumElements()) {
173 for (unsigned i = 0; i < NumElements; ++i) {
174 Constant *COp = C->getAggregateElement(i);
175 if (!COp) {
176 ShuffleMask.clear();
177 return;
178 }
179 if (isa(COp))
180 ShuffleMask.push_back(SM_SentinelUndef);
181 else {
182 uint64_t Element = cast(COp)->getZExtValue();
183 Element &= (1 << NumElements*2) - 1;
184 ShuffleMask.push_back(Element);
185 }
186 }
187 }
188 }
189 } // llvm namespace
0 //===-- X86ShuffleDecodeConstantPool.h - X86 shuffle decode -----*-C++-*---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Define several functions to decode x86 specific shuffle semantics using
10 // constants from the constant pool.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_TARGET_X86_X86SHUFFLEDECODECONSTANTPOOL_H
15 #define LLVM_LIB_TARGET_X86_X86SHUFFLEDECODECONSTANTPOOL_H
16
17 #include "llvm/ADT/SmallVector.h"
18
19 //===----------------------------------------------------------------------===//
20 // Vector Mask Decoding
21 //===----------------------------------------------------------------------===//
22
23 namespace llvm {
24 class Constant;
25 class MVT;
26
27 /// \brief Decode a PSHUFB mask from an IR-level vector constant.
28 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl &ShuffleMask);
29
30 /// \brief Decode a VPERMILP variable mask from an IR-level vector constant.
31 void DecodeVPERMILPMask(const Constant *C, unsigned ElSize,
32 SmallVectorImpl &ShuffleMask);
33
34 /// \brief Decode a VPERM W/D/Q/PS/PD mask from an IR-level vector constant.
35 void DecodeVPERMVMask(const Constant *C, MVT VT,
36 SmallVectorImpl &ShuffleMask);
37
38 /// \brief Decode a VPERMT2 W/D/Q/PS/PD mask from an IR-level vector constant.
39 void DecodeVPERMV3Mask(const Constant *C, MVT VT,
40 SmallVectorImpl &ShuffleMask);
41
42 } // llvm namespace
43
44 #endif