1 //===-- X86ShuffleDecodeConstantPool.cpp - X86 shuffle decode -------------===// 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 // Define several functions to decode x86 specific shuffle semantics using 11 // constants from the constant pool. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "X86ShuffleDecodeConstantPool.h" 16 #include "Utils/X86ShuffleDecode.h" 17 #include "llvm/CodeGen/MachineValueType.h" 18 #include "llvm/IR/Constants.h" 19 20 //===----------------------------------------------------------------------===// 21 // Vector Mask Decoding 22 //===----------------------------------------------------------------------===// 23 24 namespace llvm { 25 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 32 // 33 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160> 34 // 35 // <4 x i32> <i32 -2147483648, i32 -2147483648, 36 // i32 -2147483648, i32 -2147483648> 37 38 #ifndef NDEBUG 39 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits(); 40 assert(MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512); 41 #endif 42 43 if (!MaskTy->isVectorTy()) 44 return; 45 int NumElts = MaskTy->getVectorNumElements(); 46 47 Type *EltTy = MaskTy->getVectorElementType(); 48 if (!EltTy->isIntegerTy()) 49 return; 50 51 // The shuffle mask requires a byte vector - decode cases with 52 // wider elements as well. 53 unsigned BitWidth = cast<IntegerType>(EltTy)->getBitWidth(); 54 if ((BitWidth % 8) != 0) 55 return; 56 57 int Scale = BitWidth / 8; 58 int NumBytes = NumElts * Scale; 59 ShuffleMask.reserve(NumBytes); 60 61 for (int i = 0; i != NumElts; ++i) { 62 Constant *COp = C->getAggregateElement(i); 63 if (!COp) { 64 ShuffleMask.clear(); 65 return; 66 } else if (isa<UndefValue>(COp)) { 67 ShuffleMask.append(Scale, SM_SentinelUndef); 68 continue; 69 } 70 71 APInt APElt = cast<ConstantInt>(COp)->getValue(); 72 for (int j = 0; j != Scale; ++j) { 73 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte 74 // lane of the vector we're inside. 75 int Base = ((i * Scale) + j) & ~0xf; 76 77 uint64_t Element = APElt.getLoBits(8).getZExtValue(); 78 APElt = APElt.lshr(8); 79 80 // If the high bit (7) of the byte is set, the element is zeroed. 81 if (Element & (1 << 7)) 82 ShuffleMask.push_back(SM_SentinelZero); 83 else { 84 // Only the least significant 4 bits of the byte are used. 85 int Index = Base + (Element & 0xf); 86 ShuffleMask.push_back(Index); 87 } 88 } 89 } 90 91 assert(NumBytes == (int)ShuffleMask.size() && "Unexpected shuffle mask size"); 92 } 93 94 void DecodeVPERMILPMask(const Constant *C, unsigned ElSize, 95 SmallVectorImpl<int> &ShuffleMask) { 96 Type *MaskTy = C->getType(); 97 // It is not an error for the PSHUFB mask to not be a vector of i8 because the 98 // constant pool uniques constants by their bit representation. 99 // e.g. the following take up the same space in the constant pool: 100 // i128 -170141183420855150465331762880109871104 101 // 102 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160> 103 // 104 // <4 x i32> <i32 -2147483648, i32 -2147483648, 105 // i32 -2147483648, i32 -2147483648> 106 107 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits(); 108 109 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512. 110 return; 111 112 // Only support vector types. 113 if (!MaskTy->isVectorTy()) 114 return; 115 116 // Make sure its an integer type. 117 Type *VecEltTy = MaskTy->getVectorElementType(); 118 if (!VecEltTy->isIntegerTy()) 119 return; 120 121 // Support any element type from byte up to element size. 122 // This is necessary primarily because 64-bit elements get split to 32-bit 123 // in the constant pool on 32-bit target. 124 unsigned EltTySize = VecEltTy->getIntegerBitWidth(); 125 if (EltTySize < 8 || EltTySize > ElSize) 126 return; 127 128 unsigned NumElements = MaskTySize / ElSize; 129 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) && 130 "Unexpected number of vector elements."); 131 ShuffleMask.reserve(NumElements); 132 unsigned NumElementsPerLane = 128 / ElSize; 133 unsigned Factor = ElSize / EltTySize; 134 135 for (unsigned i = 0; i < NumElements; ++i) { 136 Constant *COp = C->getAggregateElement(i * Factor); 137 if (!COp) { 138 ShuffleMask.clear(); 139 return; 140 } else if (isa<UndefValue>(COp)) { 141 ShuffleMask.push_back(SM_SentinelUndef); 142 continue; 143 } 144 int Index = i & ~(NumElementsPerLane - 1); 145 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 146 if (ElSize == 64) 147 Index += (Element >> 1) & 0x1; 148 else 149 Index += Element & 0x3; 150 ShuffleMask.push_back(Index); 151 } 152 153 // TODO: Handle funny-looking vectors too. 154 } 155 156 void DecodeVPPERMMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) { 157 Type *MaskTy = C->getType(); 158 assert(MaskTy->getPrimitiveSizeInBits() == 128); 159 160 // Only support vector types. 161 if (!MaskTy->isVectorTy()) 162 return; 163 164 // Make sure its an integer type. 165 Type *VecEltTy = MaskTy->getVectorElementType(); 166 if (!VecEltTy->isIntegerTy()) 167 return; 168 169 // The shuffle mask requires a byte vector - decode cases with 170 // wider elements as well. 171 unsigned BitWidth = cast<IntegerType>(VecEltTy)->getBitWidth(); 172 if ((BitWidth % 8) != 0) 173 return; 174 175 int NumElts = MaskTy->getVectorNumElements(); 176 int Scale = BitWidth / 8; 177 int NumBytes = NumElts * Scale; 178 ShuffleMask.reserve(NumBytes); 179 180 for (int i = 0; i != NumElts; ++i) { 181 Constant *COp = C->getAggregateElement(i); 182 if (!COp) { 183 ShuffleMask.clear(); 184 return; 185 } else if (isa<UndefValue>(COp)) { 186 ShuffleMask.append(Scale, SM_SentinelUndef); 187 continue; 188 } 189 190 // VPPERM Operation 191 // Bits[4:0] - Byte Index (0 - 31) 192 // Bits[7:5] - Permute Operation 193 // 194 // Permute Operation: 195 // 0 - Source byte (no logical operation). 196 // 1 - Invert source byte. 197 // 2 - Bit reverse of source byte. 198 // 3 - Bit reverse of inverted source byte. 199 // 4 - 00h (zero - fill). 200 // 5 - FFh (ones - fill). 201 // 6 - Most significant bit of source byte replicated in all bit positions. 202 // 7 - Invert most significant bit of source byte and replicate in all bit positions. 203 APInt MaskElt = cast<ConstantInt>(COp)->getValue(); 204 for (int j = 0; j != Scale; ++j) { 205 APInt Index = MaskElt.getLoBits(5); 206 APInt PermuteOp = MaskElt.lshr(5).getLoBits(3); 207 MaskElt = MaskElt.lshr(8); 208 209 if (PermuteOp == 4) { 210 ShuffleMask.push_back(SM_SentinelZero); 211 continue; 212 } 213 if (PermuteOp != 0) { 214 ShuffleMask.clear(); 215 return; 216 } 217 ShuffleMask.push_back((int)Index.getZExtValue()); 218 } 219 } 220 221 assert(NumBytes == (int)ShuffleMask.size() && "Unexpected shuffle mask size"); 222 } 223 224 void DecodeVPERMVMask(const Constant *C, MVT VT, 225 SmallVectorImpl<int> &ShuffleMask) { 226 Type *MaskTy = C->getType(); 227 if (MaskTy->isVectorTy()) { 228 unsigned NumElements = MaskTy->getVectorNumElements(); 229 if (NumElements == VT.getVectorNumElements()) { 230 for (unsigned i = 0; i < NumElements; ++i) { 231 Constant *COp = C->getAggregateElement(i); 232 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) { 233 ShuffleMask.clear(); 234 return; 235 } 236 if (isa<UndefValue>(COp)) 237 ShuffleMask.push_back(SM_SentinelUndef); 238 else { 239 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 240 Element &= (1 << NumElements) - 1; 241 ShuffleMask.push_back(Element); 242 } 243 } 244 } 245 return; 246 } 247 // Scalar value; just broadcast it 248 if (!isa<ConstantInt>(C)) 249 return; 250 uint64_t Element = cast<ConstantInt>(C)->getZExtValue(); 251 int NumElements = VT.getVectorNumElements(); 252 Element &= (1 << NumElements) - 1; 253 for (int i = 0; i < NumElements; ++i) 254 ShuffleMask.push_back(Element); 255 } 256 257 void DecodeVPERMV3Mask(const Constant *C, MVT VT, 258 SmallVectorImpl<int> &ShuffleMask) { 259 Type *MaskTy = C->getType(); 260 unsigned NumElements = MaskTy->getVectorNumElements(); 261 if (NumElements == VT.getVectorNumElements()) { 262 unsigned EltMaskSize = Log2_64(NumElements * 2); 263 for (unsigned i = 0; i < NumElements; ++i) { 264 Constant *COp = C->getAggregateElement(i); 265 if (!COp) { 266 ShuffleMask.clear(); 267 return; 268 } 269 if (isa<UndefValue>(COp)) 270 ShuffleMask.push_back(SM_SentinelUndef); 271 else { 272 APInt Element = cast<ConstantInt>(COp)->getValue(); 273 Element = Element.getLoBits(EltMaskSize); 274 ShuffleMask.push_back(Element.getZExtValue()); 275 } 276 } 277 } 278 } 279 } // llvm namespace 280