X86ShuffleDecodeConstantPool.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //===-- X86ShuffleDecodeConstantPool.cpp - X86 shuffle decode -------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Define several functions to decode x86 specific shuffle semantics using
  10. // constants from the constant pool.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "X86ShuffleDecodeConstantPool.h"
  14. #include "MCTargetDesc/X86ShuffleDecode.h"
  15. #include "llvm/ADT/APInt.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/IR/Constants.h"
  18. //===----------------------------------------------------------------------===//
  19. // Vector Mask Decoding
  20. //===----------------------------------------------------------------------===//
  21. namespace llvm {
  22. static bool extractConstantMask(const Constant *C, unsigned MaskEltSizeInBits,
  23. APInt &UndefElts,
  24. SmallVectorImpl<uint64_t> &RawMask) {
  25. // It is not an error for shuffle masks to not be a vector of
  26. // MaskEltSizeInBits because the constant pool uniques constants by their
  27. // bit representation.
  28. // e.g. the following take up the same space in the constant pool:
  29. // i128 -170141183420855150465331762880109871104
  30. //
  31. // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
  32. //
  33. // <4 x i32> <i32 -2147483648, i32 -2147483648,
  34. // i32 -2147483648, i32 -2147483648>
  35. auto *CstTy = dyn_cast<FixedVectorType>(C->getType());
  36. if (!CstTy)
  37. return false;
  38. Type *CstEltTy = CstTy->getElementType();
  39. if (!CstEltTy->isIntegerTy())
  40. return false;
  41. unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits();
  42. unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits();
  43. unsigned NumCstElts = CstTy->getNumElements();
  44. assert((CstSizeInBits % MaskEltSizeInBits) == 0 &&
  45. "Unaligned shuffle mask size");
  46. unsigned NumMaskElts = CstSizeInBits / MaskEltSizeInBits;
  47. UndefElts = APInt(NumMaskElts, 0);
  48. RawMask.resize(NumMaskElts, 0);
  49. // Fast path - if the constants match the mask size then copy direct.
  50. if (MaskEltSizeInBits == CstEltSizeInBits) {
  51. assert(NumCstElts == NumMaskElts && "Unaligned shuffle mask size");
  52. for (unsigned i = 0; i != NumMaskElts; ++i) {
  53. Constant *COp = C->getAggregateElement(i);
  54. if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
  55. return false;
  56. if (isa<UndefValue>(COp)) {
  57. UndefElts.setBit(i);
  58. RawMask[i] = 0;
  59. continue;
  60. }
  61. auto *Elt = cast<ConstantInt>(COp);
  62. RawMask[i] = Elt->getValue().getZExtValue();
  63. }
  64. return true;
  65. }
  66. // Extract all the undef/constant element data and pack into single bitsets.
  67. APInt UndefBits(CstSizeInBits, 0);
  68. APInt MaskBits(CstSizeInBits, 0);
  69. for (unsigned i = 0; i != NumCstElts; ++i) {
  70. Constant *COp = C->getAggregateElement(i);
  71. if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
  72. return false;
  73. unsigned BitOffset = i * CstEltSizeInBits;
  74. if (isa<UndefValue>(COp)) {
  75. UndefBits.setBits(BitOffset, BitOffset + CstEltSizeInBits);
  76. continue;
  77. }
  78. MaskBits.insertBits(cast<ConstantInt>(COp)->getValue(), BitOffset);
  79. }
  80. // Now extract the undef/constant bit data into the raw shuffle masks.
  81. for (unsigned i = 0; i != NumMaskElts; ++i) {
  82. unsigned BitOffset = i * MaskEltSizeInBits;
  83. APInt EltUndef = UndefBits.extractBits(MaskEltSizeInBits, BitOffset);
  84. // Only treat the element as UNDEF if all bits are UNDEF, otherwise
  85. // treat it as zero.
  86. if (EltUndef.isAllOnes()) {
  87. UndefElts.setBit(i);
  88. RawMask[i] = 0;
  89. continue;
  90. }
  91. APInt EltBits = MaskBits.extractBits(MaskEltSizeInBits, BitOffset);
  92. RawMask[i] = EltBits.getZExtValue();
  93. }
  94. return true;
  95. }
  96. void DecodePSHUFBMask(const Constant *C, unsigned Width,
  97. SmallVectorImpl<int> &ShuffleMask) {
  98. assert((Width == 128 || Width == 256 || Width == 512) &&
  99. C->getType()->getPrimitiveSizeInBits() >= Width &&
  100. "Unexpected vector size.");
  101. // The shuffle mask requires a byte vector.
  102. APInt UndefElts;
  103. SmallVector<uint64_t, 64> RawMask;
  104. if (!extractConstantMask(C, 8, UndefElts, RawMask))
  105. return;
  106. unsigned NumElts = Width / 8;
  107. assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
  108. "Unexpected number of vector elements.");
  109. for (unsigned i = 0; i != NumElts; ++i) {
  110. if (UndefElts[i]) {
  111. ShuffleMask.push_back(SM_SentinelUndef);
  112. continue;
  113. }
  114. uint64_t Element = RawMask[i];
  115. // If the high bit (7) of the byte is set, the element is zeroed.
  116. if (Element & (1 << 7))
  117. ShuffleMask.push_back(SM_SentinelZero);
  118. else {
  119. // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
  120. // lane of the vector we're inside.
  121. unsigned Base = i & ~0xf;
  122. // Only the least significant 4 bits of the byte are used.
  123. int Index = Base + (Element & 0xf);
  124. ShuffleMask.push_back(Index);
  125. }
  126. }
  127. }
  128. void DecodeVPERMILPMask(const Constant *C, unsigned ElSize, unsigned Width,
  129. SmallVectorImpl<int> &ShuffleMask) {
  130. assert((Width == 128 || Width == 256 || Width == 512) &&
  131. C->getType()->getPrimitiveSizeInBits() >= Width &&
  132. "Unexpected vector size.");
  133. assert((ElSize == 32 || ElSize == 64) && "Unexpected vector element size.");
  134. // The shuffle mask requires elements the same size as the target.
  135. APInt UndefElts;
  136. SmallVector<uint64_t, 16> RawMask;
  137. if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
  138. return;
  139. unsigned NumElts = Width / ElSize;
  140. unsigned NumEltsPerLane = 128 / ElSize;
  141. assert((NumElts == 2 || NumElts == 4 || NumElts == 8 || NumElts == 16) &&
  142. "Unexpected number of vector elements.");
  143. for (unsigned i = 0; i != NumElts; ++i) {
  144. if (UndefElts[i]) {
  145. ShuffleMask.push_back(SM_SentinelUndef);
  146. continue;
  147. }
  148. int Index = i & ~(NumEltsPerLane - 1);
  149. uint64_t Element = RawMask[i];
  150. if (ElSize == 64)
  151. Index += (Element >> 1) & 0x1;
  152. else
  153. Index += Element & 0x3;
  154. ShuffleMask.push_back(Index);
  155. }
  156. }
  157. void DecodeVPERMIL2PMask(const Constant *C, unsigned M2Z, unsigned ElSize,
  158. unsigned Width, SmallVectorImpl<int> &ShuffleMask) {
  159. Type *MaskTy = C->getType();
  160. unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
  161. (void)MaskTySize;
  162. assert((MaskTySize == 128 || MaskTySize == 256) && Width >= MaskTySize &&
  163. "Unexpected vector size.");
  164. // The shuffle mask requires elements the same size as the target.
  165. APInt UndefElts;
  166. SmallVector<uint64_t, 8> RawMask;
  167. if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
  168. return;
  169. unsigned NumElts = Width / ElSize;
  170. unsigned NumEltsPerLane = 128 / ElSize;
  171. assert((NumElts == 2 || NumElts == 4 || NumElts == 8) &&
  172. "Unexpected number of vector elements.");
  173. for (unsigned i = 0; i != NumElts; ++i) {
  174. if (UndefElts[i]) {
  175. ShuffleMask.push_back(SM_SentinelUndef);
  176. continue;
  177. }
  178. // VPERMIL2 Operation.
  179. // Bits[3] - Match Bit.
  180. // Bits[2:1] - (Per Lane) PD Shuffle Mask.
  181. // Bits[2:0] - (Per Lane) PS Shuffle Mask.
  182. uint64_t Selector = RawMask[i];
  183. unsigned MatchBit = (Selector >> 3) & 0x1;
  184. // M2Z[0:1] MatchBit
  185. // 0Xb X Source selected by Selector index.
  186. // 10b 0 Source selected by Selector index.
  187. // 10b 1 Zero.
  188. // 11b 0 Zero.
  189. // 11b 1 Source selected by Selector index.
  190. if ((M2Z & 0x2) != 0u && MatchBit != (M2Z & 0x1)) {
  191. ShuffleMask.push_back(SM_SentinelZero);
  192. continue;
  193. }
  194. int Index = i & ~(NumEltsPerLane - 1);
  195. if (ElSize == 64)
  196. Index += (Selector >> 1) & 0x1;
  197. else
  198. Index += Selector & 0x3;
  199. int Src = (Selector >> 2) & 0x1;
  200. Index += Src * NumElts;
  201. ShuffleMask.push_back(Index);
  202. }
  203. }
  204. void DecodeVPPERMMask(const Constant *C, unsigned Width,
  205. SmallVectorImpl<int> &ShuffleMask) {
  206. Type *MaskTy = C->getType();
  207. unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
  208. (void)MaskTySize;
  209. assert(Width == 128 && Width >= MaskTySize && "Unexpected vector size.");
  210. // The shuffle mask requires a byte vector.
  211. APInt UndefElts;
  212. SmallVector<uint64_t, 16> RawMask;
  213. if (!extractConstantMask(C, 8, UndefElts, RawMask))
  214. return;
  215. unsigned NumElts = Width / 8;
  216. assert(NumElts == 16 && "Unexpected number of vector elements.");
  217. for (unsigned i = 0; i != NumElts; ++i) {
  218. if (UndefElts[i]) {
  219. ShuffleMask.push_back(SM_SentinelUndef);
  220. continue;
  221. }
  222. // VPPERM Operation
  223. // Bits[4:0] - Byte Index (0 - 31)
  224. // Bits[7:5] - Permute Operation
  225. //
  226. // Permute Operation:
  227. // 0 - Source byte (no logical operation).
  228. // 1 - Invert source byte.
  229. // 2 - Bit reverse of source byte.
  230. // 3 - Bit reverse of inverted source byte.
  231. // 4 - 00h (zero - fill).
  232. // 5 - FFh (ones - fill).
  233. // 6 - Most significant bit of source byte replicated in all bit positions.
  234. // 7 - Invert most significant bit of source byte and replicate in all bit
  235. // positions.
  236. uint64_t Element = RawMask[i];
  237. uint64_t Index = Element & 0x1F;
  238. uint64_t PermuteOp = (Element >> 5) & 0x7;
  239. if (PermuteOp == 4) {
  240. ShuffleMask.push_back(SM_SentinelZero);
  241. continue;
  242. }
  243. if (PermuteOp != 0) {
  244. ShuffleMask.clear();
  245. return;
  246. }
  247. ShuffleMask.push_back((int)Index);
  248. }
  249. }
  250. } // namespace llvm