Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 1 | //===-- X86ShuffleDecodeConstantPool.cpp - X86 shuffle decode -------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Define several functions to decode x86 specific shuffle semantics using |
| 10 | // constants from the constant pool. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 14 | #include "Utils/X86ShuffleDecode.h" |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
| 17 | |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | // Vector Mask Decoding |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 24 | static bool extractConstantMask(const Constant *C, unsigned MaskEltSizeInBits, |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 25 | APInt &UndefElts, |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 26 | SmallVectorImpl<uint64_t> &RawMask) { |
| 27 | // It is not an error for shuffle masks to not be a vector of |
| 28 | // MaskEltSizeInBits because the constant pool uniques constants by their |
| 29 | // bit representation. |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 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> |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 37 | Type *CstTy = C->getType(); |
| 38 | if (!CstTy->isVectorTy()) |
| 39 | return false; |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 40 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 41 | Type *CstEltTy = CstTy->getVectorElementType(); |
| 42 | if (!CstEltTy->isIntegerTy()) |
| 43 | return false; |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 44 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 45 | unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits(); |
| 46 | unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits(); |
| 47 | unsigned NumCstElts = CstTy->getVectorNumElements(); |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 48 | |
Simon Pilgrim | e86b7e2 | 2017-03-09 14:06:39 +0000 | [diff] [blame] | 49 | assert((CstSizeInBits % MaskEltSizeInBits) == 0 && |
| 50 | "Unaligned shuffle mask size"); |
| 51 | |
| 52 | unsigned NumMaskElts = CstSizeInBits / MaskEltSizeInBits; |
| 53 | UndefElts = APInt(NumMaskElts, 0); |
| 54 | RawMask.resize(NumMaskElts, 0); |
| 55 | |
| 56 | // Fast path - if the constants match the mask size then copy direct. |
| 57 | if (MaskEltSizeInBits == CstEltSizeInBits) { |
| 58 | assert(NumCstElts == NumMaskElts && "Unaligned shuffle mask size"); |
| 59 | for (unsigned i = 0; i != NumMaskElts; ++i) { |
| 60 | Constant *COp = C->getAggregateElement(i); |
| 61 | if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) |
| 62 | return false; |
| 63 | |
| 64 | if (isa<UndefValue>(COp)) { |
| 65 | UndefElts.setBit(i); |
| 66 | RawMask[i] = 0; |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | auto *Elt = cast<ConstantInt>(COp); |
| 71 | RawMask[i] = Elt->getValue().getZExtValue(); |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 76 | // Extract all the undef/constant element data and pack into single bitsets. |
| 77 | APInt UndefBits(CstSizeInBits, 0); |
| 78 | APInt MaskBits(CstSizeInBits, 0); |
| 79 | for (unsigned i = 0; i != NumCstElts; ++i) { |
Simon Pilgrim | 05e48b9 | 2016-02-18 10:17:40 +0000 | [diff] [blame] | 80 | Constant *COp = C->getAggregateElement(i); |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 81 | if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) |
| 82 | return false; |
| 83 | |
Simon Pilgrim | 3a895c4 | 2017-02-22 15:04:55 +0000 | [diff] [blame] | 84 | unsigned BitOffset = i * CstEltSizeInBits; |
| 85 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 86 | if (isa<UndefValue>(COp)) { |
Simon Pilgrim | aed3522 | 2017-02-24 10:15:29 +0000 | [diff] [blame] | 87 | UndefBits.setBits(BitOffset, BitOffset + CstEltSizeInBits); |
Simon Pilgrim | 05e48b9 | 2016-02-18 10:17:40 +0000 | [diff] [blame] | 88 | continue; |
| 89 | } |
| 90 | |
Simon Pilgrim | b02667c | 2017-03-10 13:44:32 +0000 | [diff] [blame] | 91 | MaskBits.insertBits(cast<ConstantInt>(COp)->getValue(), BitOffset); |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 92 | } |
Simon Pilgrim | 05e48b9 | 2016-02-18 10:17:40 +0000 | [diff] [blame] | 93 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 94 | // Now extract the undef/constant bit data into the raw shuffle masks. |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 95 | for (unsigned i = 0; i != NumMaskElts; ++i) { |
Simon Pilgrim | 0f5fb5f | 2017-02-25 20:01:58 +0000 | [diff] [blame] | 96 | unsigned BitOffset = i * MaskEltSizeInBits; |
| 97 | APInt EltUndef = UndefBits.extractBits(MaskEltSizeInBits, BitOffset); |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 98 | |
| 99 | // Only treat the element as UNDEF if all bits are UNDEF, otherwise |
| 100 | // treat it as zero. |
Simon Pilgrim | 96ef0c1 | 2016-10-23 15:09:44 +0000 | [diff] [blame] | 101 | if (EltUndef.isAllOnesValue()) { |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 102 | UndefElts.setBit(i); |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 103 | RawMask[i] = 0; |
| 104 | continue; |
| 105 | } |
| 106 | |
Simon Pilgrim | 0f5fb5f | 2017-02-25 20:01:58 +0000 | [diff] [blame] | 107 | APInt EltBits = MaskBits.extractBits(MaskEltSizeInBits, BitOffset); |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 108 | RawMask[i] = EltBits.getZExtValue(); |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 114 | void DecodePSHUFBMask(const Constant *C, unsigned Width, |
| 115 | SmallVectorImpl<int> &ShuffleMask) { |
| 116 | assert((Width == 128 || Width == 256 || Width == 512) && |
| 117 | C->getType()->getPrimitiveSizeInBits() >= Width && |
Simon Pilgrim | 630dd6f | 2016-10-01 20:12:56 +0000 | [diff] [blame] | 118 | "Unexpected vector size."); |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 119 | |
| 120 | // The shuffle mask requires a byte vector. |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 121 | APInt UndefElts; |
Craig Topper | e1be95c | 2017-02-27 16:15:27 +0000 | [diff] [blame] | 122 | SmallVector<uint64_t, 64> RawMask; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 123 | if (!extractConstantMask(C, 8, UndefElts, RawMask)) |
| 124 | return; |
| 125 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 126 | unsigned NumElts = Width / 8; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 127 | assert((NumElts == 16 || NumElts == 32 || NumElts == 64) && |
| 128 | "Unexpected number of vector elements."); |
| 129 | |
| 130 | for (unsigned i = 0; i != NumElts; ++i) { |
| 131 | if (UndefElts[i]) { |
| 132 | ShuffleMask.push_back(SM_SentinelUndef); |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | uint64_t Element = RawMask[i]; |
| 137 | // If the high bit (7) of the byte is set, the element is zeroed. |
| 138 | if (Element & (1 << 7)) |
| 139 | ShuffleMask.push_back(SM_SentinelZero); |
| 140 | else { |
| 141 | // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte |
| 142 | // lane of the vector we're inside. |
| 143 | unsigned Base = i & ~0xf; |
| 144 | |
| 145 | // Only the least significant 4 bits of the byte are used. |
| 146 | int Index = Base + (Element & 0xf); |
| 147 | ShuffleMask.push_back(Index); |
| 148 | } |
| 149 | } |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 152 | void DecodeVPERMILPMask(const Constant *C, unsigned ElSize, unsigned Width, |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 153 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 154 | assert((Width == 128 || Width == 256 || Width == 512) && |
| 155 | C->getType()->getPrimitiveSizeInBits() >= Width && |
Simon Pilgrim | 630dd6f | 2016-10-01 20:12:56 +0000 | [diff] [blame] | 156 | "Unexpected vector size."); |
| 157 | assert((ElSize == 32 || ElSize == 64) && "Unexpected vector element size."); |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 158 | |
| 159 | // The shuffle mask requires elements the same size as the target. |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 160 | APInt UndefElts; |
Craig Topper | e1be95c | 2017-02-27 16:15:27 +0000 | [diff] [blame] | 161 | SmallVector<uint64_t, 16> RawMask; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 162 | if (!extractConstantMask(C, ElSize, UndefElts, RawMask)) |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 163 | return; |
| 164 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 165 | unsigned NumElts = Width / ElSize; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 166 | unsigned NumEltsPerLane = 128 / ElSize; |
| 167 | assert((NumElts == 2 || NumElts == 4 || NumElts == 8 || NumElts == 16) && |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 168 | "Unexpected number of vector elements."); |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 169 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 170 | for (unsigned i = 0; i != NumElts; ++i) { |
| 171 | if (UndefElts[i]) { |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 172 | ShuffleMask.push_back(SM_SentinelUndef); |
| 173 | continue; |
| 174 | } |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 175 | |
| 176 | int Index = i & ~(NumEltsPerLane - 1); |
| 177 | uint64_t Element = RawMask[i]; |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 178 | if (ElSize == 64) |
| 179 | Index += (Element >> 1) & 0x1; |
| 180 | else |
| 181 | Index += Element & 0x3; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 182 | |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 183 | ShuffleMask.push_back(Index); |
| 184 | } |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 187 | void DecodeVPERMIL2PMask(const Constant *C, unsigned M2Z, unsigned ElSize, |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 188 | unsigned Width, |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 189 | SmallVectorImpl<int> &ShuffleMask) { |
| 190 | Type *MaskTy = C->getType(); |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 191 | unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits(); |
Douglas Katzman | 3ace13a | 2016-09-29 17:26:12 +0000 | [diff] [blame] | 192 | (void)MaskTySize; |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 193 | assert((MaskTySize == 128 || MaskTySize == 256) && |
| 194 | Width >= MaskTySize && "Unexpected vector size."); |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 195 | |
| 196 | // The shuffle mask requires elements the same size as the target. |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 197 | APInt UndefElts; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 198 | SmallVector<uint64_t, 8> RawMask; |
| 199 | if (!extractConstantMask(C, ElSize, UndefElts, RawMask)) |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 200 | return; |
| 201 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 202 | unsigned NumElts = Width / ElSize; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 203 | unsigned NumEltsPerLane = 128 / ElSize; |
| 204 | assert((NumElts == 2 || NumElts == 4 || NumElts == 8) && |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 205 | "Unexpected number of vector elements."); |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 206 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 207 | for (unsigned i = 0; i != NumElts; ++i) { |
| 208 | if (UndefElts[i]) { |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 209 | ShuffleMask.push_back(SM_SentinelUndef); |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | // VPERMIL2 Operation. |
| 214 | // Bits[3] - Match Bit. |
| 215 | // Bits[2:1] - (Per Lane) PD Shuffle Mask. |
| 216 | // Bits[2:0] - (Per Lane) PS Shuffle Mask. |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 217 | uint64_t Selector = RawMask[i]; |
Chandler Carruth | 4c0e94d | 2016-06-11 09:13:00 +0000 | [diff] [blame] | 218 | unsigned MatchBit = (Selector >> 3) & 0x1; |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 219 | |
| 220 | // M2Z[0:1] MatchBit |
| 221 | // 0Xb X Source selected by Selector index. |
| 222 | // 10b 0 Source selected by Selector index. |
| 223 | // 10b 1 Zero. |
| 224 | // 11b 0 Zero. |
| 225 | // 11b 1 Source selected by Selector index. |
Chandler Carruth | 306e270 | 2016-06-11 08:02:01 +0000 | [diff] [blame] | 226 | if ((M2Z & 0x2) != 0u && MatchBit != (M2Z & 0x1)) { |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 227 | ShuffleMask.push_back(SM_SentinelZero); |
| 228 | continue; |
| 229 | } |
| 230 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 231 | int Index = i & ~(NumEltsPerLane - 1); |
Simon Pilgrim | 163987a | 2016-06-05 14:33:43 +0000 | [diff] [blame] | 232 | if (ElSize == 64) |
| 233 | Index += (Selector >> 1) & 0x1; |
| 234 | else |
| 235 | Index += Selector & 0x3; |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 236 | |
| 237 | int Src = (Selector >> 2) & 0x1; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 238 | Index += Src * NumElts; |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 239 | ShuffleMask.push_back(Index); |
| 240 | } |
Simon Pilgrim | 2ead861 | 2016-06-04 21:44:28 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 243 | void DecodeVPPERMMask(const Constant *C, unsigned Width, |
| 244 | SmallVectorImpl<int> &ShuffleMask) { |
| 245 | Type *MaskTy = C->getType(); |
| 246 | unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits(); |
| 247 | (void)MaskTySize; |
| 248 | assert(Width == 128 && Width >= MaskTySize && "Unexpected vector size."); |
Simon Pilgrim | 1cc5712 | 2016-04-09 14:51:26 +0000 | [diff] [blame] | 249 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 250 | // The shuffle mask requires a byte vector. |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 251 | APInt UndefElts; |
Craig Topper | e1be95c | 2017-02-27 16:15:27 +0000 | [diff] [blame] | 252 | SmallVector<uint64_t, 16> RawMask; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 253 | if (!extractConstantMask(C, 8, UndefElts, RawMask)) |
Simon Pilgrim | 1cc5712 | 2016-04-09 14:51:26 +0000 | [diff] [blame] | 254 | return; |
| 255 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 256 | unsigned NumElts = Width / 8; |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 257 | assert(NumElts == 16 && "Unexpected number of vector elements."); |
Simon Pilgrim | 1cc5712 | 2016-04-09 14:51:26 +0000 | [diff] [blame] | 258 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 259 | for (unsigned i = 0; i != NumElts; ++i) { |
| 260 | if (UndefElts[i]) { |
| 261 | ShuffleMask.push_back(SM_SentinelUndef); |
Simon Pilgrim | 1cc5712 | 2016-04-09 14:51:26 +0000 | [diff] [blame] | 262 | continue; |
| 263 | } |
| 264 | |
| 265 | // VPPERM Operation |
| 266 | // Bits[4:0] - Byte Index (0 - 31) |
| 267 | // Bits[7:5] - Permute Operation |
| 268 | // |
| 269 | // Permute Operation: |
| 270 | // 0 - Source byte (no logical operation). |
| 271 | // 1 - Invert source byte. |
| 272 | // 2 - Bit reverse of source byte. |
| 273 | // 3 - Bit reverse of inverted source byte. |
| 274 | // 4 - 00h (zero - fill). |
| 275 | // 5 - FFh (ones - fill). |
| 276 | // 6 - Most significant bit of source byte replicated in all bit positions. |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 277 | // 7 - Invert most significant bit of source byte and replicate in all bit |
| 278 | // positions. |
| 279 | uint64_t Element = RawMask[i]; |
| 280 | uint64_t Index = Element & 0x1F; |
| 281 | uint64_t PermuteOp = (Element >> 5) & 0x7; |
Simon Pilgrim | 1cc5712 | 2016-04-09 14:51:26 +0000 | [diff] [blame] | 282 | |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 283 | if (PermuteOp == 4) { |
| 284 | ShuffleMask.push_back(SM_SentinelZero); |
| 285 | continue; |
Simon Pilgrim | 1cc5712 | 2016-04-09 14:51:26 +0000 | [diff] [blame] | 286 | } |
Simon Pilgrim | 97a4820 | 2016-09-29 15:25:48 +0000 | [diff] [blame] | 287 | if (PermuteOp != 0) { |
| 288 | ShuffleMask.clear(); |
| 289 | return; |
| 290 | } |
| 291 | ShuffleMask.push_back((int)Index); |
Simon Pilgrim | 1cc5712 | 2016-04-09 14:51:26 +0000 | [diff] [blame] | 292 | } |
Simon Pilgrim | 1cc5712 | 2016-04-09 14:51:26 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 295 | void DecodeVPERMVMask(const Constant *C, unsigned ElSize, unsigned Width, |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 296 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 297 | assert((Width == 128 || Width == 256 || Width == 512) && |
| 298 | C->getType()->getPrimitiveSizeInBits() >= Width && |
Craig Topper | 448358b | 2016-10-18 04:48:33 +0000 | [diff] [blame] | 299 | "Unexpected vector size."); |
| 300 | assert((ElSize == 8 || ElSize == 16 || ElSize == 32 || ElSize == 64) && |
| 301 | "Unexpected vector element size."); |
| 302 | |
| 303 | // The shuffle mask requires elements the same size as the target. |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 304 | APInt UndefElts; |
Craig Topper | e1be95c | 2017-02-27 16:15:27 +0000 | [diff] [blame] | 305 | SmallVector<uint64_t, 64> RawMask; |
Craig Topper | 448358b | 2016-10-18 04:48:33 +0000 | [diff] [blame] | 306 | if (!extractConstantMask(C, ElSize, UndefElts, RawMask)) |
| 307 | return; |
| 308 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 309 | unsigned NumElts = Width / ElSize; |
Craig Topper | 448358b | 2016-10-18 04:48:33 +0000 | [diff] [blame] | 310 | |
| 311 | for (unsigned i = 0; i != NumElts; ++i) { |
| 312 | if (UndefElts[i]) { |
| 313 | ShuffleMask.push_back(SM_SentinelUndef); |
| 314 | continue; |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 315 | } |
Craig Topper | 448358b | 2016-10-18 04:48:33 +0000 | [diff] [blame] | 316 | int Index = RawMask[i] & (NumElts - 1); |
| 317 | ShuffleMask.push_back(Index); |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 318 | } |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 321 | void DecodeVPERMV3Mask(const Constant *C, unsigned ElSize, unsigned Width, |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 322 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 323 | assert((Width == 128 || Width == 256 || Width == 512) && |
| 324 | C->getType()->getPrimitiveSizeInBits() >= Width && |
Craig Topper | 7268bf9 | 2016-10-18 04:00:32 +0000 | [diff] [blame] | 325 | "Unexpected vector size."); |
| 326 | assert((ElSize == 8 || ElSize == 16 || ElSize == 32 || ElSize == 64) && |
| 327 | "Unexpected vector element size."); |
| 328 | |
| 329 | // The shuffle mask requires elements the same size as the target. |
Craig Topper | 53e5a38d | 2017-02-27 16:15:25 +0000 | [diff] [blame] | 330 | APInt UndefElts; |
Craig Topper | e1be95c | 2017-02-27 16:15:27 +0000 | [diff] [blame] | 331 | SmallVector<uint64_t, 64> RawMask; |
Craig Topper | 7268bf9 | 2016-10-18 04:00:32 +0000 | [diff] [blame] | 332 | if (!extractConstantMask(C, ElSize, UndefElts, RawMask)) |
| 333 | return; |
| 334 | |
Craig Topper | c8e183f | 2018-10-22 22:14:05 +0000 | [diff] [blame] | 335 | unsigned NumElts = Width / ElSize; |
Craig Topper | 7268bf9 | 2016-10-18 04:00:32 +0000 | [diff] [blame] | 336 | |
| 337 | for (unsigned i = 0; i != NumElts; ++i) { |
| 338 | if (UndefElts[i]) { |
| 339 | ShuffleMask.push_back(SM_SentinelUndef); |
| 340 | continue; |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 341 | } |
Craig Topper | 7268bf9 | 2016-10-18 04:00:32 +0000 | [diff] [blame] | 342 | int Index = RawMask[i] & (NumElts*2 - 1); |
| 343 | ShuffleMask.push_back(Index); |
Craig Topper | 69653af | 2015-12-31 22:40:45 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } // llvm namespace |