#include "X86ShuffleDecodeConstantPool.h"
#include "MCTargetDesc/X86ShuffleDecode.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Constants.h"
namespace llvm {
static bool extractConstantMask(const Constant *C, unsigned MaskEltSizeInBits,
APInt &UndefElts,
SmallVectorImpl<uint64_t> &RawMask) {
auto *CstTy = dyn_cast<FixedVectorType>(C->getType());
if (!CstTy)
return false;
Type *CstEltTy = CstTy->getElementType();
if (!CstEltTy->isIntegerTy())
return false;
unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits();
unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits();
unsigned NumCstElts = CstTy->getNumElements();
assert((CstSizeInBits % MaskEltSizeInBits) == 0 &&
"Unaligned shuffle mask size");
unsigned NumMaskElts = CstSizeInBits / MaskEltSizeInBits;
UndefElts = APInt(NumMaskElts, 0);
RawMask.resize(NumMaskElts, 0);
if (MaskEltSizeInBits == CstEltSizeInBits) {
assert(NumCstElts == NumMaskElts && "Unaligned shuffle mask size");
for (unsigned i = 0; i != NumMaskElts; ++i) {
Constant *COp = C->getAggregateElement(i);
if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
return false;
if (isa<UndefValue>(COp)) {
UndefElts.setBit(i);
RawMask[i] = 0;
continue;
}
auto *Elt = cast<ConstantInt>(COp);
RawMask[i] = Elt->getValue().getZExtValue();
}
return true;
}
APInt UndefBits(CstSizeInBits, 0);
APInt MaskBits(CstSizeInBits, 0);
for (unsigned i = 0; i != NumCstElts; ++i) {
Constant *COp = C->getAggregateElement(i);
if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
return false;
unsigned BitOffset = i * CstEltSizeInBits;
if (isa<UndefValue>(COp)) {
UndefBits.setBits(BitOffset, BitOffset + CstEltSizeInBits);
continue;
}
MaskBits.insertBits(cast<ConstantInt>(COp)->getValue(), BitOffset);
}
for (unsigned i = 0; i != NumMaskElts; ++i) {
unsigned BitOffset = i * MaskEltSizeInBits;
APInt EltUndef = UndefBits.extractBits(MaskEltSizeInBits, BitOffset);
if (EltUndef.isAllOnes()) {
UndefElts.setBit(i);
RawMask[i] = 0;
continue;
}
APInt EltBits = MaskBits.extractBits(MaskEltSizeInBits, BitOffset);
RawMask[i] = EltBits.getZExtValue();
}
return true;
}
void DecodePSHUFBMask(const Constant *C, unsigned Width,
SmallVectorImpl<int> &ShuffleMask) {
assert((Width == 128 || Width == 256 || Width == 512) &&
C->getType()->getPrimitiveSizeInBits() >= Width &&
"Unexpected vector size.");
APInt UndefElts;
SmallVector<uint64_t, 64> RawMask;
if (!extractConstantMask(C, 8, UndefElts, RawMask))
return;
unsigned NumElts = Width / 8;
assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
"Unexpected number of vector elements.");
for (unsigned i = 0; i != NumElts; ++i) {
if (UndefElts[i]) {
ShuffleMask.push_back(SM_SentinelUndef);
continue;
}
uint64_t Element = RawMask[i];
if (Element & (1 << 7))
ShuffleMask.push_back(SM_SentinelZero);
else {
unsigned Base = i & ~0xf;
int Index = Base + (Element & 0xf);
ShuffleMask.push_back(Index);
}
}
}
void DecodeVPERMILPMask(const Constant *C, unsigned ElSize, unsigned Width,
SmallVectorImpl<int> &ShuffleMask) {
assert((Width == 128 || Width == 256 || Width == 512) &&
C->getType()->getPrimitiveSizeInBits() >= Width &&
"Unexpected vector size.");
assert((ElSize == 32 || ElSize == 64) && "Unexpected vector element size.");
APInt UndefElts;
SmallVector<uint64_t, 16> RawMask;
if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
return;
unsigned NumElts = Width / ElSize;
unsigned NumEltsPerLane = 128 / ElSize;
assert((NumElts == 2 || NumElts == 4 || NumElts == 8 || NumElts == 16) &&
"Unexpected number of vector elements.");
for (unsigned i = 0; i != NumElts; ++i) {
if (UndefElts[i]) {
ShuffleMask.push_back(SM_SentinelUndef);
continue;
}
int Index = i & ~(NumEltsPerLane - 1);
uint64_t Element = RawMask[i];
if (ElSize == 64)
Index += (Element >> 1) & 0x1;
else
Index += Element & 0x3;
ShuffleMask.push_back(Index);
}
}
void DecodeVPERMIL2PMask(const Constant *C, unsigned M2Z, unsigned ElSize,
unsigned Width, SmallVectorImpl<int> &ShuffleMask) {
Type *MaskTy = C->getType();
unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
(void)MaskTySize;
assert((MaskTySize == 128 || MaskTySize == 256) && Width >= MaskTySize &&
"Unexpected vector size.");
APInt UndefElts;
SmallVector<uint64_t, 8> RawMask;
if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
return;
unsigned NumElts = Width / ElSize;
unsigned NumEltsPerLane = 128 / ElSize;
assert((NumElts == 2 || NumElts == 4 || NumElts == 8) &&
"Unexpected number of vector elements.");
for (unsigned i = 0; i != NumElts; ++i) {
if (UndefElts[i]) {
ShuffleMask.push_back(SM_SentinelUndef);
continue;
}
uint64_t Selector = RawMask[i];
unsigned MatchBit = (Selector >> 3) & 0x1;
if ((M2Z & 0x2) != 0u && MatchBit != (M2Z & 0x1)) {
ShuffleMask.push_back(SM_SentinelZero);
continue;
}
int Index = i & ~(NumEltsPerLane - 1);
if (ElSize == 64)
Index += (Selector >> 1) & 0x1;
else
Index += Selector & 0x3;
int Src = (Selector >> 2) & 0x1;
Index += Src * NumElts;
ShuffleMask.push_back(Index);
}
}
void DecodeVPPERMMask(const Constant *C, unsigned Width,
SmallVectorImpl<int> &ShuffleMask) {
Type *MaskTy = C->getType();
unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
(void)MaskTySize;
assert(Width == 128 && Width >= MaskTySize && "Unexpected vector size.");
APInt UndefElts;
SmallVector<uint64_t, 16> RawMask;
if (!extractConstantMask(C, 8, UndefElts, RawMask))
return;
unsigned NumElts = Width / 8;
assert(NumElts == 16 && "Unexpected number of vector elements.");
for (unsigned i = 0; i != NumElts; ++i) {
if (UndefElts[i]) {
ShuffleMask.push_back(SM_SentinelUndef);
continue;
}
uint64_t Element = RawMask[i];
uint64_t Index = Element & 0x1F;
uint64_t PermuteOp = (Element >> 5) & 0x7;
if (PermuteOp == 4) {
ShuffleMask.push_back(SM_SentinelZero);
continue;
}
if (PermuteOp != 0) {
ShuffleMask.clear();
return;
}
ShuffleMask.push_back((int)Index);
}
}
}