X86ShuffleDecode.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. //===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===//
  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 into a
  10. // generic vector mask.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "X86ShuffleDecode.h"
  14. #include "llvm/ADT/APInt.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Support/MathExtras.h"
  18. //===----------------------------------------------------------------------===//
  19. // Vector Mask Decoding
  20. //===----------------------------------------------------------------------===//
  21. namespace llvm {
  22. void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
  23. // Defaults the copying the dest value.
  24. ShuffleMask.push_back(0);
  25. ShuffleMask.push_back(1);
  26. ShuffleMask.push_back(2);
  27. ShuffleMask.push_back(3);
  28. // Decode the immediate.
  29. unsigned ZMask = Imm & 15;
  30. unsigned CountD = (Imm >> 4) & 3;
  31. unsigned CountS = (Imm >> 6) & 3;
  32. // CountS selects which input element to use.
  33. unsigned InVal = 4 + CountS;
  34. // CountD specifies which element of destination to update.
  35. ShuffleMask[CountD] = InVal;
  36. // ZMask zaps values, potentially overriding the CountD elt.
  37. if (ZMask & 1) ShuffleMask[0] = SM_SentinelZero;
  38. if (ZMask & 2) ShuffleMask[1] = SM_SentinelZero;
  39. if (ZMask & 4) ShuffleMask[2] = SM_SentinelZero;
  40. if (ZMask & 8) ShuffleMask[3] = SM_SentinelZero;
  41. }
  42. void DecodeInsertElementMask(unsigned NumElts, unsigned Idx, unsigned Len,
  43. SmallVectorImpl<int> &ShuffleMask) {
  44. assert((Idx + Len) <= NumElts && "Insertion out of range");
  45. for (unsigned i = 0; i != NumElts; ++i)
  46. ShuffleMask.push_back(i);
  47. for (unsigned i = 0; i != Len; ++i)
  48. ShuffleMask[Idx + i] = NumElts + i;
  49. }
  50. // <3,1> or <6,7,2,3>
  51. void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
  52. for (unsigned i = NElts / 2; i != NElts; ++i)
  53. ShuffleMask.push_back(NElts + i);
  54. for (unsigned i = NElts / 2; i != NElts; ++i)
  55. ShuffleMask.push_back(i);
  56. }
  57. // <0,2> or <0,1,4,5>
  58. void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
  59. for (unsigned i = 0; i != NElts / 2; ++i)
  60. ShuffleMask.push_back(i);
  61. for (unsigned i = 0; i != NElts / 2; ++i)
  62. ShuffleMask.push_back(NElts + i);
  63. }
  64. void DecodeMOVSLDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {
  65. for (int i = 0, e = NumElts / 2; i < e; ++i) {
  66. ShuffleMask.push_back(2 * i);
  67. ShuffleMask.push_back(2 * i);
  68. }
  69. }
  70. void DecodeMOVSHDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {
  71. for (int i = 0, e = NumElts / 2; i < e; ++i) {
  72. ShuffleMask.push_back(2 * i + 1);
  73. ShuffleMask.push_back(2 * i + 1);
  74. }
  75. }
  76. void DecodeMOVDDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {
  77. const unsigned NumLaneElts = 2;
  78. for (unsigned l = 0; l < NumElts; l += NumLaneElts)
  79. for (unsigned i = 0; i < NumLaneElts; ++i)
  80. ShuffleMask.push_back(l);
  81. }
  82. void DecodePSLLDQMask(unsigned NumElts, unsigned Imm,
  83. SmallVectorImpl<int> &ShuffleMask) {
  84. const unsigned NumLaneElts = 16;
  85. for (unsigned l = 0; l < NumElts; l += NumLaneElts)
  86. for (unsigned i = 0; i < NumLaneElts; ++i) {
  87. int M = SM_SentinelZero;
  88. if (i >= Imm) M = i - Imm + l;
  89. ShuffleMask.push_back(M);
  90. }
  91. }
  92. void DecodePSRLDQMask(unsigned NumElts, unsigned Imm,
  93. SmallVectorImpl<int> &ShuffleMask) {
  94. const unsigned NumLaneElts = 16;
  95. for (unsigned l = 0; l < NumElts; l += NumLaneElts)
  96. for (unsigned i = 0; i < NumLaneElts; ++i) {
  97. unsigned Base = i + Imm;
  98. int M = Base + l;
  99. if (Base >= NumLaneElts) M = SM_SentinelZero;
  100. ShuffleMask.push_back(M);
  101. }
  102. }
  103. void DecodePALIGNRMask(unsigned NumElts, unsigned Imm,
  104. SmallVectorImpl<int> &ShuffleMask) {
  105. const unsigned NumLaneElts = 16;
  106. for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
  107. for (unsigned i = 0; i != NumLaneElts; ++i) {
  108. unsigned Base = i + Imm;
  109. // if i+imm is out of this lane then we actually need the other source
  110. if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;
  111. ShuffleMask.push_back(Base + l);
  112. }
  113. }
  114. }
  115. void DecodeVALIGNMask(unsigned NumElts, unsigned Imm,
  116. SmallVectorImpl<int> &ShuffleMask) {
  117. // Not all bits of the immediate are used so mask it.
  118. assert(isPowerOf2_32(NumElts) && "NumElts should be power of 2");
  119. Imm = Imm & (NumElts - 1);
  120. for (unsigned i = 0; i != NumElts; ++i)
  121. ShuffleMask.push_back(i + Imm);
  122. }
  123. void DecodePSHUFMask(unsigned NumElts, unsigned ScalarBits, unsigned Imm,
  124. SmallVectorImpl<int> &ShuffleMask) {
  125. unsigned Size = NumElts * ScalarBits;
  126. unsigned NumLanes = Size / 128;
  127. if (NumLanes == 0) NumLanes = 1; // Handle MMX
  128. unsigned NumLaneElts = NumElts / NumLanes;
  129. uint32_t SplatImm = (Imm & 0xff) * 0x01010101;
  130. for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
  131. for (unsigned i = 0; i != NumLaneElts; ++i) {
  132. ShuffleMask.push_back(SplatImm % NumLaneElts + l);
  133. SplatImm /= NumLaneElts;
  134. }
  135. }
  136. }
  137. void DecodePSHUFHWMask(unsigned NumElts, unsigned Imm,
  138. SmallVectorImpl<int> &ShuffleMask) {
  139. for (unsigned l = 0; l != NumElts; l += 8) {
  140. unsigned NewImm = Imm;
  141. for (unsigned i = 0, e = 4; i != e; ++i) {
  142. ShuffleMask.push_back(l + i);
  143. }
  144. for (unsigned i = 4, e = 8; i != e; ++i) {
  145. ShuffleMask.push_back(l + 4 + (NewImm & 3));
  146. NewImm >>= 2;
  147. }
  148. }
  149. }
  150. void DecodePSHUFLWMask(unsigned NumElts, unsigned Imm,
  151. SmallVectorImpl<int> &ShuffleMask) {
  152. for (unsigned l = 0; l != NumElts; l += 8) {
  153. unsigned NewImm = Imm;
  154. for (unsigned i = 0, e = 4; i != e; ++i) {
  155. ShuffleMask.push_back(l + (NewImm & 3));
  156. NewImm >>= 2;
  157. }
  158. for (unsigned i = 4, e = 8; i != e; ++i) {
  159. ShuffleMask.push_back(l + i);
  160. }
  161. }
  162. }
  163. void DecodePSWAPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {
  164. unsigned NumHalfElts = NumElts / 2;
  165. for (unsigned l = 0; l != NumHalfElts; ++l)
  166. ShuffleMask.push_back(l + NumHalfElts);
  167. for (unsigned h = 0; h != NumHalfElts; ++h)
  168. ShuffleMask.push_back(h);
  169. }
  170. void DecodeSHUFPMask(unsigned NumElts, unsigned ScalarBits,
  171. unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
  172. unsigned NumLaneElts = 128 / ScalarBits;
  173. unsigned NewImm = Imm;
  174. for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
  175. // each half of a lane comes from different source
  176. for (unsigned s = 0; s != NumElts * 2; s += NumElts) {
  177. for (unsigned i = 0; i != NumLaneElts / 2; ++i) {
  178. ShuffleMask.push_back(NewImm % NumLaneElts + s + l);
  179. NewImm /= NumLaneElts;
  180. }
  181. }
  182. if (NumLaneElts == 4) NewImm = Imm; // reload imm
  183. }
  184. }
  185. void DecodeUNPCKHMask(unsigned NumElts, unsigned ScalarBits,
  186. SmallVectorImpl<int> &ShuffleMask) {
  187. // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
  188. // independently on 128-bit lanes.
  189. unsigned NumLanes = (NumElts * ScalarBits) / 128;
  190. if (NumLanes == 0) NumLanes = 1; // Handle MMX
  191. unsigned NumLaneElts = NumElts / NumLanes;
  192. for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
  193. for (unsigned i = l + NumLaneElts / 2, e = l + NumLaneElts; i != e; ++i) {
  194. ShuffleMask.push_back(i); // Reads from dest/src1
  195. ShuffleMask.push_back(i + NumElts); // Reads from src/src2
  196. }
  197. }
  198. }
  199. void DecodeUNPCKLMask(unsigned NumElts, unsigned ScalarBits,
  200. SmallVectorImpl<int> &ShuffleMask) {
  201. // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
  202. // independently on 128-bit lanes.
  203. unsigned NumLanes = (NumElts * ScalarBits) / 128;
  204. if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
  205. unsigned NumLaneElts = NumElts / NumLanes;
  206. for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
  207. for (unsigned i = l, e = l + NumLaneElts / 2; i != e; ++i) {
  208. ShuffleMask.push_back(i); // Reads from dest/src1
  209. ShuffleMask.push_back(i + NumElts); // Reads from src/src2
  210. }
  211. }
  212. }
  213. void DecodeVectorBroadcast(unsigned NumElts,
  214. SmallVectorImpl<int> &ShuffleMask) {
  215. ShuffleMask.append(NumElts, 0);
  216. }
  217. void DecodeSubVectorBroadcast(unsigned DstNumElts, unsigned SrcNumElts,
  218. SmallVectorImpl<int> &ShuffleMask) {
  219. unsigned Scale = DstNumElts / SrcNumElts;
  220. for (unsigned i = 0; i != Scale; ++i)
  221. for (unsigned j = 0; j != SrcNumElts; ++j)
  222. ShuffleMask.push_back(j);
  223. }
  224. void decodeVSHUF64x2FamilyMask(unsigned NumElts, unsigned ScalarSize,
  225. unsigned Imm,
  226. SmallVectorImpl<int> &ShuffleMask) {
  227. unsigned NumElementsInLane = 128 / ScalarSize;
  228. unsigned NumLanes = NumElts / NumElementsInLane;
  229. for (unsigned l = 0; l != NumElts; l += NumElementsInLane) {
  230. unsigned Index = (Imm % NumLanes) * NumElementsInLane;
  231. Imm /= NumLanes; // Discard the bits we just used.
  232. // We actually need the other source.
  233. if (l >= (NumElts / 2))
  234. Index += NumElts;
  235. for (unsigned i = 0; i != NumElementsInLane; ++i)
  236. ShuffleMask.push_back(Index + i);
  237. }
  238. }
  239. void DecodeVPERM2X128Mask(unsigned NumElts, unsigned Imm,
  240. SmallVectorImpl<int> &ShuffleMask) {
  241. unsigned HalfSize = NumElts / 2;
  242. for (unsigned l = 0; l != 2; ++l) {
  243. unsigned HalfMask = Imm >> (l * 4);
  244. unsigned HalfBegin = (HalfMask & 0x3) * HalfSize;
  245. for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)
  246. ShuffleMask.push_back((HalfMask & 8) ? SM_SentinelZero : (int)i);
  247. }
  248. }
  249. void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask, const APInt &UndefElts,
  250. SmallVectorImpl<int> &ShuffleMask) {
  251. for (int i = 0, e = RawMask.size(); i < e; ++i) {
  252. uint64_t M = RawMask[i];
  253. if (UndefElts[i]) {
  254. ShuffleMask.push_back(SM_SentinelUndef);
  255. continue;
  256. }
  257. // For 256/512-bit vectors the base of the shuffle is the 128-bit
  258. // subvector we're inside.
  259. int Base = (i / 16) * 16;
  260. // If the high bit (7) of the byte is set, the element is zeroed.
  261. if (M & (1 << 7))
  262. ShuffleMask.push_back(SM_SentinelZero);
  263. else {
  264. // Only the least significant 4 bits of the byte are used.
  265. int Index = Base + (M & 0xf);
  266. ShuffleMask.push_back(Index);
  267. }
  268. }
  269. }
  270. void DecodeBLENDMask(unsigned NumElts, unsigned Imm,
  271. SmallVectorImpl<int> &ShuffleMask) {
  272. for (unsigned i = 0; i < NumElts; ++i) {
  273. // If there are more than 8 elements in the vector, then any immediate blend
  274. // mask wraps around.
  275. unsigned Bit = i % 8;
  276. ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElts + i : i);
  277. }
  278. }
  279. void DecodeVPPERMMask(ArrayRef<uint64_t> RawMask, const APInt &UndefElts,
  280. SmallVectorImpl<int> &ShuffleMask) {
  281. assert(RawMask.size() == 16 && "Illegal VPPERM shuffle mask size");
  282. // VPPERM Operation
  283. // Bits[4:0] - Byte Index (0 - 31)
  284. // Bits[7:5] - Permute Operation
  285. //
  286. // Permute Operation:
  287. // 0 - Source byte (no logical operation).
  288. // 1 - Invert source byte.
  289. // 2 - Bit reverse of source byte.
  290. // 3 - Bit reverse of inverted source byte.
  291. // 4 - 00h (zero - fill).
  292. // 5 - FFh (ones - fill).
  293. // 6 - Most significant bit of source byte replicated in all bit positions.
  294. // 7 - Invert most significant bit of source byte and replicate in all bit positions.
  295. for (int i = 0, e = RawMask.size(); i < e; ++i) {
  296. if (UndefElts[i]) {
  297. ShuffleMask.push_back(SM_SentinelUndef);
  298. continue;
  299. }
  300. uint64_t M = RawMask[i];
  301. uint64_t PermuteOp = (M >> 5) & 0x7;
  302. if (PermuteOp == 4) {
  303. ShuffleMask.push_back(SM_SentinelZero);
  304. continue;
  305. }
  306. if (PermuteOp != 0) {
  307. ShuffleMask.clear();
  308. return;
  309. }
  310. uint64_t Index = M & 0x1F;
  311. ShuffleMask.push_back((int)Index);
  312. }
  313. }
  314. void DecodeVPERMMask(unsigned NumElts, unsigned Imm,
  315. SmallVectorImpl<int> &ShuffleMask) {
  316. for (unsigned l = 0; l != NumElts; l += 4)
  317. for (unsigned i = 0; i != 4; ++i)
  318. ShuffleMask.push_back(l + ((Imm >> (2 * i)) & 3));
  319. }
  320. void DecodeZeroExtendMask(unsigned SrcScalarBits, unsigned DstScalarBits,
  321. unsigned NumDstElts, bool IsAnyExtend,
  322. SmallVectorImpl<int> &ShuffleMask) {
  323. unsigned Scale = DstScalarBits / SrcScalarBits;
  324. assert(SrcScalarBits < DstScalarBits &&
  325. "Expected zero extension mask to increase scalar size");
  326. int Sentinel = IsAnyExtend ? SM_SentinelUndef : SM_SentinelZero;
  327. for (unsigned i = 0; i != NumDstElts; i++) {
  328. ShuffleMask.push_back(i);
  329. ShuffleMask.append(Scale - 1, Sentinel);
  330. }
  331. }
  332. void DecodeZeroMoveLowMask(unsigned NumElts,
  333. SmallVectorImpl<int> &ShuffleMask) {
  334. ShuffleMask.push_back(0);
  335. ShuffleMask.append(NumElts - 1, SM_SentinelZero);
  336. }
  337. void DecodeScalarMoveMask(unsigned NumElts, bool IsLoad,
  338. SmallVectorImpl<int> &ShuffleMask) {
  339. // First element comes from the first element of second source.
  340. // Remaining elements: Load zero extends / Move copies from first source.
  341. ShuffleMask.push_back(NumElts);
  342. for (unsigned i = 1; i < NumElts; i++)
  343. ShuffleMask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i);
  344. }
  345. void DecodeEXTRQIMask(unsigned NumElts, unsigned EltSize, int Len, int Idx,
  346. SmallVectorImpl<int> &ShuffleMask) {
  347. unsigned HalfElts = NumElts / 2;
  348. // Only the bottom 6 bits are valid for each immediate.
  349. Len &= 0x3F;
  350. Idx &= 0x3F;
  351. // We can only decode this bit extraction instruction as a shuffle if both the
  352. // length and index work with whole elements.
  353. if (0 != (Len % EltSize) || 0 != (Idx % EltSize))
  354. return;
  355. // A length of zero is equivalent to a bit length of 64.
  356. if (Len == 0)
  357. Len = 64;
  358. // If the length + index exceeds the bottom 64 bits the result is undefined.
  359. if ((Len + Idx) > 64) {
  360. ShuffleMask.append(NumElts, SM_SentinelUndef);
  361. return;
  362. }
  363. // Convert index and index to work with elements.
  364. Len /= EltSize;
  365. Idx /= EltSize;
  366. // EXTRQ: Extract Len elements starting from Idx. Zero pad the remaining
  367. // elements of the lower 64-bits. The upper 64-bits are undefined.
  368. for (int i = 0; i != Len; ++i)
  369. ShuffleMask.push_back(i + Idx);
  370. for (int i = Len; i != (int)HalfElts; ++i)
  371. ShuffleMask.push_back(SM_SentinelZero);
  372. for (int i = HalfElts; i != (int)NumElts; ++i)
  373. ShuffleMask.push_back(SM_SentinelUndef);
  374. }
  375. void DecodeINSERTQIMask(unsigned NumElts, unsigned EltSize, int Len, int Idx,
  376. SmallVectorImpl<int> &ShuffleMask) {
  377. unsigned HalfElts = NumElts / 2;
  378. // Only the bottom 6 bits are valid for each immediate.
  379. Len &= 0x3F;
  380. Idx &= 0x3F;
  381. // We can only decode this bit insertion instruction as a shuffle if both the
  382. // length and index work with whole elements.
  383. if (0 != (Len % EltSize) || 0 != (Idx % EltSize))
  384. return;
  385. // A length of zero is equivalent to a bit length of 64.
  386. if (Len == 0)
  387. Len = 64;
  388. // If the length + index exceeds the bottom 64 bits the result is undefined.
  389. if ((Len + Idx) > 64) {
  390. ShuffleMask.append(NumElts, SM_SentinelUndef);
  391. return;
  392. }
  393. // Convert index and index to work with elements.
  394. Len /= EltSize;
  395. Idx /= EltSize;
  396. // INSERTQ: Extract lowest Len elements from lower half of second source and
  397. // insert over first source starting at Idx element. The upper 64-bits are
  398. // undefined.
  399. for (int i = 0; i != Idx; ++i)
  400. ShuffleMask.push_back(i);
  401. for (int i = 0; i != Len; ++i)
  402. ShuffleMask.push_back(i + NumElts);
  403. for (int i = Idx + Len; i != (int)HalfElts; ++i)
  404. ShuffleMask.push_back(i);
  405. for (int i = HalfElts; i != (int)NumElts; ++i)
  406. ShuffleMask.push_back(SM_SentinelUndef);
  407. }
  408. void DecodeVPERMILPMask(unsigned NumElts, unsigned ScalarBits,
  409. ArrayRef<uint64_t> RawMask, const APInt &UndefElts,
  410. SmallVectorImpl<int> &ShuffleMask) {
  411. unsigned VecSize = NumElts * ScalarBits;
  412. unsigned NumLanes = VecSize / 128;
  413. unsigned NumEltsPerLane = NumElts / NumLanes;
  414. assert((VecSize == 128 || VecSize == 256 || VecSize == 512) &&
  415. "Unexpected vector size");
  416. assert((ScalarBits == 32 || ScalarBits == 64) && "Unexpected element size");
  417. for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {
  418. if (UndefElts[i]) {
  419. ShuffleMask.push_back(SM_SentinelUndef);
  420. continue;
  421. }
  422. uint64_t M = RawMask[i];
  423. M = (ScalarBits == 64 ? ((M >> 1) & 0x1) : (M & 0x3));
  424. unsigned LaneOffset = i & ~(NumEltsPerLane - 1);
  425. ShuffleMask.push_back((int)(LaneOffset + M));
  426. }
  427. }
  428. void DecodeVPERMIL2PMask(unsigned NumElts, unsigned ScalarBits, unsigned M2Z,
  429. ArrayRef<uint64_t> RawMask, const APInt &UndefElts,
  430. SmallVectorImpl<int> &ShuffleMask) {
  431. unsigned VecSize = NumElts * ScalarBits;
  432. unsigned NumLanes = VecSize / 128;
  433. unsigned NumEltsPerLane = NumElts / NumLanes;
  434. assert((VecSize == 128 || VecSize == 256) && "Unexpected vector size");
  435. assert((ScalarBits == 32 || ScalarBits == 64) && "Unexpected element size");
  436. assert((NumElts == RawMask.size()) && "Unexpected mask size");
  437. for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {
  438. if (UndefElts[i]) {
  439. ShuffleMask.push_back(SM_SentinelUndef);
  440. continue;
  441. }
  442. // VPERMIL2 Operation.
  443. // Bits[3] - Match Bit.
  444. // Bits[2:1] - (Per Lane) PD Shuffle Mask.
  445. // Bits[2:0] - (Per Lane) PS Shuffle Mask.
  446. uint64_t Selector = RawMask[i];
  447. unsigned MatchBit = (Selector >> 3) & 0x1;
  448. // M2Z[0:1] MatchBit
  449. // 0Xb X Source selected by Selector index.
  450. // 10b 0 Source selected by Selector index.
  451. // 10b 1 Zero.
  452. // 11b 0 Zero.
  453. // 11b 1 Source selected by Selector index.
  454. if ((M2Z & 0x2) != 0 && MatchBit != (M2Z & 0x1)) {
  455. ShuffleMask.push_back(SM_SentinelZero);
  456. continue;
  457. }
  458. int Index = i & ~(NumEltsPerLane - 1);
  459. if (ScalarBits == 64)
  460. Index += (Selector >> 1) & 0x1;
  461. else
  462. Index += Selector & 0x3;
  463. int Src = (Selector >> 2) & 0x1;
  464. Index += Src * NumElts;
  465. ShuffleMask.push_back(Index);
  466. }
  467. }
  468. void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask, const APInt &UndefElts,
  469. SmallVectorImpl<int> &ShuffleMask) {
  470. uint64_t EltMaskSize = RawMask.size() - 1;
  471. for (int i = 0, e = RawMask.size(); i != e; ++i) {
  472. if (UndefElts[i]) {
  473. ShuffleMask.push_back(SM_SentinelUndef);
  474. continue;
  475. }
  476. uint64_t M = RawMask[i];
  477. M &= EltMaskSize;
  478. ShuffleMask.push_back((int)M);
  479. }
  480. }
  481. void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask, const APInt &UndefElts,
  482. SmallVectorImpl<int> &ShuffleMask) {
  483. uint64_t EltMaskSize = (RawMask.size() * 2) - 1;
  484. for (int i = 0, e = RawMask.size(); i != e; ++i) {
  485. if (UndefElts[i]) {
  486. ShuffleMask.push_back(SM_SentinelUndef);
  487. continue;
  488. }
  489. uint64_t M = RawMask[i];
  490. M &= EltMaskSize;
  491. ShuffleMask.push_back((int)M);
  492. }
  493. }
  494. } // namespace llvm