Path: blob/main/contrib/llvm-project/llvm/lib/Target/X86/MCTargetDesc/X86ShuffleDecode.cpp
35294 views
//===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Define several functions to decode x86 specific shuffle semantics into a9// generic vector mask.10//11//===----------------------------------------------------------------------===//1213#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"1819//===----------------------------------------------------------------------===//20// Vector Mask Decoding21//===----------------------------------------------------------------------===//2223namespace llvm {2425void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {26// Defaults the copying the dest value.27ShuffleMask.push_back(0);28ShuffleMask.push_back(1);29ShuffleMask.push_back(2);30ShuffleMask.push_back(3);3132// Decode the immediate.33unsigned ZMask = Imm & 15;34unsigned CountD = (Imm >> 4) & 3;35unsigned CountS = (Imm >> 6) & 3;3637// CountS selects which input element to use.38unsigned InVal = 4 + CountS;39// CountD specifies which element of destination to update.40ShuffleMask[CountD] = InVal;41// ZMask zaps values, potentially overriding the CountD elt.42if (ZMask & 1) ShuffleMask[0] = SM_SentinelZero;43if (ZMask & 2) ShuffleMask[1] = SM_SentinelZero;44if (ZMask & 4) ShuffleMask[2] = SM_SentinelZero;45if (ZMask & 8) ShuffleMask[3] = SM_SentinelZero;46}4748void DecodeInsertElementMask(unsigned NumElts, unsigned Idx, unsigned Len,49SmallVectorImpl<int> &ShuffleMask) {50assert((Idx + Len) <= NumElts && "Insertion out of range");5152for (unsigned i = 0; i != NumElts; ++i)53ShuffleMask.push_back(i);54for (unsigned i = 0; i != Len; ++i)55ShuffleMask[Idx + i] = NumElts + i;56}5758// <3,1> or <6,7,2,3>59void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {60for (unsigned i = NElts / 2; i != NElts; ++i)61ShuffleMask.push_back(NElts + i);6263for (unsigned i = NElts / 2; i != NElts; ++i)64ShuffleMask.push_back(i);65}6667// <0,2> or <0,1,4,5>68void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {69for (unsigned i = 0; i != NElts / 2; ++i)70ShuffleMask.push_back(i);7172for (unsigned i = 0; i != NElts / 2; ++i)73ShuffleMask.push_back(NElts + i);74}7576void DecodeMOVSLDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {77for (int i = 0, e = NumElts / 2; i < e; ++i) {78ShuffleMask.push_back(2 * i);79ShuffleMask.push_back(2 * i);80}81}8283void DecodeMOVSHDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {84for (int i = 0, e = NumElts / 2; i < e; ++i) {85ShuffleMask.push_back(2 * i + 1);86ShuffleMask.push_back(2 * i + 1);87}88}8990void DecodeMOVDDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {91const unsigned NumLaneElts = 2;9293for (unsigned l = 0; l < NumElts; l += NumLaneElts)94for (unsigned i = 0; i < NumLaneElts; ++i)95ShuffleMask.push_back(l);96}9798void DecodePSLLDQMask(unsigned NumElts, unsigned Imm,99SmallVectorImpl<int> &ShuffleMask) {100const unsigned NumLaneElts = 16;101102for (unsigned l = 0; l < NumElts; l += NumLaneElts)103for (unsigned i = 0; i < NumLaneElts; ++i) {104int M = SM_SentinelZero;105if (i >= Imm) M = i - Imm + l;106ShuffleMask.push_back(M);107}108}109110void DecodePSRLDQMask(unsigned NumElts, unsigned Imm,111SmallVectorImpl<int> &ShuffleMask) {112const unsigned NumLaneElts = 16;113114for (unsigned l = 0; l < NumElts; l += NumLaneElts)115for (unsigned i = 0; i < NumLaneElts; ++i) {116unsigned Base = i + Imm;117int M = Base + l;118if (Base >= NumLaneElts) M = SM_SentinelZero;119ShuffleMask.push_back(M);120}121}122123void DecodePALIGNRMask(unsigned NumElts, unsigned Imm,124SmallVectorImpl<int> &ShuffleMask) {125const unsigned NumLaneElts = 16;126127for (unsigned l = 0; l != NumElts; l += NumLaneElts) {128for (unsigned i = 0; i != NumLaneElts; ++i) {129unsigned Base = i + Imm;130// if i+imm is out of this lane then we actually need the other source131if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;132ShuffleMask.push_back(Base + l);133}134}135}136137void DecodeVALIGNMask(unsigned NumElts, unsigned Imm,138SmallVectorImpl<int> &ShuffleMask) {139// Not all bits of the immediate are used so mask it.140assert(isPowerOf2_32(NumElts) && "NumElts should be power of 2");141Imm = Imm & (NumElts - 1);142for (unsigned i = 0; i != NumElts; ++i)143ShuffleMask.push_back(i + Imm);144}145146void DecodePSHUFMask(unsigned NumElts, unsigned ScalarBits, unsigned Imm,147SmallVectorImpl<int> &ShuffleMask) {148unsigned Size = NumElts * ScalarBits;149unsigned NumLanes = Size / 128;150if (NumLanes == 0) NumLanes = 1; // Handle MMX151unsigned NumLaneElts = NumElts / NumLanes;152153uint32_t SplatImm = (Imm & 0xff) * 0x01010101;154for (unsigned l = 0; l != NumElts; l += NumLaneElts) {155for (unsigned i = 0; i != NumLaneElts; ++i) {156ShuffleMask.push_back(SplatImm % NumLaneElts + l);157SplatImm /= NumLaneElts;158}159}160}161162void DecodePSHUFHWMask(unsigned NumElts, unsigned Imm,163SmallVectorImpl<int> &ShuffleMask) {164for (unsigned l = 0; l != NumElts; l += 8) {165unsigned NewImm = Imm;166for (unsigned i = 0, e = 4; i != e; ++i) {167ShuffleMask.push_back(l + i);168}169for (unsigned i = 4, e = 8; i != e; ++i) {170ShuffleMask.push_back(l + 4 + (NewImm & 3));171NewImm >>= 2;172}173}174}175176void DecodePSHUFLWMask(unsigned NumElts, unsigned Imm,177SmallVectorImpl<int> &ShuffleMask) {178for (unsigned l = 0; l != NumElts; l += 8) {179unsigned NewImm = Imm;180for (unsigned i = 0, e = 4; i != e; ++i) {181ShuffleMask.push_back(l + (NewImm & 3));182NewImm >>= 2;183}184for (unsigned i = 4, e = 8; i != e; ++i) {185ShuffleMask.push_back(l + i);186}187}188}189190void DecodePSWAPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {191unsigned NumHalfElts = NumElts / 2;192193for (unsigned l = 0; l != NumHalfElts; ++l)194ShuffleMask.push_back(l + NumHalfElts);195for (unsigned h = 0; h != NumHalfElts; ++h)196ShuffleMask.push_back(h);197}198199void DecodeSHUFPMask(unsigned NumElts, unsigned ScalarBits,200unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {201unsigned NumLaneElts = 128 / ScalarBits;202203unsigned NewImm = Imm;204for (unsigned l = 0; l != NumElts; l += NumLaneElts) {205// each half of a lane comes from different source206for (unsigned s = 0; s != NumElts * 2; s += NumElts) {207for (unsigned i = 0; i != NumLaneElts / 2; ++i) {208ShuffleMask.push_back(NewImm % NumLaneElts + s + l);209NewImm /= NumLaneElts;210}211}212if (NumLaneElts == 4) NewImm = Imm; // reload imm213}214}215216void DecodeUNPCKHMask(unsigned NumElts, unsigned ScalarBits,217SmallVectorImpl<int> &ShuffleMask) {218// Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate219// independently on 128-bit lanes.220unsigned NumLanes = (NumElts * ScalarBits) / 128;221if (NumLanes == 0) NumLanes = 1; // Handle MMX222unsigned NumLaneElts = NumElts / NumLanes;223224for (unsigned l = 0; l != NumElts; l += NumLaneElts) {225for (unsigned i = l + NumLaneElts / 2, e = l + NumLaneElts; i != e; ++i) {226ShuffleMask.push_back(i); // Reads from dest/src1227ShuffleMask.push_back(i + NumElts); // Reads from src/src2228}229}230}231232void DecodeUNPCKLMask(unsigned NumElts, unsigned ScalarBits,233SmallVectorImpl<int> &ShuffleMask) {234// Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate235// independently on 128-bit lanes.236unsigned NumLanes = (NumElts * ScalarBits) / 128;237if (NumLanes == 0 ) NumLanes = 1; // Handle MMX238unsigned NumLaneElts = NumElts / NumLanes;239240for (unsigned l = 0; l != NumElts; l += NumLaneElts) {241for (unsigned i = l, e = l + NumLaneElts / 2; i != e; ++i) {242ShuffleMask.push_back(i); // Reads from dest/src1243ShuffleMask.push_back(i + NumElts); // Reads from src/src2244}245}246}247248void DecodeVectorBroadcast(unsigned NumElts,249SmallVectorImpl<int> &ShuffleMask) {250ShuffleMask.append(NumElts, 0);251}252253void DecodeSubVectorBroadcast(unsigned DstNumElts, unsigned SrcNumElts,254SmallVectorImpl<int> &ShuffleMask) {255unsigned Scale = DstNumElts / SrcNumElts;256257for (unsigned i = 0; i != Scale; ++i)258for (unsigned j = 0; j != SrcNumElts; ++j)259ShuffleMask.push_back(j);260}261262void decodeVSHUF64x2FamilyMask(unsigned NumElts, unsigned ScalarSize,263unsigned Imm,264SmallVectorImpl<int> &ShuffleMask) {265unsigned NumElementsInLane = 128 / ScalarSize;266unsigned NumLanes = NumElts / NumElementsInLane;267268for (unsigned l = 0; l != NumElts; l += NumElementsInLane) {269unsigned Index = (Imm % NumLanes) * NumElementsInLane;270Imm /= NumLanes; // Discard the bits we just used.271// We actually need the other source.272if (l >= (NumElts / 2))273Index += NumElts;274for (unsigned i = 0; i != NumElementsInLane; ++i)275ShuffleMask.push_back(Index + i);276}277}278279void DecodeVPERM2X128Mask(unsigned NumElts, unsigned Imm,280SmallVectorImpl<int> &ShuffleMask) {281unsigned HalfSize = NumElts / 2;282283for (unsigned l = 0; l != 2; ++l) {284unsigned HalfMask = Imm >> (l * 4);285unsigned HalfBegin = (HalfMask & 0x3) * HalfSize;286for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)287ShuffleMask.push_back((HalfMask & 8) ? SM_SentinelZero : (int)i);288}289}290291void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask, const APInt &UndefElts,292SmallVectorImpl<int> &ShuffleMask) {293for (int i = 0, e = RawMask.size(); i < e; ++i) {294uint64_t M = RawMask[i];295if (UndefElts[i]) {296ShuffleMask.push_back(SM_SentinelUndef);297continue;298}299// For 256/512-bit vectors the base of the shuffle is the 128-bit300// subvector we're inside.301int Base = (i / 16) * 16;302// If the high bit (7) of the byte is set, the element is zeroed.303if (M & (1 << 7))304ShuffleMask.push_back(SM_SentinelZero);305else {306// Only the least significant 4 bits of the byte are used.307int Index = Base + (M & 0xf);308ShuffleMask.push_back(Index);309}310}311}312313void DecodeBLENDMask(unsigned NumElts, unsigned Imm,314SmallVectorImpl<int> &ShuffleMask) {315for (unsigned i = 0; i < NumElts; ++i) {316// If there are more than 8 elements in the vector, then any immediate blend317// mask wraps around.318unsigned Bit = i % 8;319ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElts + i : i);320}321}322323void DecodeVPPERMMask(ArrayRef<uint64_t> RawMask, const APInt &UndefElts,324SmallVectorImpl<int> &ShuffleMask) {325assert(RawMask.size() == 16 && "Illegal VPPERM shuffle mask size");326327// VPPERM Operation328// Bits[4:0] - Byte Index (0 - 31)329// Bits[7:5] - Permute Operation330//331// Permute Operation:332// 0 - Source byte (no logical operation).333// 1 - Invert source byte.334// 2 - Bit reverse of source byte.335// 3 - Bit reverse of inverted source byte.336// 4 - 00h (zero - fill).337// 5 - FFh (ones - fill).338// 6 - Most significant bit of source byte replicated in all bit positions.339// 7 - Invert most significant bit of source byte and replicate in all bit positions.340for (int i = 0, e = RawMask.size(); i < e; ++i) {341if (UndefElts[i]) {342ShuffleMask.push_back(SM_SentinelUndef);343continue;344}345346uint64_t M = RawMask[i];347uint64_t PermuteOp = (M >> 5) & 0x7;348if (PermuteOp == 4) {349ShuffleMask.push_back(SM_SentinelZero);350continue;351}352if (PermuteOp != 0) {353ShuffleMask.clear();354return;355}356357uint64_t Index = M & 0x1F;358ShuffleMask.push_back((int)Index);359}360}361362void DecodeVPERMMask(unsigned NumElts, unsigned Imm,363SmallVectorImpl<int> &ShuffleMask) {364for (unsigned l = 0; l != NumElts; l += 4)365for (unsigned i = 0; i != 4; ++i)366ShuffleMask.push_back(l + ((Imm >> (2 * i)) & 3));367}368369void DecodeZeroExtendMask(unsigned SrcScalarBits, unsigned DstScalarBits,370unsigned NumDstElts, bool IsAnyExtend,371SmallVectorImpl<int> &ShuffleMask) {372unsigned Scale = DstScalarBits / SrcScalarBits;373assert(SrcScalarBits < DstScalarBits &&374"Expected zero extension mask to increase scalar size");375376int Sentinel = IsAnyExtend ? SM_SentinelUndef : SM_SentinelZero;377for (unsigned i = 0; i != NumDstElts; i++) {378ShuffleMask.push_back(i);379ShuffleMask.append(Scale - 1, Sentinel);380}381}382383void DecodeZeroMoveLowMask(unsigned NumElts,384SmallVectorImpl<int> &ShuffleMask) {385ShuffleMask.push_back(0);386ShuffleMask.append(NumElts - 1, SM_SentinelZero);387}388389void DecodeScalarMoveMask(unsigned NumElts, bool IsLoad,390SmallVectorImpl<int> &ShuffleMask) {391// First element comes from the first element of second source.392// Remaining elements: Load zero extends / Move copies from first source.393ShuffleMask.push_back(NumElts);394for (unsigned i = 1; i < NumElts; i++)395ShuffleMask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i);396}397398void DecodeEXTRQIMask(unsigned NumElts, unsigned EltSize, int Len, int Idx,399SmallVectorImpl<int> &ShuffleMask) {400unsigned HalfElts = NumElts / 2;401402// Only the bottom 6 bits are valid for each immediate.403Len &= 0x3F;404Idx &= 0x3F;405406// We can only decode this bit extraction instruction as a shuffle if both the407// length and index work with whole elements.408if (0 != (Len % EltSize) || 0 != (Idx % EltSize))409return;410411// A length of zero is equivalent to a bit length of 64.412if (Len == 0)413Len = 64;414415// If the length + index exceeds the bottom 64 bits the result is undefined.416if ((Len + Idx) > 64) {417ShuffleMask.append(NumElts, SM_SentinelUndef);418return;419}420421// Convert index and index to work with elements.422Len /= EltSize;423Idx /= EltSize;424425// EXTRQ: Extract Len elements starting from Idx. Zero pad the remaining426// elements of the lower 64-bits. The upper 64-bits are undefined.427for (int i = 0; i != Len; ++i)428ShuffleMask.push_back(i + Idx);429for (int i = Len; i != (int)HalfElts; ++i)430ShuffleMask.push_back(SM_SentinelZero);431for (int i = HalfElts; i != (int)NumElts; ++i)432ShuffleMask.push_back(SM_SentinelUndef);433}434435void DecodeINSERTQIMask(unsigned NumElts, unsigned EltSize, int Len, int Idx,436SmallVectorImpl<int> &ShuffleMask) {437unsigned HalfElts = NumElts / 2;438439// Only the bottom 6 bits are valid for each immediate.440Len &= 0x3F;441Idx &= 0x3F;442443// We can only decode this bit insertion instruction as a shuffle if both the444// length and index work with whole elements.445if (0 != (Len % EltSize) || 0 != (Idx % EltSize))446return;447448// A length of zero is equivalent to a bit length of 64.449if (Len == 0)450Len = 64;451452// If the length + index exceeds the bottom 64 bits the result is undefined.453if ((Len + Idx) > 64) {454ShuffleMask.append(NumElts, SM_SentinelUndef);455return;456}457458// Convert index and index to work with elements.459Len /= EltSize;460Idx /= EltSize;461462// INSERTQ: Extract lowest Len elements from lower half of second source and463// insert over first source starting at Idx element. The upper 64-bits are464// undefined.465for (int i = 0; i != Idx; ++i)466ShuffleMask.push_back(i);467for (int i = 0; i != Len; ++i)468ShuffleMask.push_back(i + NumElts);469for (int i = Idx + Len; i != (int)HalfElts; ++i)470ShuffleMask.push_back(i);471for (int i = HalfElts; i != (int)NumElts; ++i)472ShuffleMask.push_back(SM_SentinelUndef);473}474475void DecodeVPERMILPMask(unsigned NumElts, unsigned ScalarBits,476ArrayRef<uint64_t> RawMask, const APInt &UndefElts,477SmallVectorImpl<int> &ShuffleMask) {478unsigned VecSize = NumElts * ScalarBits;479unsigned NumLanes = VecSize / 128;480unsigned NumEltsPerLane = NumElts / NumLanes;481assert((VecSize == 128 || VecSize == 256 || VecSize == 512) &&482"Unexpected vector size");483assert((ScalarBits == 32 || ScalarBits == 64) && "Unexpected element size");484485for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {486if (UndefElts[i]) {487ShuffleMask.push_back(SM_SentinelUndef);488continue;489}490uint64_t M = RawMask[i];491M = (ScalarBits == 64 ? ((M >> 1) & 0x1) : (M & 0x3));492unsigned LaneOffset = i & ~(NumEltsPerLane - 1);493ShuffleMask.push_back((int)(LaneOffset + M));494}495}496497void DecodeVPERMIL2PMask(unsigned NumElts, unsigned ScalarBits, unsigned M2Z,498ArrayRef<uint64_t> RawMask, const APInt &UndefElts,499SmallVectorImpl<int> &ShuffleMask) {500unsigned VecSize = NumElts * ScalarBits;501unsigned NumLanes = VecSize / 128;502unsigned NumEltsPerLane = NumElts / NumLanes;503assert((VecSize == 128 || VecSize == 256) && "Unexpected vector size");504assert((ScalarBits == 32 || ScalarBits == 64) && "Unexpected element size");505assert((NumElts == RawMask.size()) && "Unexpected mask size");506507for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {508if (UndefElts[i]) {509ShuffleMask.push_back(SM_SentinelUndef);510continue;511}512513// VPERMIL2 Operation.514// Bits[3] - Match Bit.515// Bits[2:1] - (Per Lane) PD Shuffle Mask.516// Bits[2:0] - (Per Lane) PS Shuffle Mask.517uint64_t Selector = RawMask[i];518unsigned MatchBit = (Selector >> 3) & 0x1;519520// M2Z[0:1] MatchBit521// 0Xb X Source selected by Selector index.522// 10b 0 Source selected by Selector index.523// 10b 1 Zero.524// 11b 0 Zero.525// 11b 1 Source selected by Selector index.526if ((M2Z & 0x2) != 0 && MatchBit != (M2Z & 0x1)) {527ShuffleMask.push_back(SM_SentinelZero);528continue;529}530531int Index = i & ~(NumEltsPerLane - 1);532if (ScalarBits == 64)533Index += (Selector >> 1) & 0x1;534else535Index += Selector & 0x3;536537int Src = (Selector >> 2) & 0x1;538Index += Src * NumElts;539ShuffleMask.push_back(Index);540}541}542543void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask, const APInt &UndefElts,544SmallVectorImpl<int> &ShuffleMask) {545uint64_t EltMaskSize = RawMask.size() - 1;546for (int i = 0, e = RawMask.size(); i != e; ++i) {547if (UndefElts[i]) {548ShuffleMask.push_back(SM_SentinelUndef);549continue;550}551uint64_t M = RawMask[i];552M &= EltMaskSize;553ShuffleMask.push_back((int)M);554}555}556557void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask, const APInt &UndefElts,558SmallVectorImpl<int> &ShuffleMask) {559uint64_t EltMaskSize = (RawMask.size() * 2) - 1;560for (int i = 0, e = RawMask.size(); i != e; ++i) {561if (UndefElts[i]) {562ShuffleMask.push_back(SM_SentinelUndef);563continue;564}565uint64_t M = RawMask[i];566M &= EltMaskSize;567ShuffleMask.push_back((int)M);568}569}570571} // namespace llvm572573574