1 //===-- X86ShuffleDecodeConstantPool.cpp - X86 shuffle decode -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Define several functions to decode x86 specific shuffle semantics using
11 // constants from the constant pool.
13 //===----------------------------------------------------------------------===//
15 #include "X86ShuffleDecodeConstantPool.h"
16 #include "Utils/X86ShuffleDecode.h"
17 #include "llvm/CodeGen/MachineValueType.h"
18 #include "llvm/IR/Constants.h"
20 //===----------------------------------------------------------------------===//
21 // Vector Mask Decoding
22 //===----------------------------------------------------------------------===//
26 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
27 Type *MaskTy = C->getType();
28 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
29 // constant pool uniques constants by their bit representation.
30 // e.g. the following take up the same space in the constant pool:
31 // i128 -170141183420855150465331762880109871104
33 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
35 // <4 x i32> <i32 -2147483648, i32 -2147483648,
36 // i32 -2147483648, i32 -2147483648>
39 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
40 assert(MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512);
43 // This is a straightforward byte vector.
44 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) {
45 int NumElements = MaskTy->getVectorNumElements();
46 ShuffleMask.reserve(NumElements);
48 for (int i = 0; i < NumElements; ++i) {
49 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
50 // lane of the vector we're inside.
52 Constant *COp = C->getAggregateElement(i);
56 } else if (isa<UndefValue>(COp)) {
57 ShuffleMask.push_back(SM_SentinelUndef);
60 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
61 // If the high bit (7) of the byte is set, the element is zeroed.
62 if (Element & (1 << 7))
63 ShuffleMask.push_back(SM_SentinelZero);
65 // Only the least significant 4 bits of the byte are used.
66 int Index = Base + (Element & 0xf);
67 ShuffleMask.push_back(Index);
71 // TODO: Handle funny-looking vectors too.
74 void DecodeVPERMILPMask(const Constant *C, unsigned ElSize,
75 SmallVectorImpl<int> &ShuffleMask) {
76 Type *MaskTy = C->getType();
77 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
78 // constant pool uniques constants by their bit representation.
79 // e.g. the following take up the same space in the constant pool:
80 // i128 -170141183420855150465331762880109871104
82 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
84 // <4 x i32> <i32 -2147483648, i32 -2147483648,
85 // i32 -2147483648, i32 -2147483648>
87 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
89 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512.
92 // Only support vector types.
93 if (!MaskTy->isVectorTy())
96 // Make sure its an integer type.
97 Type *VecEltTy = MaskTy->getVectorElementType();
98 if (!VecEltTy->isIntegerTy())
101 // Support any element type from byte up to element size.
102 // This is necesary primarily because 64-bit elements get split to 32-bit
103 // in the constant pool on 32-bit target.
104 unsigned EltTySize = VecEltTy->getIntegerBitWidth();
105 if (EltTySize < 8 || EltTySize > ElSize)
108 unsigned NumElements = MaskTySize / ElSize;
109 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
110 "Unexpected number of vector elements.");
111 ShuffleMask.reserve(NumElements);
112 unsigned NumElementsPerLane = 128 / ElSize;
113 unsigned Factor = ElSize / EltTySize;
115 for (unsigned i = 0; i < NumElements; ++i) {
116 Constant *COp = C->getAggregateElement(i * Factor);
120 } else if (isa<UndefValue>(COp)) {
121 ShuffleMask.push_back(SM_SentinelUndef);
124 int Index = i & ~(NumElementsPerLane - 1);
125 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
127 Index += (Element >> 1) & 0x1;
129 Index += Element & 0x3;
130 ShuffleMask.push_back(Index);
133 // TODO: Handle funny-looking vectors too.
136 void DecodeVPERMVMask(const Constant *C, MVT VT,
137 SmallVectorImpl<int> &ShuffleMask) {
138 Type *MaskTy = C->getType();
139 if (MaskTy->isVectorTy()) {
140 unsigned NumElements = MaskTy->getVectorNumElements();
141 if (NumElements == VT.getVectorNumElements()) {
142 for (unsigned i = 0; i < NumElements; ++i) {
143 Constant *COp = C->getAggregateElement(i);
144 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) {
148 if (isa<UndefValue>(COp))
149 ShuffleMask.push_back(SM_SentinelUndef);
151 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
152 Element &= (1 << NumElements) - 1;
153 ShuffleMask.push_back(Element);
159 // Scalar value; just broadcast it
160 if (!isa<ConstantInt>(C))
162 uint64_t Element = cast<ConstantInt>(C)->getZExtValue();
163 int NumElements = VT.getVectorNumElements();
164 Element &= (1 << NumElements) - 1;
165 for (int i = 0; i < NumElements; ++i)
166 ShuffleMask.push_back(Element);
169 void DecodeVPERMV3Mask(const Constant *C, MVT VT,
170 SmallVectorImpl<int> &ShuffleMask) {
171 Type *MaskTy = C->getType();
172 unsigned NumElements = MaskTy->getVectorNumElements();
173 if (NumElements == VT.getVectorNumElements()) {
174 for (unsigned i = 0; i < NumElements; ++i) {
175 Constant *COp = C->getAggregateElement(i);
180 if (isa<UndefValue>(COp))
181 ShuffleMask.push_back(SM_SentinelUndef);
183 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
184 Element &= (1 << NumElements*2) - 1;
185 ShuffleMask.push_back(Element);