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 // This is a straightforward byte vector. 44 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) { 45 int NumElements = MaskTy->getVectorNumElements(); 46 ShuffleMask.reserve(NumElements); 47 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. 51 int Base = i & ~0xf; 52 Constant *COp = C->getAggregateElement(i); 53 if (!COp) { 54 ShuffleMask.clear(); 55 return; 56 } else if (isa<UndefValue>(COp)) { 57 ShuffleMask.push_back(SM_SentinelUndef); 58 continue; 59 } 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); 64 else { 65 // Only the least significant 4 bits of the byte are used. 66 int Index = Base + (Element & 0xf); 67 ShuffleMask.push_back(Index); 68 } 69 } 70 } 71 // TODO: Handle funny-looking vectors too. 72 } 73 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 81 // 82 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160> 83 // 84 // <4 x i32> <i32 -2147483648, i32 -2147483648, 85 // i32 -2147483648, i32 -2147483648> 86 87 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits(); 88 89 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512. 90 return; 91 92 // Only support vector types. 93 if (!MaskTy->isVectorTy()) 94 return; 95 96 // Make sure its an integer type. 97 Type *VecEltTy = MaskTy->getVectorElementType(); 98 if (!VecEltTy->isIntegerTy()) 99 return; 100 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) 106 return; 107 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; 114 115 for (unsigned i = 0; i < NumElements; ++i) { 116 Constant *COp = C->getAggregateElement(i * Factor); 117 if (!COp) { 118 ShuffleMask.clear(); 119 return; 120 } else if (isa<UndefValue>(COp)) { 121 ShuffleMask.push_back(SM_SentinelUndef); 122 continue; 123 } 124 int Index = i & ~(NumElementsPerLane - 1); 125 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 126 if (ElSize == 64) 127 Index += (Element >> 1) & 0x1; 128 else 129 Index += Element & 0x3; 130 ShuffleMask.push_back(Index); 131 } 132 133 // TODO: Handle funny-looking vectors too. 134 } 135 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))) { 145 ShuffleMask.clear(); 146 return; 147 } 148 if (isa<UndefValue>(COp)) 149 ShuffleMask.push_back(SM_SentinelUndef); 150 else { 151 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 152 Element &= (1 << NumElements) - 1; 153 ShuffleMask.push_back(Element); 154 } 155 } 156 } 157 return; 158 } 159 // Scalar value; just broadcast it 160 if (!isa<ConstantInt>(C)) 161 return; 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); 167 } 168 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); 176 if (!COp) { 177 ShuffleMask.clear(); 178 return; 179 } 180 if (isa<UndefValue>(COp)) 181 ShuffleMask.push_back(SM_SentinelUndef); 182 else { 183 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 184 Element &= (1 << NumElements*2) - 1; 185 ShuffleMask.push_back(Element); 186 } 187 } 188 } 189 } 190 } // llvm namespace 191